1

I'm wondering why closing a socket with the standard close function without a prior call to the shutdown function after failing to sending data due to an unavailable peer doesn't cause a recv function on the same socket to return ?

pseudo code:

void sendData()
{
  ::send(socket_,...);
  if(error)
     close(socket_); //recv still block after this call
}

void worker()
{
   while(1)
   {
      ::recv(socket_...)

      if(error)
        break;
   }
}
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
  • 1
    http://stackoverflow.com/questions/3624365/blocking-recv-doesnt-exit-when-closing-socket-from-another-thread – Ashalynd Nov 24 '14 at 19:54

1 Answers1

2

Correct. It causes recv() to exit with an end of stream status.

Your code never examines the return value, so it can never detect that it's zero. You need to store the result into a variable; check it for -1 indicating an error; check it for zero indicating end of stream; and otherwise use it as the length of the data returned.

user207421
  • 305,947
  • 44
  • 307
  • 483