关闭

TCP方式的Socket

标签: POCOtcpC++
156人阅读 评论(0) 收藏 举报
分类:

TCPServer要用TCP的客户端来测试。POCO中TCP方式的Socket有:

Poco::Net::ServerSocket
Poco::Net::StreamSocket
Poco::Net::DialogSocket
Poco::Net::SecureServerSocket       ---相对于SSL
Poco::Net::SecureStreamSocket      ---相对于SSL


复制代码
 1     ServerSocket svs(0);
 2     TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs);
 3     srv.start();
 4     SocketAddress sa("localhost", svs.address().port());
 5     StreamSocket ss1(sa);
 6     std::string data("hello, world");
 7     ss1.sendBytes(data.data(), (int) data.size());
 8     char buffer[256];
 9     int n = ss1.receiveBytes(buffer, sizeof(buffer));
10     cout<<"n = "<<n<<endl;
11     ss1.close();   
复制代码
复制代码
 1         EchoServer echoServer;
 2         DialogSocket ds;
 3         ds.connect(SocketAddress("localhost",echoServer.port()));
 4         ds.sendMessage("Hello,world!");
 5         std::string str;
 6         ds.receiveMessage(str);
 7         cout<<"str = "<<str<<endl;
 8 
 9         ds.sendMessage("KAREN","appinf.com");
10         ds.receiveMessage(str);
11         cout<<"str = "<<str<<endl;
12 
13         ds.sendMessage("220 Hello, world!");
14         int status = ds.receiveStatusMessage(str);
15         cout<<"status = "<<status<<endl;
16 
17         ds.sendString("220-line1\r\n220 line2\r\n");
18         status = ds.receiveStatusMessage(str);
19         cout<<"status = "<<status<<endl;
20         cout<<"str = "<<str<<endl;
复制代码

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8723次
    • 积分:264
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:15篇
    • 译文:0篇
    • 评论:0条
    文章分类