TcpListener.Server Property

Definition

Gets the underlying network Socket.

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

Property Value

The underlying Socket.

Examples

The following code example demonstrates the use of the Server property. The underlying Socket is retrieved and the LingerSocket option is configured to time out after 10 seconds if data still remains in the network buffer after the connection is closed.

C#
public static void listenerOption(string host, int port)
{
    IPHostEntry server = Dns.Resolve(host);
    IPAddress ipAddress = server.AddressList[0];

    Console.WriteLine("listening on {0}, port {1}", ipAddress, port);
    TcpListener listener = new TcpListener(ipAddress, port);
    Socket listenerSocket = listener.Server;		

    LingerOption lingerOption = new LingerOption(true, 10);
    listenerSocket.SetSocketOption(SocketOptionLevel.Socket, 
                      SocketOptionName.Linger, 
                      lingerOption);

    // start listening and process connections here.
    listener.Start();
}

Remarks

TcpListener creates a Socket to listen for incoming client connection requests. Classes deriving from TcpListener can use this property to get this Socket. Use the underlying Socket returned by the Server property if you require access beyond that which TcpListener provides.

Note

The Server property only returns the Socket used to listen for incoming client connection requests. Use the AcceptSocket method to accept a pending connection request and obtain a Socket for sending and receiving data. You can also use the AcceptTcpClient method to accept a pending connection request and obtain a TcpClient for sending and receiving data.

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