次の方法で共有


TcpListener.Active プロパティ

TcpListener がクライアント接続をアクティブに待機しているかどうかを示す値を取得します。

Protected ReadOnly Property Active As Boolean
[C#]
protected bool Active {get;}
[C++]
protected: __property bool get_Active();
[JScript]
protected function get Active() : Boolean;

プロパティ値

TcpListener がアクティブに待機している場合は true 。それ以外の場合は false

解説

TcpListener の派生クラスはこのプロパティを使用して、現在 Socket が受信接続の試行を待機しているかどうかを判断します。 Active プロパティを使用すると、 Start の冗長な試行を避けることができます。

使用例

[Visual Basic, C#, C++] 次に示すのは、 TcpListener を継承し、保護されている Active メソッドを実装するクラスの例です。この例では、 Socket オプションの設定前に TcpListener による接続の処理が終了しているかどうかがチェックされます。

 
Imports System
Imports System.Net
Imports System.Net.Sockets

Public Class MyTcpListenerDerivedClass
   Inherits TcpListener
   
   Public Sub New(address As IPAddress, port As Integer)
      MyBase.New(address, port)
   End Sub 'New
   
   Public Sub UsingProtectedMethods()
      ' The protected property 'Active' determines whether a connection is established.
      If Me.Active Then
         Dim s As Socket = Me.Server
         'Limit socket linger time to 10 seconds.
         Dim lingerOption As New LingerOption(True, 10)
         s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption)
      End If
   End Sub 
End Class 


[C#] 
using System;
using System.Net; 
using System.Net.Sockets;

public class MyTcpListenerDerivedClass : TcpListener
{
    public MyTcpListenerDerivedClass(IPAddress address, int port) : base(address, port){}
    public  void  UsingProtectedMethods()
    {
        // The protected property 'Active' determines whether a connection is established.
        if (this.Active)
        {
            Socket s = this.Server;
            // Limit socket linger time to 10 seconds.
            LingerOption lingerOption = new LingerOption(true, 10);
            s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);
        }
    }
}


[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Net; 
using namespace System::Net::Sockets;

__gc class MyTcpListenerDerivedClass : public TcpListener {
public:
    MyTcpListenerDerivedClass(int port) : TcpListener(port) {}

    void UsingProtectedMethods() {
        // The protected property 'Active' determines whether a connection is established.
        if (this->Active) 
        {
            Socket* s = this->Server;
            // Limit socket linger time to 10 seconds.
            LingerOption* lingerOption = new LingerOption(true, 10);
            s->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Linger, lingerOption);
        }
    }
};

[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 ファミリ

参照

TcpListener クラス | TcpListener メンバ | System.Net.Sockets 名前空間 | Start