次の方法で共有


TcpClient.Client プロパティ

基になる Socket を取得または設定します。

Protected Property Client As Socket
[C#]
protected Socket Client {get; set;}
[C++]
protected: __property Socket* get_Client();protected: __property void set_Client(Socket*);
[JScript]
protected function get Client() : Socket;protected function set Client(Socket);

プロパティ値

基になるネットワーク Socket

解説

TcpClient はネットワーク上でデータを送受信するための Socket を作成します。 TcpClient の派生クラスはこのプロパティを使用してこの Socket を取得または設定します。 TcpClient が提供する以上のアクセスが必要な場合は、 Client が返した基になる Socket を使用します。 Client を使用して、基になる Socket を既存の Socket に設定することもできます。この方法は、既存の Socket を使用して TcpClient の単純さを利用する場合に役立ちます。

使用例

プロテクト プロパティ Client を使用している派生クラスの例を次に示します。この例では、 MyTcpClientDerivedClass が基になる Socket を取得してブロードキャストを有効にしています。

 
' 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 名前空間 | Socket