संपादित करें

इसके माध्यम से साझा किया गया


Socket.Listen Method

Definition

Overloads

Listen()

Places a Socket in a listening state.

Listen(Int32)

Places a Socket in a listening state.

Listen()

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Places a Socket in a listening state.

public:
 void Listen();
public void Listen ();
member this.Listen : unit -> unit
Public Sub Listen ()

Remarks

The maximum length of the pending connections queue is determined automatically.

Applies to

Listen(Int32)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Places a Socket in a listening state.

public:
 void Listen(int backlog);
public void Listen (int backlog);
member this.Listen : int -> unit
Public Sub Listen (backlog As Integer)

Parameters

backlog
Int32

The maximum length of the pending connections queue.

Exceptions

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

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 );
    // create the socket
    Socket listenSocket = new 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 = new IPEndPoint(hostIP, port);
    listenSocket.Bind(ep);

    // start listening
    listenSocket.Listen(backlog);
   ' create the socket
   Dim listenSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
   
   ' bind the listening socket to the port
   Dim hostIP As IPAddress = Dns.Resolve(IPAddress.Any.ToString()).AddressList(0)
   Dim ep As New IPEndPoint(hostIP, port)
   listenSocket.Bind(ep)
   
   ' start listening
   listenSocket.Listen(backlog)
End Sub

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 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 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.

Note

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

Note

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.

See also

Applies to