I have a thread that contains a blocking
recv(socket, buffer, sizeof(buffer), 0);
- Set a read timeout, withand
setsockopt(), and whenever it triggers check a state variable to see if you've told
yourself to stop reading.
- If you want to stop reading the socket forever, shut it down for input with. This will cause
shutdown(sd, SHUT_RD)to return
zero from now on.
- Set the socket into non-blocking mode and usewith a timeout to tell you when to read, adopting the same strategy with a
state variable as at (1) above. However non-blocking mode introduces
considerable complications into the send operation, so you should
prefer (1) or (2) above.
Your pseudo-code lacks EOS- and error-checking. I hope it doesn't
really look like that.
recv() exits only if:
data is read.
The read times out (SO_RCVTIMEO).
The connection is closed.
If none of those options are viable for you, you will have to simply not call
recv() in blocking mode until you know there is something waiting to be read, as reported by
Otherwise, re-write your socket logic to either:
use the socket in non-blocking mode.
RIOReceive/Ex() with Overlapped I/O or an I/O Completion Port. An I/O operation can be canceled with