How much buffer does NetworkStream and TcpClient have?
We are writing a TCPServer and Client program. How much space is there in the TcpClient buffer? Like, at what point will开发者_如何学编程 it begin to throw away data? We are trying to determine if the TcpClient can be blocking or if it should go into it's own background thread(so that the buffer can not get full)..
You can get the buffer sizes from TcpClient.ReceiveBufferSize and TcpClient.SendBufferSize .
The available buffer sizes will vary as data is received/acknowledged(or not) at the TCP level. TcpClient is blocking by default.
No data will be thrown away as a result of full buffers, though data could be throw away in under error conditions (such as the peer disappears/crashes/exits etc.)
The MSDN documentation says the default size of the send and receive buffers for TcpClient
is 8192 bytes, or 8K. The documentation does not specify a limit as to how big these buffers can be.
As I'm sure you're aware, you send and receive data via the TcpClient
using its underlying NetworkStream
object. You are in control over whether these are synchronous or asynchronous operations. If you want synchronous behavior, use the Read
and Write
methods of NetworkStream
. If you want asynchronous behavior, use the BeginRead
/EndRead
and BeginWrite
/EndWrite
operations.
If you are receiving data as part of some front-end application, I would highly recommend doing this in a secondary thread, whether you do this using the asynchronous methods or synchronously in a separate thread. This will allow your UI to be responsive to the user while still handling the sending and receiving of data in the background.
精彩评论