What are some of the reasons NetworkStream.Read hangs / blocks?

The MSDN documentation seems to suggest that NetworkStream.Read will always return immediately. If no data is found, it returns 0. However, I have some code that is currently deployed, which only in some cases (and I haven't figured out which ones yet), NetworkStream.Read seems to freeze. Here is the stack trace I was able to collect from the dump file

 00000000705ae850 000007fef784f60d DomainBoundILStubClass.IL_STUB (IntPtr, Byte *, Int32, System.Net.Sockets.SocketFlags)
 00000000705ae930 000007fef785c930 System.Net.Sockets.Socket.Receive (Byte [], Int32, Int32, System.Net.Sockets.SocketFlags, System.Net.Sockets.SocketError ByRef)
 00000000705ae9b0 000007ff004eb668 System.Net.Sockets.NetworkStream.Read (Byte [], Int32, Int32)
 00000000705aea40 000007fef784e6ae MySocketStuff.SocketConnectCallback (System.IAsyncResult)
 00000000705aeb20 000007fef84f2bbb System.Net.LazyAsyncResult.Complete (IntPtr)
 00000000705aeb90 000007fef7853c7b System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
 00000000705aebe0 000007fef784e5d3 System.Net.ContextAwareResult.Complete (IntPtr)
 00000000705aec40 000007fef7d027f9 System.Net.LazyAsyncResult.ProtectedInvokeCallback (System.Object, IntPtr)
 00000000705aeca0 000007fef8b9815e System.Net.Sockets.Socket.ConnectCallback ()
 00000000705aed20 000007fef93e14c2 System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback (System.Object, Boolean)

I notice that NetworkStrea.Read actually calls Socket.Receive, which can be blocked, as I understand it. I just don’t know why sometimes it is blocked, and sometimes not.

+8
c # sockets blocking networkstream
source share
4 answers

The "Notes" section of the documentation for NetworkStream.Read is misleading. It says:

This method reads the data into the buffer parameter and returns the number of bytes read successfully. If no data is available for reading, the Read method returns 0. A read operation reads as much data as is available, up to the number of bytes specified by the size parameter. If the remote node disconnects the connection and all available data has been received, the Read method terminates immediately and returns zero bytes.

He must say:

This method reads the data into the buffer parameter and returns the number of bytes read successfully. If the data is not readable, the read method is blocked until the data becomes available or the connection is closed. The read operation reads as much data as is available, up to the number of bytes specified by the parameter size. If the remote node disconnects the connection and all available data has been received, the Read method terminates immediately and returns zero bytes.

+24
source share

Sometimes there will be data in the socket buffer, and sometimes it will not.

One common reason to view a NetworkStream is because each side of the connection expects the other to close. For example, if you create an HTTP 1.1 keep-alive connection, but still do a “read before closing the connection” way to get the content.

+7
source share

One common mistake when working with NetworkStream is to send incomplete commands using the Write method, which calls a sequential Read call.

See the example below that is trying to send a username to an open FTP port. It expects a response as 331. Specify a password, but the Read method hangs:

 var request = Encoding.ASCII.GetBytes("user [username]"); networkStream.Write(request, 0, request.Length); var streamReader = new StreamReader(networkStream); var response = streamReader.ReadLine(); // <-- hangs 

The magic solution is to replace the first line as follows:

 var request = Encoding.ASCII.GetBytes("user [username] \r\n"); 

Just by adding the \ r \ n phrase at the end of the command, everything will start working as expected.

+1
source share

If read data is not available, the Read method will block until data is available. Consider using Async Socket features if you do not want to block. http://msdn.microsoft.com/en-us/library/bbx2eya8.aspx

0
source share

All Articles