您的位置:首页 > 编程语言 > C语言/C++

boost::asio 连接管理7

2013-01-15 20:58 405 查看
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.lsp
send a successfullysend a successfully
send a successfullysend a successfully

send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
1

send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
send a successfully
send a successfully
send a successfully
send a successfully
1
服务程序显示如下:
./cppapplication_4
the number of connections is: 1
the new connection object is starting now.
correct data received
the number of connections is: 2
the new connection object is starting now.
correct data received
the number of connections is: 3
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
correct data received
wrong data received, char is:113
closing the socket
~Connection
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
the number of connections is: 1
the new connection object is starting now.
the number of connections is: 2
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
correct data received
correct data received
the number of connections is: 2
the new connection object is starting now.
correct data received
correct data received
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
correct data received
correct data received
wrong data received, char is:113
closing the socket
~Connection
仔细检查,创建了10个连接,又关闭了10个连接。程序正常。好,下面考虑将服务器从单线程改成多线程模型。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++