I have specified the ReceiveTimout
as 40 ms. But it takes more than 500ms for the receive to timeout. I am using a Stopwatch to compute the timetaken.
The code is shown below.
Socket TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
TCPSocket.ReceiveTimeout = 40;
try
{
TCPSocket.Receive(Buffer);
} catch(SocketException e) { }
I found this one:
You can synchronously poll on the socket with any timeout you wish. If
Poll()
returnstrue
, you can be certain that you can make a call toReceive()
that won't block.I recommend you read Stevens' UNIX Network Programming chapters 6 and 16 for more in-depth information on non-blocking socket usage. Even though the book has UNIX in its name, the overall sockets architecture is essentially the same in UNIX and Windows (and .net)
You cannot use timeout values which are less than 500ms. See here for
SendTimeout
: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.sendtimeoutEven though MSDN doesn't state the same requirement for the
ReceiveTimeout
, my experience shows that this restriction is still there.You can also read more about this on several SO posts: