Freigeben über


TcpListener.Pending-Methode

Bestimmt, ob ausstehende Verbindungsanforderungen vorliegen.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

Syntax

'Declaration
Public Function Pending As Boolean
'Usage
Dim instance As TcpListener
Dim returnValue As Boolean

returnValue = instance.Pending
public bool Pending ()
public:
bool Pending ()
public boolean Pending ()
public function Pending () : boolean

Rückgabewert

true, wenn Verbindungen ausstehen, andernfalls false.

Ausnahmen

Ausnahmetyp Bedingung

InvalidOperationException

Der Listener wurde nicht mit einem Aufruf von Start gestartet.

Hinweise

Diese nicht blockierende Methode bestimmt, ob ausstehende Verbindungsanforderungen vorhanden sind. Da die AcceptSocket-Methode und die AcceptTcpClient-Methode die Ausführung blockieren, bis die Start-Methode eine eingehende Verbindung in die Warteschlange gestellt hat, kann mit der Pending-Methode die Verfügbarkeit von Verbindungen bestimmt werden, bevor deren Annahme versucht wird.

Beispiel

Im folgenden Codebeispiel wird die Pending-Methode überprüft. Wenn eine Verbindungsanforderung auf die Annahme wartet, wird AcceptTcpClient aufgerufen.

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())
      
        
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());
try
{
   // Use the Pending method to poll the underlying socket instance for client connection requests.
   TcpListener^ tcpListener = gcnew TcpListener( 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 {0} on port number {1}",
         IPAddress::Parse( ( (IPEndPoint^)(tcpListener->LocalEndpoint) )->Address->ToString() ),
         ( (IPEndPoint^)(tcpListener->LocalEndpoint) )->Port );
try {
    // Use the Pending method to poll the underlying socket instance
    // for client connection requests.
    IPAddress ipAddress = Dns.Resolve("localhost").get_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.get_LocalEndpoint())).get_Address().ToString())
            + "on port number " + ((Int32)((IPEndPoint)
            (tcpListener.get_LocalEndpoint())).get_Port()).ToString());

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

TcpListener-Klasse
TcpListener-Member
System.Net.Sockets-Namespace
Start
AcceptSocket
AcceptTcpClient