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)

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

Parameters

buffer
Type: System.Byte()%
An array of type Byte that contains the bytes transferred.
bytesTransferred
Type: System.Int32%
The number of bytes transferred.
asyncResult
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.

ExceptionCondition
NotSupportedException

Windows NT is required for this method.

ObjectDisposedException

The Socket object has been closed.

ArgumentNullException

asyncResult is empty.

ArgumentException

asyncResult was not created by a call to BeginAccept.

InvalidOperationException

EndAccept method was previously called.

SocketException

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.

NoteNote

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.

NoteNote

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

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.


    ' This server waits for a connection and then uses asynchronous operations to
    ' accept the connection with initial data sent from the client.


    ' Establish the local endpoint for the socket.

    Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
    Dim localEndPoint As New IPEndPoint(ipAddress, 11000)

    ' Create a TCP/IP socket.
    Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

    ' Bind the socket to the local endpoint, and listen for incoming connections.
    listener.Bind(localEndPoint)
    listener.Listen(100)

    Do
        ' Set the event to nonsignaled state.
        allDone.Reset()

        ' Start an asynchronous socket to listen for connections and receive data from the client.
        Console.WriteLine("Waiting for a connection...")

        ' Accept the connection and receive the first 10 bytes of data.
        Dim receivedDataSize As Integer = 10
        listener.BeginAccept(receivedDataSize, New AsyncCallback(AddressOf AcceptReceiveCallback), listener)

        ' Wait until a connection is made and processed before continuing.
        allDone.WaitOne()
    Loop

End Sub


Public Shared Sub AcceptReceiveCallback(ByVal ar As IAsyncResult)
    ' Get the socket that handles the client request.
    Dim listener As Socket = CType(ar.AsyncState, Socket)

    ' End the operation and display the received data on the console.
    Dim Buffer() As Byte
    Dim bytesTransferred As Integer
    Dim handler As Socket = listener.EndAccept(Buffer, bytesTransferred, ar)
    Dim stringTransferred As String = Encoding.ASCII.GetString(Buffer, 0, bytesTransferred)

    Console.WriteLine(stringTransferred)
    Console.WriteLine("Size of data transferred is {0}", bytesTransferred)

    ' Create the state object for the asynchronous receive.
    Dim state As New StateObject()
    state.workSocket = handler
    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
End Sub


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

ADD
Show:
© 2014 Microsoft