boost:asio 联接管理6

boost::asio 连接管理6

先来一个newLISP程序测试,发起一个连接,发送一个正确字符'a',连接不断,再发送一个错误字符,连接断开。

测试脚本如下:

(define (quit-for-error)
  ((println (net-error)) (exit)))
(set 'socket (net-connect "localhost" 8888))
(if (net-send socket "a") (println "send a successfully") (quit-for-error))
(if socket (println (net-send socket "c")) (quit-for-error))
(exit)

newLISP脚本这里解释一下:

quit-for-error是自定义的函数,负责打印网络错误信息,并退出程序。

每一个net-send都要判断一下返回值,如果是nil则退出。


运行一下:

newlisp ./test_wrong_char.lsp
send a successfully
1


服务程序打印出来的结果是:

 ./cppapplication_4 
the number of connections is: 1
the new connection object is starting now.
correct data received
wrong data received, char is:99
closing the socket
~Connection

后面要写一个多线程的脚本,发起10个连接。