Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TcpListener.Start Method (Int32)

Note: This method is new in the .NET Framework version 2.0.

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

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

public void Start (
	int backlog
)
public void Start (
	int backlog
)
public function Start (
	backlog : int
)

Parameters

backlog

The maximum length of the pending connections queue.

Exception typeCondition

SocketException

An error occurred while accessing the socket. See the Remarks section for more information.

ArgumentOutOfRangeException

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

InvalidOperationException

The underlying Socket is null.

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.

NoteNote

Use the SocketException.ErrorCode property to obtain the specific error code and refer to the Windows Sockets version 2 API error code documentation in the MSDN library at http://msdn.microsoft.com/library/ for a detailed description of the error.

NoteNote

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

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");
}

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft