Socket.EndReceive Socket.EndReceive Socket.EndReceive Socket.EndReceive Method

Definition

Ends a pending asynchronous read.

Overloads

EndReceive(IAsyncResult) EndReceive(IAsyncResult) EndReceive(IAsyncResult) EndReceive(IAsyncResult)

Ends a pending asynchronous read.

EndReceive(IAsyncResult, SocketError) EndReceive(IAsyncResult, SocketError) EndReceive(IAsyncResult, SocketError) EndReceive(IAsyncResult, SocketError)

Ends a pending asynchronous read.

EndReceive(IAsyncResult) EndReceive(IAsyncResult) EndReceive(IAsyncResult) EndReceive(IAsyncResult)

Ends a pending asynchronous read.

public:
 int EndReceive(IAsyncResult ^ result);
public int EndReceive (IAsyncResult result);
member this.EndReceive : IAsyncResult -> int
Public Function EndReceive (result As IAsyncResult) As Integer
Parameters
asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

An IAsyncResult that stores state information and any user defined data for this asynchronous operation.

Returns

The number of bytes received.

Exceptions

An error occurred when attempting to access the socket.

Examples

The following code example ends a pending asynchronous read. For a complete example that demonstrates asynchronous communications with sockets, see Socket Code Examples.

static void Read_Callback( IAsyncResult^ ar )
{
   StateObject^ so = safe_cast<StateObject^>(ar->AsyncState);
   Socket^ s = so->workSocket;

   int read = s->EndReceive( ar );

   if ( read > 0 )
   {
      so->sb->Append( Encoding::ASCII->GetString( so->buffer, 0, read ) );
      s->BeginReceive( so->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None,
         gcnew AsyncCallback( &Async_Send_Receive::Read_Callback ), so );
   }
   else
   {
      if ( so->sb->Length > 1 )
      {
         //All of the data has been read, so displays it to the console
         String^ strContent = so->sb->ToString();
         Console::WriteLine( String::Format( "Read {0} byte from socket" +
            " data = {1} ", strContent->Length, strContent ) );
      }
      s->Close();
   }
}
public static void Read_Callback(IAsyncResult ar){
	StateObject so = (StateObject) ar.AsyncState;
	Socket s = so.workSocket;

	int read = s.EndReceive(ar);

	if (read > 0) {
            so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
            s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0, 
            	                     new AsyncCallback(Async_Send_Receive.Read_Callback), so);
	}
	else{
	     if (so.sb.Length > 1) {
	          //All of the data has been read, so displays it to the console
	          string strContent;
	          strContent = so.sb.ToString();
	          Console.WriteLine(String.Format("Read {0} byte from socket" + 
	          	               "data = {1} ", strContent.Length, strContent));
	     }
	     s.Close();
	}
}
Public Shared Sub Read_Callback(ar As IAsyncResult)
   Dim so As StateObject = CType(ar.AsyncState, StateObject)
   Dim s As Socket = so.workSocket
   
   Dim read As Integer = s.EndReceive(ar)
   
   If read > 0 Then
      so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read))
      s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0, New AsyncCallback(AddressOf Async_Send_Receive.Read_Callback), so)
   Else
      If so.sb.Length > 1 Then
         'All the data has been read, so displays it to the console
         Dim strContent As String
         strContent = so.sb.ToString()
         Console.WriteLine([String].Format("Read {0} byte from socket" + "data = {1} ", strContent.Length, strContent))
      End If
      s.Close()
   End If
End Sub 'Read_Callback

Remarks

The EndReceive method completes the asynchronous read operation started in the BeginReceive method.

Before calling BeginReceive, you need to create a callback method that implements the AsyncCallback delegate. This callback method executes in a separate thread and is called by the system after BeginReceive returns. The callback method must accept the IAsyncResult returned by the BeginReceive method as a parameter.

Within the callback method, call the AsyncState method of the IAsyncResult to obtain the state object passed to the BeginReceive method. Extract the receiving Socket from this state object. After obtaining the Socket, you can call the EndReceive method to successfully complete the read operation and return the number of bytes read.

The EndReceive method will block until data is available. If you are using a connectionless protocol, EndReceive will read the first enqueued datagram available in the incoming network buffer. If you are using a connection-oriented protocol, the EndReceive method will read as much data as is available up to the number of bytes you specified in the size parameter of the BeginReceive method. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the EndReceive method will complete immediately and return zero bytes.

To obtain the received data, call the AsyncState method of the IAsyncResult, and extract the buffer contained in the resulting state object.

To cancel a pending BeginReceive, call the Close method.

Note

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.

Note

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Note

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

See Also

EndReceive(IAsyncResult, SocketError) EndReceive(IAsyncResult, SocketError) EndReceive(IAsyncResult, SocketError) EndReceive(IAsyncResult, SocketError)

Ends a pending asynchronous read.

public:
 int EndReceive(IAsyncResult ^ asyncResult, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int EndReceive (IAsyncResult asyncResult, out System.Net.Sockets.SocketError errorCode);
member this.EndReceive : IAsyncResult *  -> int
Public Function EndReceive (asyncResult As IAsyncResult, ByRef errorCode As SocketError) As Integer
Parameters
asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

An IAsyncResult that stores state information and any user defined data for this asynchronous operation.

errorCode
SocketError SocketError SocketError SocketError

A SocketError object that stores the socket error.

Returns

The number of bytes received.

Exceptions

An error occurred when attempting to access the socket.

Remarks

The EndReceive method completes the asynchronous read operation started in the BeginReceive method.

Before calling BeginReceive, you need to create a callback method that implements the AsyncCallback delegate. This callback method executes in a separate thread and is called by the system after BeginReceive returns. The callback method must accept the IAsyncResult returned by the BeginReceive method as a parameter.

Within the callback method, call the AsyncState method of the IAsyncResult to obtain the state object passed to the BeginReceive method. Extract the receiving Socket from this state object. After obtaining the Socket, you can call the EndReceive method to successfully complete the read operation and return the number of bytes read.

The EndReceive method will block until data is available. If you are using a connectionless protocol, EndReceive will read the first enqueued datagram available in the incoming network buffer. If you are using a connection-oriented protocol, the EndReceive method will read as much data as is available up to the number of bytes you specified in the size parameter of the BeginReceive method. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the EndReceive method will complete immediately and return zero bytes.

To obtain the received data, call the AsyncState method of the IAsyncResult, and extract the buffer contained in the resulting state object.

Note

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.

Note

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Note

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

See Also

Applies to