Export (0) Print
Expand All

Socket.Listen Method

Places a Socket in a listening state.

[Visual Basic]
Public Sub Listen( _
   ByVal backlog As Integer _
)
[C#]
public void Listen(
 int backlog
);
[C++]
public: void Listen(
 int backlog
);
[JScript]
public function Listen(
   backlog : int
);

Parameters

backlog
The maximum length of the pending connections queue.

Exceptions

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.

Remarks

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.

Example

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

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

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

[C++] 
// Allows a queue of 10 connections.
aSocket->Listen(10);
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.

Requirements

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

Show:
© 2014 Microsoft