TcpListener.Pending Metoda

Definicja

Określa, czy istnieją oczekujące żądania połączenia.

public:
 bool Pending();
public bool Pending ();
member this.Pending : unit -> bool
Public Function Pending () As Boolean

Zwraca

true jeśli połączenia są oczekujące; w przeciwnym razie , false.

Wyjątki

Odbiornik nie został uruchomiony z wywołaniem metody Start().

Przykłady

Poniższy przykład kodu sprawdza metodę Pending . Jeśli żądanie połączenia oczekuje na zaakceptowanie, zostanie wykonane wywołanie AcceptTcpClient metody .

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 );
const int portNumber = 13;

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

        //Close the tcpListener and tcpClient instances
        tcpClient.Close();
    }

    tcpListener.Stop();
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}

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

Uwagi

Ta metoda nieblokująca określa, czy istnieją oczekujące żądania połączenia. AcceptSocket Ponieważ metody i AcceptTcpClient blokują wykonywanie, dopóki Start metoda nie utworzy kolejki przychodzącego żądania połączenia, można użyć metody w celu określenia, Pending czy połączenia są dostępne przed podjęciem próby ich zaakceptowania.

Dotyczy

Zobacz też