我有以下功能设置,每2分钟从服务器接收数据.我第一次调用函数它似乎工作,但然后它冻结在recv永远不会返回的调用.我是否需要在每次调用时分配缓冲区,即使服务器没有任何内容可以发送?
#define RCVBUFSIZE 32
void Receive()
{
UINT totalBytesRcvd = 0, bytesRcvd = 0;
char buffer[RCVBUFSIZE]; /* Buffer for string */
/* Receive up to the buffer size (minus 1 to leave space for
a null terminator) bytes from the sender */
bytesRcvd = recv(sockClient, buffer, RCVBUFSIZE - 1, 0);
if (bytesRcvd)
{
buffer[bytesRcvd] = '\0';
MessageBox(0, buffer, 0, 0); //some way to display the received buffer
}
else if (bytesRcvd == SOCKET_ERROR)
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)