TcpListener.Pending メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
保留中の接続要求があるかどうかを確認します。
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())
注釈
この非ブロッキング メソッドは、保留中の接続要求があるかどうかを判断します。 AcceptSocketメソッドと AcceptTcpClient メソッドは、メソッドが受信接続要求をStartキューに入れるまで実行をブロックするため、 メソッドをPending使用して、接続を受け入れる前に接続が使用可能かどうかを判断できます。
適用対象
こちらもご覧ください
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示