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

定义

结束挂起的异步读取。Ends a pending asynchronous read.

重载

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

参数

asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

IAsyncResult,它存储此异步操作的状态信息以及所有用户定义的数据。An IAsyncResult that stores state information and any user defined data for this asynchronous operation.

返回

收到的字节数。The number of bytes received.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例结束挂起的异步读取。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

注解

方法完成BeginReceive方法中启动的异步读取操作。 EndReceiveThe EndReceive method completes the asynchronous read operation started in the BeginReceive method.

在调用BeginReceive之前,需要创建一个AsyncCallback实现委托的回调方法。Before calling BeginReceive, you need to create a callback method that implements the AsyncCallback delegate. 此回调方法在单独的线程中执行,并在返回后BeginReceive由系统调用。This callback method executes in a separate thread and is called by the system after BeginReceive returns. 回调方法必须接受IAsyncResult BeginReceive方法返回的作为参数。The callback method must accept the IAsyncResult returned by the BeginReceive method as a parameter.

在回调方法中调用AsyncStateIAsyncResult方法,以获取传递给BeginReceive方法的状态对象。Within the callback method, call the AsyncState method of the IAsyncResult to obtain the state object passed to the BeginReceive method. 从此状态对象Socket提取接收。Extract the receiving Socket from this state object. 获取Socket后,可以EndReceive调用方法来成功完成读取操作并返回读取的字节数。After obtaining the Socket, you can call the EndReceive method to successfully complete the read operation and return the number of bytes read.

EndReceive数据可用之前,将阻止该方法。The EndReceive method will block until data is available. 如果使用的是无连接协议, EndReceive将读取传入网络缓冲区中的第一个排队数据报。If you are using a connectionless protocol, EndReceive will read the first enqueued datagram available in the incoming network buffer. 如果使用面向连接的协议,则该EndReceive方法将读取尽可能多的数据,最多可达在BeginReceive方法的size参数中指定的字节数。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. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据,则该EndReceive方法将立即完成并返回零字节。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.

若要获取收到的数据,请AsyncState调用的IAsyncResult方法,并提取生成的状态对象中包含的缓冲区。To obtain the received data, call the AsyncState method of the IAsyncResult, and extract the buffer contained in the resulting state object.

若要取消挂起BeginReceive的,请Close调用方法。To cancel a pending BeginReceive, call the Close method.

备注

如果收到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.

备注

给定线程启动的所有 i/o 都将在该线程退出时取消。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

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 * SocketError -> int
Public Function EndReceive (asyncResult As IAsyncResult, ByRef errorCode As SocketError) As Integer

参数

asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

IAsyncResult,它存储此异步操作的状态信息以及所有用户定义的数据。An IAsyncResult that stores state information and any user defined data for this asynchronous operation.

errorCode
SocketError SocketError SocketError SocketError

一个 SocketError 对象,它存储套接字错误。A SocketError object that stores the socket error.

返回

收到的字节数。The number of bytes received.

异常

尝试访问套接字时出错。An error occurred when attempting to access the socket.

注解

方法完成BeginReceive方法中启动的异步读取操作。 EndReceiveThe EndReceive method completes the asynchronous read operation started in the BeginReceive method.

在调用BeginReceive之前,需要创建一个AsyncCallback实现委托的回调方法。Before calling BeginReceive, you need to create a callback method that implements the AsyncCallback delegate. 此回调方法在单独的线程中执行,并在返回后BeginReceive由系统调用。This callback method executes in a separate thread and is called by the system after BeginReceive returns. 回调方法必须接受IAsyncResult BeginReceive方法返回的作为参数。The callback method must accept the IAsyncResult returned by the BeginReceive method as a parameter.

在回调方法中调用AsyncStateIAsyncResult方法,以获取传递给BeginReceive方法的状态对象。Within the callback method, call the AsyncState method of the IAsyncResult to obtain the state object passed to the BeginReceive method. 从此状态对象Socket提取接收。Extract the receiving Socket from this state object. 获取Socket后,可以EndReceive调用方法来成功完成读取操作并返回读取的字节数。After obtaining the Socket, you can call the EndReceive method to successfully complete the read operation and return the number of bytes read.

EndReceive数据可用之前,将阻止该方法。The EndReceive method will block until data is available. 如果使用的是无连接协议, EndReceive将读取传入网络缓冲区中的第一个排队数据报。If you are using a connectionless protocol, EndReceive will read the first enqueued datagram available in the incoming network buffer. 如果使用面向连接的协议,则该EndReceive方法将读取尽可能多的数据,最多可达在BeginReceive方法的size参数中指定的字节数。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. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据,则该EndReceive方法将立即完成并返回零字节。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.

若要获取收到的数据,请AsyncState调用的IAsyncResult方法,并提取生成的状态对象中包含的缓冲区。To obtain the received data, call the AsyncState method of the IAsyncResult, and extract the buffer contained in the resulting state object.

备注

如果收到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.

备注

给定线程启动的所有 i/o 都将在该线程退出时取消。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.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

适用于