Socket.Ttl 屬性

定義

取得或設定值,指定 Socket 傳送之網際網路通訊協定 (IP) 封包的存留時間 (TTL) 值。

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 值是負數。

套接字不在 或 InterNetworkV6 系列中InterNetwork

嘗試存取通訊端時發生錯誤。 當嘗試將 TTL 設定為大於 255 的值時,也會傳回這個錯誤。

範例

下列程式代碼範例示範 屬性的使用 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 = gcnew 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 {0}", 
        tcpSocket->ExclusiveAddressUse);
    Console::WriteLine("  LingerState {0}, {1}", 
        tcpSocket->LingerState->Enabled,
        tcpSocket->LingerState->LingerTime);
    Console::WriteLine("  NoDelay {0}",
        tcpSocket->NoDelay);
    Console::WriteLine("  ReceiveBufferSize {0}", 
        tcpSocket->ReceiveBufferSize);
    Console::WriteLine("  ReceiveTimeout {0}",
        tcpSocket->ReceiveTimeout);
    Console::WriteLine("  SendBufferSize {0}",
        tcpSocket->SendBufferSize);
    Console::WriteLine("  SendTimeout {0}",
        tcpSocket->SendTimeout);
    Console::WriteLine("  Ttl {0}",
        tcpSocket->Ttl);
    Console::WriteLine("  IsBound {0}",
        tcpSocket->IsBound);
    Console::WriteLine("");
}
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 錯誤碼 檔,以取得錯誤的詳細描述。

適用於