共用方式為


Socket.Ttl 屬性

定義

取得或設定一個值,指定由 .Socket

public:
 property short Ttl { short get(); void set(short value); };
public short Ttl { get; set; }
member this.Ttl : int16 with get, set
Public Property Ttl As Short

屬性值

TTL 值。

例外狀況

TTL 值為負數。

這個套筒不屬於 InterNetwork or InterNetworkV6 系列。

嘗試存取該套接字時發生錯誤。 當嘗試將 TTL 設定為高於 255 時,也會回傳此錯誤。

該店 Socket 已經關閉。

範例

以下程式碼範例示範了該 Ttl 屬性的使用方式。

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("");
}

備註

TTL 值表示封包在丟棄封包前可通過的最大路由器數量,並會回傳網路控制訊息協定(ICMP)「TTL 超過」錯誤訊息給發送端。

TTL 值可設定為 0 到 255 之間。 當未設定此功能時,套接字的預設 TTL 值為 32。

若已成功建立連線,TCP/IP 堆疊會忽略在傳輸控制協定(TCP)套接字上設定此屬性。

如果你收到 SocketException,請使用該 SocketException.ErrorCode 屬性取得特定的錯誤代碼。 取得此程式碼後,請參閱 Windows Sockets 版本 2 API 錯誤代碼 文件,以獲得錯誤的詳細說明。

適用於