Socket.BeginAccept Method (AsyncCallback, Object)
Begins an asynchronous operation to accept an incoming connection attempt.
Assembly: System (in System.dll)
<HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading := True)> Public Function BeginAccept ( callback As AsyncCallback, state As Object ) As IAsyncResult
An object that contains state information for this request.
The Socket object has been closed.
Windows NT is required for this method.
receiveSize is less than 0.
An error occurred when attempting to access the socket. See the Remarks section for more information.
Connection-oriented protocols can use the Listen method to listen for and queue incoming connection requests.method to asynchronously process incoming connection attempts. Accepting connections asynchronously gives you the ability to send and receive data within a separate execution thread. Before calling the method, you must call the
You must create a callback method that implements the AsyncCallback delegate and pass its name to the method. To do this, at the very minimum, you must pass the listening Socket object to through the state parameter. If your callback needs more information, you can create a small class to hold the Socket and the other required information. Pass an instance of this class to the method through the state parameter.
Your callback method should invoke the EndAccept method. When your application calls , the system usually uses a separate thread to execute the specified callback method and blocks on EndAccept until a pending connection is retrieved. EndAccept will return a new Socket object that you can use to send and receive data with the remote host. You cannot use this returned Socket to accept any additional connections from the connection queue. If you want the original thread to block after you call the method, use WaitHandle.WaitOne. Call the Set method on a ManualResetEvent in the callback method when you want the original thread to continue executing.
The system may also use the calling thread to invoke the callback method. In this case, the CompletedSynchronously property on the returned IAsyncResult will be set to indicate that the method completed synchronously.
For additional information on writing callback methods see Marshaling a Delegate as a Callback Method.
To cancel a pending call to the BeginAccept method, close the Socket. When theClosemethod is called while an asynchronous operation is in progress, the callback provided to the BeginAccept method is called. A subsequent call to the EndAcceptmethod will throw an ObjectDisposedException to indicate that the operation has been cancelled.
If you receive a SocketException, use the SocketException.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 in MSDN for a detailed description of the error.
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.
The execution context (the security context, the impersonated user, and the calling context) is cached for the asynchronous Socket methods. After the first use of a particular context (a specific asynchronous Socket method, a specific Socket instance, and a specific callback), subsequent uses of that context will see a performance improvement.
The following code example attempts to receive an incoming connection asynchronously.
Dim lipa As IPHostEntry = Dns.Resolve("host.contoso.com") Dim lep As New IPEndPoint(lipa.AddressList(0), 11000) Dim s As New Socket(lep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp) Try s.Bind(lep) s.Listen(1000) While True allDone.Reset() Console.WriteLine("Waiting for a connection...") s.BeginAccept(New AsyncCallback(AddressOf Async_Send_Receive.Listen_Callback), s) allDone.WaitOne() End While Catch e As Exception Console.WriteLine(e.ToString()) End Try End Sub 'Listen
Available since 1.1