TcpListener.Start Method

Definition

Starts listening for incoming connection requests.

Overloads

Start()

Starts listening for incoming connection requests.

Start(Int32)

Starts listening for incoming connection requests with a maximum number of pending connection.

Start()

Source:
TCPListener.cs
Source:
TCPListener.cs
Source:
TCPListener.cs

Starts listening for incoming connection requests.

C#
public void Start();

Exceptions

Use the ErrorCode property to obtain the specific error code. When you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Examples

The following code example demonstrates how Start is used to listen for incoming client connection attempts.

C#
public static void DoStart(TcpListener t, int backlog)
{
    // Start listening for client connections with the
    // specified backlog.
    t.Start(backlog);
    Console.WriteLine("started listening");
}

Remarks

The Start method initializes the underlying Socket, binds it to a local endpoint, and listens for incoming connection attempts. If a connection request is received, the Start method will queue the request and continue listening for additional requests until you call the Stop method. If TcpListener receives a connection request after it has already queued the maximum number of connections, it will throw a SocketException on the client.

To remove a connection from the incoming connection queue, use either the AcceptTcpClient method or the AcceptSocket method. The AcceptTcpClient method will remove a connection from the queue and return a TcpClient that you can use to send and receive data. The AcceptSocket method will return a Socket that you can use to do the same. If your application only requires synchronous I/O, use AcceptTcpClient. For more detailed behavioral control, use AcceptSocket. Both of these methods block until a connection request is available in the queue.

Use the Stop method to close the TcpListener and stop listening. You are responsible for closing your accepted connections separately.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

See also

Applies to

.NET 9 and other versions
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
.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

Start(Int32)

Source:
TCPListener.cs
Source:
TCPListener.cs
Source:
TCPListener.cs

Starts listening for incoming connection requests with a maximum number of pending connection.

C#
public void Start(int backlog);

Parameters

backlog
Int32

The maximum length of the pending connections queue.

Exceptions

An error occurred while accessing the socket.

The backlog parameter is less than zero or exceeds the maximum number of permitted connections.

The underlying Socket is null.

Examples

The following code example demonstrates how Start is used to listen for incoming client connection attempts.

C#
public static void DoStart(TcpListener t, int backlog)
{
    // Start listening for client connections with the
    // specified backlog.
    t.Start(backlog);
    Console.WriteLine("started listening");
}

Remarks

The Start method initializes the underlying Socket, binds it to a local endpoint, and listens for incoming connection attempts. If a connection request is received, Start will queue the request and continue listening for additional requests until you call the Stop method. If TcpListener receives a connection request after it has already queued the maximum number of connections it will throw a SocketException on the client.

To remove a connection from the incoming connection queue, use either the AcceptTcpClient method or the AcceptSocket method. The AcceptTcpClient method will remove a connection from the queue and return a TcpClient that you can use to send and receive data. The AcceptSocket method will return a Socket that you can use to do the same. If your application only requires synchronous I/O, use the AcceptTcpClient. For more detailed behavioral control, use AcceptSocket method. Both of these methods block until a connection request is available in the queue.

Use the Stop method to close the TcpListener and stop listening. You are responsible for closing your accepted connections separately.

Note

Use the SocketException.ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

See also

Applies to

.NET 9 and other versions
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
.NET Framework 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