Ending “recv()” loop when all information is Read using Winsock

寵の児 提交于 2019-12-04 05:18:37

By default, instead of returning 0, recv blocks if there's no data to receive :

If no incoming data is available at the socket, the recv call blocks and waits for data to arrive according to the blocking rules defined for WSARecv with the MSG_PARTIAL flag not set unless the socket is nonblocking. In this case, a value of SOCKET_ERROR is returned with the error code set to WSAEWOULDBLOCK. The select, WSAAsyncSelect, or WSAEventSelect functions can be used to determine when more data arrives.

You can use ioctlsocket to put the socket in non-blocking mode:

u_long iMode = 1;
ioctlsocket(socket, FIONBIO, &iMode);

EDIT: Here's the setsockopt suggestion that I made in an earlier rev, then removed (see comments):

You can use the setsockopt function with the SO_RCVTIMEO option to set the socket to timeout on recv if no data is available.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!