When does TcpClient NetworkStream complete a single read operation?

I am working on a project that involves exchanging a client server via TCP and the Google Protocol Buffer protocol. On the client side, I mainly use NetworkStream.Read () to block reading from the server through a byte array buffer.

According to the MSDN documentation,

This method reads the data into the buffer parameter and returns the number of bytes read successfully. If there is no data to read, the Read method returns 0. The 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.

This is the same case with asynchronous reading (NetworkStream.BeginRead and EndRead). My question is: when does Read () / EndRead () return? It seems to be coming back after filling all the bytes in the buffer. But in my own testing this is not the case. Bytes read in one operation vary greatly. I think this makes sense, because if there is a pause on the server side when sending messages, the client should not wait until the read buffer is full. Does Read () / EndRead () have a built-in timeout mechanism?

I tried to figure out how Mono implements Read () on NetworkStream and is tracked before the extern method is called on the Receive_internal () method.

+3
source share
2 answers

, . . .

, , . , . . .NET , .

, , , -, "", , , .

, , , (, , -) , .

BeginRead:

  • BeginRead(); → ......
  • 1
  • 2 ,
  • EndRead(); → EndRead();
  • - .
+2

, , , , .

, . , .

, (100 ) 50 , , NetworkStream.Read()?

. , , . . .

+1

All Articles