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 的額外負荷。

適用於

另請參閱