Export (0) Print
Expand All

Socket.EndAccept Method (Byte(), Int32, IAsyncResult)

Asynchronously accepts an incoming connection attempt and creates a new Socket object to handle remote host communication. This method returns a buffer that contains the initial data and the number of bytes transferred.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

Public Function EndAccept ( _
	<OutAttribute> ByRef buffer As Byte(), _
	<OutAttribute> ByRef bytesTransferred As Integer, _
	asyncResult As IAsyncResult _
) As Socket


Type: System.Byte()

An array of type Byte that contains the bytes transferred.

Type: System.Int32

The number of bytes transferred.

Type: System.IAsyncResult

An IAsyncResult object that stores state information for this asynchronous operation as well as any user defined data.

Return Value

Type: System.Net.Sockets.Socket
A Socket object to handle communication with the remote host.


Windows NT is required for this method.


The Socket object has been closed.


asyncResult is empty.


asyncResult was not created by a call to BeginAccept.


EndAccept method was previously called.


An error occurred when attempting to access the Socket. See the Remarks section for more information.

EndAccept completes a call to BeginAccept. Before calling BeginAccept, you need to create a callback method that is invoked by the AsyncCallback delegate. This callback method executes in a separate thread, and is called by the system after the BeginAccept method returns. It must accept the asyncResult parameter returned from the BeginAccept method.

Within the callback method, call the AsyncState method of the asyncResult parameter to obtain the Socket on which the connection attempt is being made. After obtaining the Socket, you can call the EndAccept method to successfully complete the connection attempt. The buffer parameter of this overload contains the data that was received in the call to BeginAccept and the bytesTransferred parameter contains the number of bytes that were transferred in the call.

The EndAccept method blocks until a connection is pending in the incoming connection queue. The EndAccept method accepts the incoming connection and returns a new Socket that can be used to send data to and receive data from the remote host.


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 the MSDN library 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 following code example uses BeginAccept to create and connect a socket and accept the initial 10 bytes of data. The callback delegate calls EndAccept to end the asynchronous request. The number of bytes transmitted and the data are returned in the buffer and bytesTransferred parameters of this method and are displayed on the console.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft