Condividi tramite


Socket.Ttl Proprietà

Definizione

Ottiene o imposta un valore che specifica il valore TTL (Time To Live) dei pacchetti IP (Internet Protocol) inviati da 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

Valore della proprietà

Valore TTL.

Eccezioni

Il valore TTL è un numero negativo.

Il socket non si trova nelle InterNetwork famiglie o InterNetworkV6 .

Si è verificato un errore durante il tentativo di accesso al socket. Questo errore viene restituito anche quando è stato effettuato un tentativo di impostare TTL su un valore superiore a 255.

L'oggetto Socket è stato chiuso.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso della Ttl proprietà .

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

Commenti

Il valore TTL indica il numero massimo di router che il pacchetto può attraversare prima che il router elimini il pacchetto e venga restituito al mittente un messaggio di errore ICMP (Internet Control Message Protocol) "TTL superato".

Il valore TTL può essere impostato su un valore compreso tra 0 e 255. Quando questa proprietà non è impostata, il valore TTL predefinito per un socket è 32.

L'impostazione di questa proprietà su un socket TCP (Transmission Control Protocol) viene ignorata dallo stack TCP/IP se è stata stabilita una connessione con esito positivo tramite il socket.

Se si riceve un oggetto SocketException, utilizzare la SocketException.ErrorCode proprietà per ottenere il codice di errore specifico. Dopo aver ottenuto questo codice, fare riferimento alla documentazione relativa al codice di errore dell'API Windows Sockets versione 2 per una descrizione dettagliata dell'errore.

Si applica a