C++ Winsock recv not blocking

2019-08-18 22:37发布

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!

标签: c++ winsock
2条回答
你好瞎i
2楼-- · 2019-08-18 23:24

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

查看更多
冷血范
3楼-- · 2019-08-18 23:24

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

查看更多
登录 后发表回答