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

注解

EndReceive方法完成异步读取的操作以启动BeginReceive方法。The 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.

在回调方法中,调用AsyncState方法IAsyncResult若要获取状态对象传递给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方法将读取的数据,可在指定的字节数目可高达size参数的BeginReceive方法。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 *  -> 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.

注解

EndReceive方法完成异步读取的操作以启动BeginReceive方法。The 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.

在回调方法中,调用AsyncState方法IAsyncResult若要获取状态对象传递给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方法将读取的数据,可在指定的字节数目可高达size参数的BeginReceive方法。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.

另请参阅

适用于