The simple answer is "you do not."
Send()
and Receive()
calls block the program flow until data is sent, received, or an error occurs.
If you want more control over your challenges, several mechanisms are available. The simplest is to use Poll()
.
Socket s; // ... // Poll the socket for reception with a 10 ms timeout. if (s.Poll(10000, SelectMode.SelectRead)) { s.Receive(); // This call will not block } else { // Timed out }
You can also use Select()
, BeginReceive()
or ReceiveAsync()
for other types of behavior.
I recommend that you read Chapters 6 and 16 of Stevens' UNIX Network Programming for more information on non-blocking socket usage. Although the book has UNIX in its title, the general socket architecture is essentially the same on UNIX and Windows (and .net)
source share