TcpListener.Pending TcpListener.Pending TcpListener.Pending TcpListener.Pending Method

Définition

Détermine la présence éventuelle de demandes de connexion en attente.Determines if there are pending connection requests.

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

Retours

true si des connexions sont en attente ; sinon, false.true if connections are pending; otherwise, false.

Exceptions

L'écouteur n'a pas démarré avec un appel à la méthode Start().The listener has not been started with a call to Start().

Exemples

L’exemple de code suivant vérifie Pending la méthode.The following code example checks the Pending method. Si une demande de connexion attend d’être acceptée, un appel à la AcceptTcpClient méthode est effectué.If a connection request is waiting to be accepted, then a call to the AcceptTcpClient method is made.

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").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

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

Remarques

Cette méthode non bloquante détermine s’il existe des demandes de connexion en attente.This non-blocking method determines if there are any pending connection requests. Étant donné que AcceptTcpClient les méthodes Start Pending et bloquent l’exécution jusqu’à ce que la méthode ait mis en file d’attente une demande de connexion entrante, la méthode peut être utilisée pour déterminer si les connexions sont disponibles avant de tenter de les accepter. AcceptSocketBecause the AcceptSocket and AcceptTcpClient methods block execution until the Start method has queued an incoming connection request, the Pending method can be used to determine if connections are available before attempting to accept them.

S’applique à

Voir aussi