Share via


TcpListener.Pending 메서드

정의

보류 중인 연결 요청이 있는지 여부를 확인합니다.

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

반환

연결이 보류 중이면 true이고, 그렇지 않으면 false입니다.

예외

Start()의 호출과 함께 수신기가 시작하지 않은 경우

예제

다음 코드 예제는 메서드를 확인합니다 Pending . 연결 요청이 수락되기를 기다리는 경우 메서드를 AcceptTcpClient 호출합니다.

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

설명

이 비 차단 메서드는 보류 중인 연결 요청이 있는지 여부를 결정합니다. AcceptSocketAcceptTcpClient 메서드는 메서드가 들어오는 연결 요청을 Pending 큐에 대기할 때까지 Start 실행을 차단하기 때문에 메서드를 사용하여 연결을 수락하기 전에 연결을 사용할 수 있는지 확인할 수 있습니다.

적용 대상

추가 정보