Socket.RemoteEndPoint Proprietà

Definizione

Ottiene l'endpoint remoto.

public:
 property System::Net::EndPoint ^ RemoteEndPoint { System::Net::EndPoint ^ get(); };
public System.Net.EndPoint RemoteEndPoint { get; }
public System.Net.EndPoint? RemoteEndPoint { get; }
member this.RemoteEndPoint : System.Net.EndPoint
Public ReadOnly Property RemoteEndPoint As EndPoint

Valore della proprietà

EndPoint con cui comunica il Socket.

Eccezioni

Si è verificato un errore durante il tentativo di accesso al socket.

L'oggetto Socket è stato chiuso.

Esempio

L'esempio di codice seguente recupera e visualizza gli endpoint locali e remoti.

s->Connect(lep);

// Uses the RemoteEndPoint property.
Console::WriteLine("I am connected to {0} on port number {1}",
                   IPAddress::Parse((((IPEndPoint^)(s->RemoteEndPoint))->Address)->ToString()),
                   ((IPEndPoint^)(s->RemoteEndPoint))->Port.ToString());

// Uses the LocalEndPoint property.
Console::Write("My local IpAddress is : {0}\nI am connected on port number {1}",
               IPAddress::Parse((((IPEndPoint^)(s->LocalEndPoint))->Address)->ToString()),
               ((IPEndPoint^)(s->LocalEndPoint))->Port.ToString());
s.Connect(lep);

// Using the RemoteEndPoint property.
Console.WriteLine("I am connected to " + IPAddress.Parse(((IPEndPoint)s.RemoteEndPoint).Address.ToString()) + "on port number " + ((IPEndPoint)s.RemoteEndPoint).Port.ToString());

// Using the LocalEndPoint property.
Console.WriteLine("My local IpAddress is :" + IPAddress.Parse(((IPEndPoint)s.LocalEndPoint).Address.ToString()) + "I am connected on port number " + ((IPEndPoint)s.LocalEndPoint).Port.ToString());
s.Connect(lep)

' Using the RemoteEndPoint property.
Console.WriteLine("I am connected to ")
Console.WriteLine(IPAddress.Parse(CType(s.RemoteEndPoint, IPEndPoint).Address.ToString()))
Console.WriteLine("on port number ")
Console.WriteLine(CType(s.RemoteEndPoint, IPEndPoint).Port.ToString())

' Using the LocalEndPoint property.
Console.WriteLine("My local IpAddress is :")
Console.WriteLine(IPAddress.Parse(CType(s.LocalEndPoint, IPEndPoint).Address.ToString()))
Console.WriteLine("I am connected on port number ")
Console.WriteLine(CType(s.LocalEndPoint, IPEndPoint).Port.ToString())

Commenti

Se si usa un protocollo orientato alla connessione, la proprietà ottiene l'oggetto contenente l'indirizzo EndPoint IP remoto e il RemoteEndPoint numero di porta a cui Socket è connesso. Se si usa un protocollo senza connessione, RemoteEndPoint contiene l'indirizzo IP remoto predefinito e il numero di porta con cui comunicare Socket . È necessario eseguire il cast in EndPoint un oggetto IPEndPoint prima di recuperare tutte le informazioni. È quindi possibile chiamare il IPEndPoint.Address metodo per recuperare il metodo remoto e il IPEndPoint.Port metodo per recuperare il numero di porta remotaIPAddress.

L'oggetto RemoteEndPoint è impostato dopo una chiamata a Accept o Connect. Se si tenta di accedere a questa proprietà in precedenza, RemoteEndPoint verrà generata una SocketExceptionclasse . Se si riceve un SocketExceptionoggetto , utilizzare la proprietà per ottenere il SocketException.ErrorCode codice di errore specifico. Dopo aver ottenuto questo codice, fare riferimento alla documentazione del codice di errore dell'API Windows Sockets versione 2 per una descrizione dettagliata dell'errore.

Nota

Questo membro genera informazioni di traccia quando viene abilitata la funzionalità di traccia di rete nell'applicazione in uso. Per altre informazioni, vedere Traccia di rete in .NET Framework.

Si applica a

Vedi anche