TcpClient.Client Property

Definition

Gets or sets the underlying Socket.

C#
public System.Net.Sockets.Socket Client { get; set; }
C#
protected System.Net.Sockets.Socket Client { get; set; }

Property Value

The underlying network Socket.

Examples

The following code example demonstrates the use of the Client property. In this example, the receive buffer size of the underlying Socket is changed.

C#
TcpClient client = new TcpClient();
Socket s = client.Client;

if (!s.Connected)
{
    s.SetSocketOption(SocketOptionLevel.Socket, 
                 SocketOptionName.ReceiveBuffer, 16384);
    Console.WriteLine(
        "client is not connected, ReceiveBuffer set\n");
}
else
{
    Console.WriteLine("client is connected");
}

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.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

See also