Export (0) Print
Expand All

TcpListener.Pending Method

Determines if there are pending connection requests.

[Visual Basic]
Public Function Pending() As Boolean
[C#]
public bool Pending();
[C++]
public: bool Pending();
[JScript]
public function Pending() : Boolean;

Return Value

true if connections are pending; otherwise, false.

Exceptions

Exception Type Condition
InvalidOperationException The listener has not been started with a call to Start.

Remarks

This non-blocking method determines if there are any pending connection requests. Because the AcceptSocket and AcceptTcpClient methods block execution until Start has queued an incoming connection request, the Pending method can be used to determine if connections are available before attempting to accept them.

Example

[Visual Basic, C#, C++] The following example checks the Pending method. If a connection request is waiting to be accepted, then then a call to AcceptTcpClient is made.

[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 | Start | AcceptSocket | AcceptTcpClient

Show:
© 2014 Microsoft