This documentation is archived and is not being maintained.

Socket.Listen Method

Places a Socket in a listening state.

[Visual Basic]
Public Sub Listen( _
   ByVal backlog As Integer _
public void Listen(
 int backlog
public: void Listen(
 int backlog
public function Listen(
   backlog : int


The maximum length of the pending connections queue.


Exception Type Condition
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 ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error. Use Accept or BeginAccept to accept a connection from the queue.

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


[Visual Basic, C#, C++] The following example uses Socket to listen for incoming connections.

[Visual Basic] 
' Allows a queue of 10 connections.
If Not aSocket.Connected Then
    Console.WriteLine("Winsock error: " _
       + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()))
End If

// Allows a queue of 10 connections.
if (!aSocket.Connected) {
   Console.WriteLine("Winsock error: "
      + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error()));

// Allows a queue of 10 connections.
if (!aSocket->Connected) {
   Console::WriteLine(S"Winsock error: {0}", Convert::ToString(System::Runtime::InteropServices::Marshal::GetLastWin32Error()));

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Socket Class | Socket Members | System.Net.Sockets Namespace | MaxConnections | Accept | Bind