Socket.NoDelay 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
public:
property bool NoDelay { bool get(); void set(bool value); };
public bool NoDelay { get; set; }
member this.NoDelay : bool with get, set
Public Property NoDelay As Boolean
屬性值
false 若使用 Socket Nagle 演算法;否則, true。 預設值為 false。
例外狀況
嘗試存取 Socket.
該店 Socket 已經關閉。
範例
以下程式碼範例示範了該 NoDelay 屬性的使用方式。
static void ConfigureTcpSocket(Socket tcpSocket)
{
// Don't allow another socket to bind to this port.
tcpSocket.ExclusiveAddressUse = true;
// The socket will linger for 10 seconds after
// Socket.Close is called.
tcpSocket.LingerState = new LingerOption (true, 10);
// Disable the Nagle Algorithm for this tcp socket.
tcpSocket.NoDelay = true;
// Set the receive buffer size to 8k
tcpSocket.ReceiveBufferSize = 8192;
// Set the timeout for synchronous receive methods to
// 1 second (1000 milliseconds.)
tcpSocket.ReceiveTimeout = 1000;
// Set the send buffer size to 8k.
tcpSocket.SendBufferSize = 8192;
// Set the timeout for synchronous send methods
// to 1 second (1000 milliseconds.)
tcpSocket.SendTimeout = 1000;
// Set the Time To Live (TTL) to 42 router hops.
tcpSocket.Ttl = 42;
Console.WriteLine("Tcp Socket configured:");
Console.WriteLine($" ExclusiveAddressUse {tcpSocket.ExclusiveAddressUse}");
Console.WriteLine($" LingerState {tcpSocket.LingerState.Enabled}, {tcpSocket.LingerState.LingerTime}");
Console.WriteLine($" NoDelay {tcpSocket.NoDelay}");
Console.WriteLine($" ReceiveBufferSize {tcpSocket.ReceiveBufferSize}");
Console.WriteLine($" ReceiveTimeout {tcpSocket.ReceiveTimeout}");
Console.WriteLine($" SendBufferSize {tcpSocket.SendBufferSize}");
Console.WriteLine($" SendTimeout {tcpSocket.SendTimeout}");
Console.WriteLine($" Ttl {tcpSocket.Ttl}");
Console.WriteLine($" IsBound {tcpSocket.IsBound}");
Console.WriteLine("");
}
備註
Nagle 演算法的設計目的是透過讓套接字緩衝小型封包,然後在特定情況下合併並匯入一個封包來減少網路流量。 一個 TCP 封包包含 40 位元組的標頭加上被傳送的資料。 當 TCP 傳送小封包時,TCP 標頭所產生的開銷可能成為網路流量的重要部分。 在負載過重的網路上,這種開銷造成的壅塞可能導致資料報遺失與重傳,以及因擁塞而產生的過長傳播時間。 當用戶收到新的外出資料時,若連線上有任何先前傳送的資料未被確認,Nagle 演算法會阻止傳送新的 TCP 區段。
大多數網路應用應使用 Nagle 演算法。
在使用者資料報協定(UDP)套接字上設定此屬性不會有影響。