Export (0) Print
Expand All

TcpListener.LocalEndpoint Property

Gets the underlying EndPoint of the current TcpListener.

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

Property Value

The EndPoint to which the Socket is bound.

Remarks

You can use LocalEndpoint to identify the local network interface and port number being used to listen for incoming client connection requests. You must first cast this EndPoint to an IPEndPoint. You can then call the IPEndPoint.Address method to retrieve the local IP address, and the IPEndPoint.Port method to retreive the local port number.

Example

[Visual Basic, C#, C++] The following example displays the local IP address and port number on which the TcpListener is listening for incoming connection requests.

[Visual Basic] 

Try

     Dim ipAddress As IPAddress = Dns.Resolve("localhost").AddressList(0)
  Dim tcpListener As New TcpListener(ipAddress, portNumber)
  tcpListener.Start()
  
   ' Use the Pending method to poll the underlying socket instance for client connection requests.
   If Not tcpListener.Pending() Then
      
      Console.WriteLine("Sorry, no connection requests have arrived")
   
   Else
      
      'Accept the pending client connection and return a TcpClient object initialized for communication.
      Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
      ' Using the RemoteEndPoint property.
      Console.Write("I am listening for connections on ")
      Console.Writeline(IPAddress.Parse(CType(tcpListener.LocalEndpoint, IPEndPoint).Address.ToString())) 
      Console.Write("on port number ")
      Console.Write(CType(tcpListener.LocalEndpoint, IPEndPoint).Port.ToString())
      

[C#] 

      try{
// Use the Pending method to poll the underlying socket instance for client connection requests.
    IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
 TcpListener tcpListener =  new TcpListener(ipAddress, portNumber); 
 tcpListener.Start();
 
  if (!tcpListener.Pending()) {

  Console.WriteLine("Sorry, no connection requests have arrived");
  
             }
             else{

     //Accept the pending client connection and return a TcpClient object initialized for communication.
     TcpClient tcpClient = tcpListener.AcceptTcpClient();
     // Using the RemoteEndPoint property.
     Console.WriteLine("I am listening for connections on " + 
                                 IPAddress.Parse(((IPEndPoint)tcpListener.LocalEndpoint).Address.ToString()) +
                                    "on port number " + ((IPEndPoint)tcpListener.LocalEndpoint).Port.ToString());


[C++] 
try {
    // Use the Pending method to poll the underlying socket instance for client connection requests.
    if (!tcpListener->Pending()) {
        Console::WriteLine(S"Sorry, no connection requests have arrived");
    } else {
        //Accept the pending client connection and return a TcpClient Object* initialized for communication.
        TcpClient* tcpClient = tcpListener->AcceptTcpClient();
        // Using the RemoteEndPoint property.
        Console::WriteLine(S"I am listening for connections on {0} on port number {1}", 
            IPAddress::Parse(__try_cast<IPEndPoint*>(tcpListener->LocalEndpoint)->Address->ToString()),
            __box(__try_cast<IPEndPoint*>(tcpListener->LocalEndpoint)->Port));

[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

See Also

TcpListener Class | TcpListener Members | System.Net.Sockets Namespace | Listen | EndPoint

Show:
© 2014 Microsoft