TcpClient.ReceiveBufferSize 属性

定义

获取或设置接收缓冲区的大小。

public:
 property int ReceiveBufferSize { int get(); void set(int value); };
public int ReceiveBufferSize { get; set; }
member this.ReceiveBufferSize : int with get, set
Public Property ReceiveBufferSize As Integer

属性值

接收缓冲区的大小(以字节为单位)。 默认值为 8192 字节。

例外

设置缓冲区大小时出错。

在 .NET Compact Framework 应用程序中,无法设置此属性。 有关解决方法,请参阅“备注”中的“平台说明”。

示例

下面的代码示例设置并获取接收缓冲区大小。

// sets the receive buffer size using the ReceiveBufferSize public property.
tcpClient->ReceiveBufferSize = 1024;

// gets the receive buffer size using the ReceiveBufferSize public property.
if ( tcpClient->ReceiveBufferSize == 1024 )
      Console::WriteLine( "The receive buffer was successfully set to {0}", tcpClient->ReceiveBufferSize );
// Sets the receive buffer size using the ReceiveBufferSize public property.
tcpClient.ReceiveBufferSize = 1024;

// Gets the receive buffer size using the ReceiveBufferSize public property.
if (tcpClient.ReceiveBufferSize == 1024)
    Console.WriteLine ("The receive buffer was successfully set to " + tcpClient.ReceiveBufferSize.ToString ());
' Sets the receive buffer size using the ReceiveBufferSize public property.
tcpClient.ReceiveBufferSize = 1024

' Gets the receive buffer size using the ReceiveBufferSize public property.
If tcpClient.ReceiveBufferSize = 1024 Then
   Console.WriteLine(("The receive buffer was successfully set to " + tcpClient.ReceiveBufferSize.ToString()))
End If

注解

属性 ReceiveBufferSize 获取或设置每个读取操作预期存储在接收缓冲区中的字节数。 此属性实际上操作分配用于接收传入数据的网络缓冲区空间。

网络缓冲区应至少与应用程序缓冲区一样大,以确保在调用 NetworkStream.Read 方法时,所需的数据可用。 ReceiveBufferSize使用 属性设置此大小。 如果应用程序将接收批量数据,则应向 方法传递 Read 一个非常大的应用程序缓冲区。

如果网络缓冲区小于在 方法中 Read 请求的数据量,则无法在一次读取操作中检索所需的数据量。 这会产生对 方法的其他调用的 Read 开销。

适用于

另请参阅