January 28th, 2009, 09:35 AM
Socket Blocking Read TimeOut!
After calling Socket.Read(...), the socket times out after 5000 ms. Because I set Socket.ReceiveTimout to 5000. If it times out, an exception occurs and catch the exception. But I don't need to do something, so it should loop again and the whole process repeats. The problem however, is at the first time Socket.Read(...) blocks for 5000 ms, which is great, but after Timeout exception, it loops and call Socket.Read(...) again, but this time it immediatly throws a SocketError "WouldBlock" and does this infinitely and my processor turns to 100%. Why this exception? It has to read in blockmode again until Timeout occurs!
I googled and read some articles where they say WouldBlock means there is zero bytes received. I don't know about that, but than it should read in blocking mode again if nothing is received. My way of thinking is this. I establish a connection, than there is only a connection. The socket should than block if trying to read data, until I receive a few bytes, it's just that easy.
Can someone help me out here, cause I don't really know what's going on in behind the mask of Socket class.
sock.ReceiveTimeout = 5000;
socketState = true;
int rcv = sock.Receive(buffer, buffer.Length, SocketFlags.None);
if (rcv > 0)
byte data = new byte[rcv];
Buffer.BlockCopy(buffer, 0, data, 0, rcv);
socketState = false;
catch (SocketException ex)
if ((ex.SocketErrorCode != SocketError.TimedOut) &&
(ex.SocketErrorCode != SocketError.WouldBlock) &&
(ex.SocketErrorCode != SocketError.IOPending) &&
(ex.SocketErrorCode != SocketError.NoBufferSpaceAvailable))
// Something is wrong with the socketconnection, so delete and try all over again
socketState = false;
e.Result = (object)null;
// Do nothing, just loop again!
// That's the BIG problem, when I call sock.Receive(...) again, it won't block for 5000 ms,
// but throws WouldBlock exception immediatly! Why??? I just want it to read in blocking mode again
// and throws an exception if it times out again!
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!