保留中の接続要求があるかどうかを確認します。
Public Function Pending() As Boolean
[C#]
public bool Pending();
[C++]
public: bool Pending();
[JScript]
public function Pending() : Boolean;
戻り値
接続が保留中の場合は true 。それ以外の場合は false 。
例外
例外の種類 | 条件 |
---|---|
InvalidOperationException | リスナが、 Start への呼び出しで開始されていません。 |
解説
この非ブロッキング メソッドは、保留になっている接続要求がないかどうかを確認します。 AcceptSocket メソッドおよび AcceptTcpClient メソッドは、 Start が受信接続要求をキューに置くまで実行をブロックするため、 Pending メソッドは、接続要求を受け取る前に接続が使用可能かどうかを確認するときにも使用できます。
使用例
[Visual Basic, C#, C++] Pending メソッドの例を次に示します。受け入れを待機している接続要求があると、 AcceptTcpClient が呼び出されます。
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())
[C#]
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());
[C++]
try {
// Use the Pending method to poll the underlying socket instance for client connection requests.
if (!tcpListener->Pending()) {
Console::WriteLine(S"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(S"I am listening for connections on {0} on port number {1}",
IPAddress::Parse(__try_cast<IPEndPoint*>(tcpListener->LocalEndpoint)->Address->ToString()),
__box(__try_cast<IPEndPoint*>(tcpListener->LocalEndpoint)->Port));
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
TcpListener クラス | TcpListener メンバ | System.Net.Sockets 名前空間 | Start | AcceptSocket | AcceptTcpClient