tags:

views:

171

answers:

3

For a TCP blocking socket, is it safe to call:

if(SOCKET_ERROR != recv(s, NULL, 0, 0))
//...

to detect errors?

I thought it was safe, then I had a situation on a computer that it was hanging on this statement. (was with an ssl socket if that matters). I also tried passing in the MSG_PEEK flag with a buffer specified but I also had a hang there.

What is the alternative?

+2  A: 

You did say you were using a blocking socket, but you should use select instead (even if you set it unblocking before the select call). It allows you to poll a file descriptor and see if there is data waiting, or if there is an error.

Dean Povey
+1  A: 

The call itself is "safe" in a sense that it should work as documented, however you must realize that recv is a blocking receive call. This means that the call will block the executing thread until data arrives on the socket. This can cause your application to "hang" if you are not using a different thread to do your receive, or checking to see if data is available before call receive (select).

heavyd
+2  A: 
Nikolai N Fetissov