This documentation is archived and is not being maintained.

Socket.LocalEndPoint Property

Gets the local endpoint.

[Visual Basic]
Public ReadOnly Property LocalEndPoint As EndPoint
public EndPoint LocalEndPoint {get;}
public: __property EndPoint* get_LocalEndPoint();
public function get LocalEndPoint() : EndPoint;

Property Value

The EndPoint that the Socket is using for communications.


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.


The LocalEndPoint property gets an EndPoint containing the local IP address and port number to which your Socket is bound. You must cast this EndPoint to an IPEndPoint before retrieving any information. You can then call the IPEndPoint.Address method to retrieve the local IPAddress, and the IPEndPoint.Port method to retrieve the local port number.

The LocalEndPoint property is usually set after you make a call to the Bind method. If you allow the system to assign your socket's local IP address and port number, the LocalEndPoint property will be set after the first I/O operation. For connection-oriented protocols, the first I/O operation would be a call to the Connect or Accept method. For connectionless protocols, the first I/O operation would be any of the send or receive calls.

Note   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.


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

[Visual Basic] 

' 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())

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


// 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.


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 | IPEndPoint | Bind