次の方法で共有


TcpClient.Active プロパティ

接続されたかどうかを示す値を取得または設定します。

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

プロパティ値

接続されている場合は true 。それ以外の場合は false

解説

TcpClient の派生クラスはこのプロパティを使用して、接続が成功しているかどうかを判断します。ただし、 TcpClient の発信接続状態は監視しません。リモート ホストが接続を終了した場合、 Active は更新されません。 TcpClient から派生させている場合、および接続状態に注意が必要な場合は、 Client メソッドによって返される SocketConnected プロパティを使用します。

使用例

プロテクト プロパティ Active を使用している派生クラスの例を次に示します。この例では、基になる Socket を取得する前に接続がアクティブになっていることを MyTcpClientDerivedClass が検証しています。

 
' This derived class demonstrates the use of three protected properties belonging to the TcpClient Class.
Public Class MyTcpClientDerivedClass
   Inherits TcpClient
   
   Public Sub New()
   End Sub 'New
   
   Public Sub UsingProtectedMethods()
      
      'Uses the protected 'Active' property  belonging to the TcpClient base class
      'to determine if a connection is established.
      If Me.Active Then
         ' Calls the protected 'Client' property belonging to the TcpClient base class.
         Dim s As Socket = Me.Client
         'Uses the Socket returned by Client to set an option that is not available using TcpClient.
         s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1)
      End If
      'To free all resources, calls protected virtual method Dispose belonging to the TcpClient base class.
      Me.Dispose(True)
      GC.SuppressFinalize(Me)
   End Sub 'UsingProtectedMethods 
End Class 'MyTcpClientDerivedClass


[C#] 
// This derived class demonstrates the use of three protected methods belonging to the TcpClient class
public class MyTcpClientDerivedClass : TcpClient{

// Constructor for the derived class.
public MyTcpClientDerivedClass() : base(){
}
public void UsingProtectedMethods(){

  // Uses the protected 'Active' property belonging to the TcpClient base class 
  // to determine if a connection is established. 
  if (this.Active){
      // Calls the protected 'Client' property belonging to the TcpClient base class.
      Socket s = this.Client;
      // Uses the Socket returned by Client to set an option that is not available using TcpClient.
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  }
  // To free all resources, calls the protected virtual method Dispose belonging to the TcpClient base class.
 this.Dispose(true);
 GC.SuppressFinalize(this);

}

}

[C++] 
// This derived class demonstrates the use of three protected methods belonging to the TcpClient class
__gc public class MyTcpClientDerivedClass : public TcpClient{

    // Constructor for the derived class.
public:
    void UsingProtectedMethods() {

        // Uses the protected 'Active' property belonging to the TcpClient base class 
        // to determine if a connection is established. 
        if (this->Active) {
            // Calls the protected 'Client' property belonging to the TcpClient base class.
            Socket* s = this->Client;
            // Uses the Socket returned by Client to set an option that is not available using TcpClient.
            s->SetSocketOption(SocketOptionLevel::Socket, SocketOptionName::Broadcast, 1);
        }
        // To free all resources, calls the protected virtual method Dispose belonging to the TcpClient base class.
        this->Dispose(true);
        GC::SuppressFinalize(this);
    }
};

[JScript] 
// The purpose of this class is to demonstrate the use of three protected methods belonging to the TcpClient Class
public class MyTcpClientDerivedClass extends TcpClient{

// constructor
public function MyTcpClientDerivedClass(){
     super();
}

public function importProtectedMethods(){

  //Use the protected property 'Active' belonging to the TcpClient base class. 
  //This determines if connection is established.
  if (this.Active){
      //Calling the protected property 'client' belonging to the TcpClient base class.
      var s : Socket = this.Client;
      //Suppose you want to set an option that is not available by just import TcpClient object.
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  }
  //Call protected virtual method Dispose belonging to the TcpClient base class to free all resources.
 this.Dispose(true);
 GC.SuppressFinalize(this);

}

}

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

TcpClient クラス | TcpClient メンバ | System.Net.Sockets 名前空間 | Connected | Connect