C++ Winsock recv not blocking

拈花ヽ惹草 提交于 2019-12-25 08:56:58

问题


I'm devleoping a server in c++ and when im using recv() in a while loop it returns all the time length of -1 and also continue the loop without blocking.

while(true)
{
   char buffer[1024];
   int dataLength =  recv(socket, buffer, 1024, 0)
   cout << dataLength;
}

output: -1-1-1-1-1-1-1-1-1.............

Anyone knows how to make this socket to block until it recievs data?

Thanks in advance!


回答1:


maybe there is an error during initilization try calling GetLastError() and printing the result after each call




回答2:


As James suggested, check if a socket error occured. recv()s default behaviour is to block. See also this thread here.



来源:https://stackoverflow.com/questions/11065017/c-winsock-recv-not-blocking

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