如何在不关闭服务器套接字的情况下在C++客户端的主循环中接收数据?
How do I receive data in a C++ client's main loop without closing the server socket?
我正在阅读这个winsock示例。
我正在尝试概念化如何创建一个具有到C#.NET服务器的持久TCP/IP连接的C++客户端程序。
我看到的问题是,为了让C++客户端离开接收循环,服务器必须关闭与客户端的套接字连接。
在我的情况下,服务器每隔几秒钟就会向客户端发送一次。我需要能够从服务器接收一个数据包,并重新启动主程序循环,以便客户端能够执行其其余功能。
如果这个接收代码在C++客户端的主循环中,那么如果服务器从未关闭与客户端的连接,客户端将永远不会停止接收:
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %dn", iResult);
else if ( iResult == 0 )
printf("Connection closedn");
else
printf("recv failed with error: %dn", WSAGetLastError());
} while( iResult > 0 );
您选择使用的示例程序设计用于发送单个请求和接收单个响应。它使用连接的状态来指示请求的结束和响应的结束。
您可能希望使用不同的示例程序。在谷歌上搜索"winsock聊天示例"。
另一方面,要按照您的要求修改此程序,您可以将do-while循环替换为:
// Assume that the OP's protocol sends 100-byte packets.
// Each while iteration attempts to receive one such packet.
// The while exits when the other side closes its connection.
while(1) {
// Receive 100-byte packet
int receivedBytes;
for(receivedBytes = 0; receivedBytes < 100; ) {
int iResult = recv(ConnectSocket,
recvbuf+receivedBytes,
recvbuflen-receivedBytes, 0);
if ( iResult > 0 ) {
receivedBytes += iResult;
printf("Bytes received: %dn", iResult);
} else if ( iResult == 0 ) {
printf("Connection closedn");
break;
} else {
printf("recv failed with error: %dn", WSAGetLastError());
break;
}
}
if(receivedBytes == 100) {
// ... process packet here
} else {
break;
}
}
这里的问题是您无法知道recv()将在什么时候返回。有五种解决方法:
- 使插座不堵塞。(不推荐)
- 使用select()并超时
- 使用异步套接字。这可能是最快的方法,但更复杂
- 使用螺纹
- BSD kqueue
相关文章:
- 与多个 for 循环与单个 for 循环 wrt 相关的性能从多映射获取数据
- OpenMP:for 循环避免数据竞争,而无需使用关键
- 有没有办法在C++中循环访问对象的不同数据成员
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 类方法 - 数据结构中 For 循环的运行时错误
- 用于循环访问多个集合的数据结构
- 增强循环缓冲区push_back在前面插入数据
- 从文件导入数据时遇到问题.我有一个没完没了的循环
- 它不显示数据,只显示永无止境的循环
- C++:从文件中读取x y数据会产生无限循环吗
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 将结构数据存储在循环缓冲区中
- 利用输入文件中的数据(字符串和int)并在循环中利用
- boost::asio::async_read 无限循环,接收数据为零字节
- 如何使用for循环将数据保存在不同的文件中?
- 查找一组循环数据的中位数
- 循环还是没有循环?(数据集很小,不需要更改)
- C 循环数据结构
- 循环数据依赖关系析构函数
- 将文件读取到内存,循环数据,然后写入文件