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从侦听套接字的连接请求队列中同步提取第一个挂起的连接请求, 然后创建并返回一个新Socket的。Accept 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.

适用于

另请参阅