Export (0) Print
Expand All

TcpListener.Start Method

Starts listening for incoming connection requests.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public void Start()

ExceptionCondition
SocketException

Use the SocketException.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 in MSDN for a detailed description of the error.

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.

NoteNote

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

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

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft