Export (0) Print
Expand All

Socket.Listen Method

Places a Socket in a listening state.

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

public:
void Listen (
	int backlog
)
public void Listen (
	int backlog
)
public function Listen (
	backlog : int
)
Not applicable.

Parameters

backlog

The maximum length of the pending connections queue.

Exception typeCondition

SocketException

An error occurred when attempting to access the socket. See the Remarks section for more information.

ObjectDisposedException

The Socket has been closed.

Listen causes a connection-oriented Socket to listen for incoming connection attempts. The backlog parameter specifies the number of incoming connections that can be queued for acceptance. To determine the maximum number of connections you can specify, retrieve the MaxConnections value. Listen does not block.

If you receive a SocketException, use the ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. Use Accept or BeginAccept to accept a connection from the queue.

NoteNote:

You must call the Bind method before calling Listen, or Listen will throw a SocketException.

NoteNote:

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

NoteNote:

The backlog parameter is limited to different values depending on the Operating System. You may specify a higher value, but the backlog will be limited based on the Operating System.

The following code example uses Socket to listen for incoming connections.

// create the socket
Socket^ listenSocket = gcnew Socket( AddressFamily::InterNetwork,
   SocketType::Stream,
   ProtocolType::Tcp );

// bind the listening socket to the port
IPAddress^ hostIP = ( Dns::Resolve( IPAddress::Any->ToString() ) )->AddressList[ 0 ];
IPEndPoint^ ep = gcnew IPEndPoint( hostIP,port );
listenSocket->Bind( ep );

// start listening
listenSocket->Listen( backlog );

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft