结束“recv()”使用 Winsock 读取所有信息时循环
我的winsock 的recv() 循环遇到问题。我试图在 iResult==0 时终止循环,但是,循环仅在套接字关闭时结束。它似乎挂在最后一个recv()处,其中iResult等于0。那么关于如何有效地终止循环有什么想法吗?我的最终目标(无论 iResult == 0 与否;也许我的处理方式是错误的)是在读取所有发送的信息后停止循环。这是循环。
do
{
iResult = recv(socket, recvbuf, BUFLEN-1, 0);
if(iResult > 0){
// Null byte :)
// Remove that garbage >:(
recvbuf[iResult] = '\0';
printf("Recvbuf: %s\n\n\niResult: %d\n",recvbuf,iResult);
continue; // working properly
}
else if(iResult == 0)
// Connection closed properly
break;
else
{
printf("ERROR! %ld",WSAGetLastError());
break;
}
} while(iResult > 0);
就像我说的,我正在接收所有数据,我只是无法退出循环。下一步是将数据写回服务器,但它会挂在这里直到 ping 超时。套接字是 SOCK_STREAM,BUFLEN 定义为 0x200
谢谢
I am having an issue in my recv() loop for winsock. I am trying to terminate the loop when iResult==0, however, the loop only ends when the socket closes. It appears to be hanging at the very last recv() where iResult would equal 0. So any ideas on how to terminate the loop effectively? My ultimate goal (whether iResult == 0 or not; perhaps I am going about this the wrong way) is to stop the loop when all the sent information has been read. Here is the loop.
do
{
iResult = recv(socket, recvbuf, BUFLEN-1, 0);
if(iResult > 0){
// Null byte :)
// Remove that garbage >:(
recvbuf[iResult] = '\0';
printf("Recvbuf: %s\n\n\niResult: %d\n",recvbuf,iResult);
continue; // working properly
}
else if(iResult == 0)
// Connection closed properly
break;
else
{
printf("ERROR! %ld",WSAGetLastError());
break;
}
} while(iResult > 0);
Like I said, I am receiving all the data, I just cannot exit the loop. The next step would to be write data back to the server, but it hangs here until ping timeout. Socket is SOCK_STREAM and BUFLEN is defined as 0x200
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,
recv
如果没有数据要接收则阻塞:您可以使用 ioctlsocket 放置套接字在非阻塞模式下:
编辑:这是我在早期版本中提出的setsockopt建议,然后删除(请参阅评论):
By default, instead of returning 0,
recv
blocks if there's no data to receive :You can use ioctlsocket to put the socket in non-blocking mode:
EDIT: Here's the setsockopt suggestion that I made in an earlier rev, then removed (see comments):
当您设计 TCP 通信机制时,您必须定义消息边界。
(通常是\r\n)。就其本身而言,tcp 不知道边界;你必须自己做这件事。
recv() 调用可能并不总是在消息边界上返回。一个 send() 可能会在另一端拆分为多个 receive()-s。
When you design a TCP communication mechanism you have to define message boundaries.
(often \r\n). In of itself, tcp doesn't know about boundaries; you have to do this yourself.
The recv() call may not always return on a message boundary. One send() might get split into multiple recv()-s on the other end.