This documentation is archived and is not being maintained.

Socket.RemoteEndPoint Property

Gets the remote endpoint.

[Visual Basic]
Public ReadOnly Property RemoteEndPoint As EndPoint
[C#]
public EndPoint RemoteEndPoint {get;}
[C++]
public: __property EndPoint* get_RemoteEndPoint();
[JScript]
public function get RemoteEndPoint() : EndPoint;

Property Value

The EndPoint with which the Socket is communicating.

Exceptions

Exception Type Condition
SocketException An error occurred when attempting to access the socket. See the Remarks section for more information.
ObjectDisposedException The Socket has been closed.

Remarks

If you are using a connection-oriented protocol, the RemoteEndPoint property gets the EndPoint containing the remote IP address and port number to which the Socket is connected. If you are using a connectionless protocol, RemoteEndPoint contains the default remote IP address and port number with which the Socket will communicate. You must cast this EndPoint to an IPEndPoint before retrieving any information. You can then call the IPEndPoint.Address method to retrieve the remote IPAddress, and the IPEndPoint.Port method to retrieve the remote port number.

The RemoteEndPoint is set after a call to either Accept or Connect. If you try to access this property earlier, RemoteEndPoint will throw a SocketException. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.

Example

[Visual Basic, C#, C++] The following example retrieves and displays the local and remote endpoints.

[Visual Basic] 
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())


[C#] 
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 ());


[C++] 
s->Connect(lep);

// Uses the RemoteEndPoint property.
Console::Write("I am connected to ");
Console::Write(IPAddress::Parse((__try_cast<IPEndPoint *>(s->RemoteEndPoint)->Address)->ToString()));
Console::Write("on port number ");
Console::WriteLine(__box(__try_cast<IPEndPoint *>(s->RemoteEndPoint)->Port)->ToString());

// Uses the LocalEndPoint property.
Console::Write("My local IpAddress is :");
Console::Write(IPAddress::Parse((__try_cast<IPEndPoint *>(s->LocalEndPoint)->Address)->ToString()));
Console::Write("I am connected on port number ");
Console::WriteLine(__box(__try_cast<IPEndPoint *>(s->LocalEndPoint)->Port)->ToString());

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace | EndPoint | Connect | Accept

Show: