Socket.Listen(Int32) Socket.Listen(Int32) Socket.Listen(Int32) Socket.Listen(Int32) Method

定义

Socket 置于侦听状态。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)

参数

backlog
Int32 Int32 Int32 Int32

挂起连接队列的最大长度。The maximum length of the pending connections queue.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例使用Socket侦听传入连接。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 'CreateAndListen

注解

Listen导致面向Socket连接的侦听传入的连接尝试。Listen causes a connection-oriented Socket to listen for incoming connection attempts. backlog参数指定可以排队等待接受的传入连接的数量。The backlog parameter specifies the number of incoming connections that can be queued for acceptance. 若要确定可以指定的最大连接数,请检索MaxConnections值。To determine the maximum number of connections you can specify, retrieve the MaxConnections value. Listen不会阻止。Listen does not block.

如果收到SocketException,请ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码文档,以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error. 使用AcceptBeginAccept接受队列中的连接。Use Accept or BeginAccept to accept a connection from the queue.

备注

必须在调用Listen之前Bind调用方法Listen ,否则将引发。 SocketExceptionYou must call the Bind method before calling Listen, or Listen will throw a SocketException.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

备注

积压工作(backlog)参数限制为不同的值,具体取决于操作系统。The backlog parameter is limited to different values depending on the Operating System. 您可以指定较大的值,但积压工作(backlog)将基于操作系统进行限制。You may specify a higher value, but the backlog will be limited based on the Operating System.

适用于

另请参阅