Socket.Accept Socket.Accept Socket.Accept Socket.Accept Method

定义

为新建连接创建新的 SocketCreates a new Socket for a newly created connection.

public:
 System::Net::Sockets::Socket ^ Accept();
public System.Net.Sockets.Socket Accept ();
member this.Accept : unit -> System.Net.Sockets.Socket
Public Function Accept () As Socket

返回

新建连接的 SocketA Socket for a newly created connection.

异常

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

正在接受的套接字不会侦听连接。The accepting socket is not listening for connections. 在调用 Bind(EndPoint) 之前必须调用 Listen(Int32)Accept()You must call Bind(EndPoint) and Listen(Int32) before calling Accept().

示例

下面的代码示例接受简单Socket连接。The following code example accepts a simple Socket connection.

protected:
   void AcceptMethod( Socket^ listeningSocket )
   {
      Socket^ mySocket = listeningSocket->Accept();
   }
protected void AcceptMethod(Socket listeningSocket)
{
    Socket mySocket = listeningSocket.Accept();
}
   Protected Sub AcceptMethod(listeningSocket As Socket)
       

Dim mySocket As Socket = listeningSocket.Accept()
       
   End Sub

注解

Accept 以同步方式从侦听套接字,在连接请求队列中提取第一个挂起的连接请求,然后创建并返回一个新SocketAccept synchronously extracts the first pending connection request from the connection request queue of the listening socket, and then creates and returns a new Socket. 不能使用此返回Socket为接受任何其他连接的连接队列。You cannot use this returned Socket to accept any additional connections from the connection queue. 但是,可以调用RemoteEndPoint方法所返回的Socket来标识远程主机的网络地址和端口号。However, you can call the RemoteEndPoint method of the returned Socket to identify the remote host's network address and port number.

在阻止模式下,Accept阻止,直到传入的连接尝试排入队列。In blocking mode, Accept blocks until an incoming connection attempt is queued. 连接被接受后,原始Socket继续队列传入的连接请求直到您关闭它。Once a connection is accepted, the original Socket continues queuing incoming connection requests until you close it.

如果调用此方法使用非阻塞Socket,并请求排队,没有连接Accept引发SocketExceptionIf you call this method using a non-blocking Socket, and no connection requests are queued, Accept throws a SocketException. 如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, use the SocketException.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.

备注

然后再调用Accept方法,必须首先调用Listen方法来侦听并对传入连接请求进行排队。Before calling the Accept method, you must first call the Listen method to listen for and queue incoming connection requests.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

适用于

另请参阅