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个连接。程序正常。好,下面考虑将服务器从单线程改成多线程模型。
(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个连接。程序正常。好,下面考虑将服务器从单线程改成多线程模型。
相关文章推荐
- boost::asio 连接管理6
- boost::asio 连接管理8
- boost::asio 连接管理1
- boost::asio 连接管理3
- boost::asio 连接管理2
- boost::asio 连接管理4
- boost::asio 连接管理8
- boost::asio 连接管理11 如何关闭连接
- boost::asio 连接管理5
- boost::asio 连接管理10
- boost::asio 连接管理11 如何关闭连接
- boost::asio 连接管理9
- boost asio 应用方法学——对象生命期管理
- boost::asio::ip::tcp::socket is connected?(如何知道socket已经连接?)
- boost::asio设置同步连接超时
- Asio服务端连接管理,实现广播数据
- boost::asio::ip::tcp::socket is connected?(如何知道socket已经连接?)
- boost.asio防止恶意空连接的方法
- boost::asio 异步主动连接多个服务器
- boost.asio防止恶意空连接的方法