Export (0) Print
Expand All

TcpClient.Client Property

Gets or sets the underlying Socket.

[Visual Basic]
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);

Property Value

The underlying network Socket.

Remarks

TcpClient creates a Socket to send and receive data over a network. Classes deriving from TcpClient can use this property to get or set this Socket. Use the underlying Socket returned from Client if you require access beyond that which TcpClient provides. You can also use Client to set the underlying Socket to an existing Socket. This might be useful if you want to take advantage of the simplicity of TcpClient using a pre-existing Socket.

Example

The following example demonstrates a derived class using the protected property Client. In this example, MyTcpClientDerivedClass obtains the underlying Socket to enable broadcasting.

[Visual Basic] 
' 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);

}

}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TcpClient Class | TcpClient Members | System.Net.Sockets Namespace | Socket

Show:
© 2015 Microsoft