Socket.EndReceiveFrom(IAsyncResult, EndPoint) Socket.EndReceiveFrom(IAsyncResult, EndPoint) Socket.EndReceiveFrom(IAsyncResult, EndPoint) Socket.EndReceiveFrom(IAsyncResult, EndPoint) Method


结束挂起的、从特定终结点进行异步读取。Ends a pending asynchronous read from a specific endpoint.

 int EndReceiveFrom(IAsyncResult ^ result, System::Net::EndPoint ^ % end_point);
public int EndReceiveFrom (IAsyncResult result, ref System.Net.EndPoint end_point);
member this.EndReceiveFrom : IAsyncResult *  -> int
Public Function EndReceiveFrom (result As IAsyncResult, ByRef end_point As EndPoint) As Integer


IAsyncResult IAsyncResult IAsyncResult IAsyncResult

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

EndPoint EndPoint EndPoint EndPoint

EndPointThe source EndPoint.


如果成功,则返回已接收的字节数。If successful, the number of bytes received. 如果不成功,则返回 0。If unsuccessful, returns 0.


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


下面的代码示例结束挂起的异步读取从特定EndPointThe following code example ends a pending asynchronous read from a specific EndPoint.

StateObject^ so = safe_cast<StateObject^>(ar->AsyncState);
Socket^ s = so->workSocket;

// Creates a temporary EndPoint to pass to EndReceiveFrom.
IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 );
EndPoint^ tempRemoteEP = safe_cast<EndPoint^>(sender);

int read = s->EndReceiveFrom( ar, tempRemoteEP );

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

      // Creates a temporary EndPoint to pass to EndReceiveFrom.
      IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sender;

      int read = s.EndReceiveFrom(ar, ref tempRemoteEP); 

if (read > 0) {
 , 0, read));
           s.BeginReceiveFrom(so.buffer, 0, StateObject.BUFFER_SIZE, 0, ref tempRemoteEP,
           	                     new AsyncCallback(Async_Send_Receive.ReceiveFrom_Callback), so);
     if ( > 1) {
          //All the data has been read, so displays it to the console.
          string strContent;
          strContent =;
          Console.WriteLine(String.Format("Read {0} byte from socket" + 
          	               "data = {1} ", strContent.Length, strContent));
   Dim so As StateObject = CType(ar.AsyncState, StateObject)
   Dim s As Socket = so.workSocket
   ' Creates a temporary EndPoint to pass to EndReceiveFrom.
   Dim sender As New IPEndPoint(IPAddress.Any, 0)
   Dim tempRemoteEP As EndPoint = CType(sender, EndPoint)
   Dim read As Integer = s.EndReceiveFrom(ar, tempRemoteEP)
   If read > 0 Then, 0, read))
      s.BeginReceiveFrom(so.buffer, 0, StateObject.BUFFER_SIZE, 0, tempRemoteEP, New AsyncCallback(AddressOf Async_Send_Receive.ReceiveFrom_Callback), so)
      If > 1 Then
         'All the data has been read, so displays it to the console.
         Dim strContent As String
         strContent =
         Console.WriteLine([String].Format("Read {0} byte from socket" + "data = {1} ", strContent.Length, strContent))
      End If
   End If 
End Sub 'ReceiveFrom_Callback


EndReceiveFrom方法完成异步读取的操作以启动BeginReceiveFrom方法。The EndReceiveFrom method completes the asynchronous read operation started in the BeginReceiveFrom method.

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

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

EndReceiveFrom方法将阻塞,直到有可用数据。The EndReceiveFrom method will block until data is available. 如果您使用的是无连接协议,EndReceiveFrom将读取传入的网络缓冲区中有可用的第一个排队数据报。If you are using a connectionless protocol, EndReceiveFrom will read the first enqueued datagram available in the incoming network buffer. 如果使用面向连接的协议EndReceiveFrom方法将读取的数据,可在指定的字节数目可高达size参数的BeginReceiveFrom方法。If you are using a connection-oriented protocol, the EndReceiveFrom method will read as much data as is available up to the number of bytes you specified in the size parameter of the BeginReceiveFrom method. 如果远程主机关闭Socket与连接Shutdown收到了方法,并且所有可用数据,EndReceiveFrom方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the EndReceiveFrom method will complete immediately and return zero bytes. 若要获取所接收的数据,请调用AsyncState方法的IAsyncResult对象,并提取生成的状态对象中包含的缓冲区。To obtain the received data, call the AsyncState method of the IAsyncResult object, and extract the buffer contained in the resulting state object. 若要标识原始主机,提取EndPoint将其转换为IPEndPointTo identify the originating host, extract the EndPoint and cast it to an IPEndPoint. 使用IPEndPoint.Address方法来获取 IP 地址和IPEndPoint.Port方法来获取端口号。Use the IPEndPoint.Address method to obtain the IP address and the IPEndPoint.Port method to obtain the port number.


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


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