TcpClient.ReceiveBufferSize 屬性

定義

取得或設定接收緩衝區的大小。Gets or sets the size of the receive buffer.

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

屬性值

接收緩衝區的大小,以位元組為單位。The size of the receive buffer, in bytes. 預設值為 8192 位元組。The default value is 8192 bytes.

例外狀況

設定緩衝區大小時發生錯誤。An error occurred when setting the buffer size.

-或--or-

在 .NET Compact Framework 應用程式中,您無法設定這個屬性。In .NET Compact Framework applications, you cannot set this property. 如需解決方法,請參閱<註解>中的<平台注意事項>。For a workaround, see the Platform Note in Remarks.

範例

下列程式碼範例會設定並取得接收緩衝區大小。The following code example sets and gets the receive buffer size.

// 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 屬性會取得或設定您預期要在每個讀取作業的接收緩衝區中儲存的位元組數目。The ReceiveBufferSize property gets or sets the number of bytes that you are expecting to store in the receive buffer for each read operation. 這個屬性實際上會操控配置給接收傳入資料的網路緩衝區空間。This property actually manipulates the network buffer space allocated for receiving incoming data.

您的網路緩衝區應該至少與您的應用程式緩衝區一樣大,以確保當您呼叫 NetworkStream.Read 方法時,會提供所需的資料。Your network buffer should be at least as large as your application buffer to ensure that the desired data will be available when you call the NetworkStream.Read method. 使用 [ReceiveBufferSize] 屬性來設定此大小。Use the ReceiveBufferSize property to set this size. 如果您的應用程式將接收大量資料,您應該將 Read 的方法傳遞至非常大型的應用程式緩衝區。If your application will be receiving bulk data, you should pass the Read method a very large application buffer.

如果網路緩衝區小於您在 Read 方法中要求的資料量,您就無法在一個讀取作業中取得所需的資料量。If the network buffer is smaller than the amount of data you request in the Read method, you will not be able to retrieve the desired amount of data in one read operation. 這會造成額外呼叫 Read 方法的額外負荷。This incurs the overhead of additional calls to the Read method.

適用於

另請參閱