boost::asio 连接管理7

来源:互联网 发布:打印机端口名称是什么 编辑:程序博客网 时间:2024/05/21 23:34

newLISP提供了简单的方法让我创建多个进程。下面的程序创建10个进程,每个进程发送几个'a', 最后发送一个'q'.

(define (quit-for-error)  ((println (net-error)) (exit)))(define (send-test)  (set 'socket (net-connect "localhost" 8888))  (if (net-send socket "a") (println "send a successfully") (quit-for-error))  (if (net-send socket "a") (println "send a successfully") (quit-for-error))  (if (net-send socket "a") (println "send a successfully") (quit-for-error))  (if (net-send socket "a") (println "send a successfully") (quit-for-error))  (if socket (println (net-send socket "q")) (quit-for-error))  (exit))(spawn 'r1 (send-test))(spawn 'r2 (send-test))(spawn 'r3 (send-test))(spawn 'r4 (send-test))(spawn 'r5 (send-test))(spawn 'r6 (send-test))(spawn 'r7 (send-test))(spawn 'r8 (send-test))(spawn 'r9 (send-test))(spawn 'r10 (send-test))(until (sync 1000))(exit)

spawn 创建了子进程,用来执行函数send-test.

一共创建了10个进程。

until是一个等待,直到所有子进程都退出才返回。


现在运行测试程序:

 newlisp ./parallel_text.lspsend a successfullysend a successfullysend a successfullysend a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfully1send a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1send a successfullysend a successfullysend a successfullysend a successfully1

服务程序显示如下:

 ./cppapplication_4 the number of connections is: 1the new connection object is starting now.correct data receivedthe number of connections is: 2the new connection object is starting now.correct data receivedthe number of connections is: 3the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectionthe number of connections is: 1the new connection object is starting now.the number of connections is: 2the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedthe number of connections is: 2the new connection object is starting now.correct data receivedcorrect data receivedcorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connectioncorrect data receivedcorrect data receivedwrong data received, char is:113closing the socket~Connection

仔细检查,创建了10个连接,又关闭了10个连接。程序正常。好,下面考虑将服务器从单线程改成多线程模型。



原创粉丝点击