Setting the send / receive socket timeout to less than 500 ms in .NET.

According to the MSDN documentation, it is not possible to set the value of Socket.SendTimeout to less than 500 ms: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.sendtimeout The same rule applies for Socket.ReceiveTimeout (even this is not mentioned in the MSDN documentation, this is true, since both cases have been tested in practice).

Are there other ways to timeout a socket get operation if it, for example, takes more than 10 ms?

+7
source share
1 answer

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)

+9
source

Source: https://habr.com/ru/post/908701/


All Articles