UdpClient.Receive(IPEndPoint) 方法

定义

返回由远程主机发送的 UDP 数据报。Returns a UDP datagram that was sent by a remote host.

public:
 cli::array <System::Byte> ^ Receive(System::Net::IPEndPoint ^ % remoteEP);
public byte[] Receive (ref System.Net.IPEndPoint? remoteEP);
public byte[] Receive (ref System.Net.IPEndPoint remoteEP);
member this.Receive : IPEndPoint -> byte[]
Public Function Receive (ByRef remoteEP As IPEndPoint) As Byte()

参数

remoteEP
IPEndPoint

一个 IPEndPoint,它表示从其发送数据的远程主机。An IPEndPoint that represents the remote host from which the data was sent.

返回

Byte[]

一个 Byte 类型的数组,它包含数据报数据。An array of type Byte that contains datagram data.

例外

已关闭基础 SocketThe underlying Socket has been closed.

访问套接字时出错。An error occurred when accessing the socket.

示例

下面的示例演示 Receive 方法。The following example demonstrates the Receive method. Receive方法阻止执行,直到收到消息。The Receive method blocks execution until it receives a message. 使用传递给的将显示 IPEndPoint Receive 响应主机的标识。Using the IPEndPoint passed to Receive, the identity of the responding host is revealed.

//Creates a UdpClient for reading incoming data.
UdpClient^ receivingUdpClient = gcnew UdpClient( 11000 );

//Creates an IPEndPoint to record the IP Address and port number of the sender. 
// The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint( IPAddress::Any,0 );
try
{
   // Blocks until a message returns on this socket from a remote host.
   array<Byte>^receiveBytes = receivingUdpClient->Receive(  RemoteIpEndPoint );

   String^ returnData = Encoding::ASCII->GetString( receiveBytes );

   Console::WriteLine( "This is the message you received {0}", returnData );
   Console::WriteLine( "This message was sent from {0} on their port number {1}",
      RemoteIpEndPoint->Address, RemoteIpEndPoint->Port );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
 //Creates a UdpClient for reading incoming data.
 UdpClient receivingUdpClient = new UdpClient(11000);

 //Creates an IPEndPoint to record the IP Address and port number of the sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
 IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
 try{

     // Blocks until a message returns on this socket from a remote host.
     Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

     string returnData = Encoding.ASCII.GetString(receiveBytes);

     Console.WriteLine("This is the message you received " +
                               returnData.ToString());
     Console.WriteLine("This message was sent from " +
                                 RemoteIpEndPoint.Address.ToString() +
                                 " on their port number " +
                                 RemoteIpEndPoint.Port.ToString());
 }
 catch ( Exception e ){
     Console.WriteLine(e.ToString());
 }

   'Creates a UdpClient for reading incoming data.
   Dim receivingUdpClient As New UdpClient(11000)
   
   'Creates an IPEndPoint to record the IP address and port number of the sender. 
   ' The IPEndPoint will allow you to read datagrams sent from any source.
   Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
   Try
      
      ' Blocks until a message returns on this socket from a remote host.
      Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)
      
      Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
      
      Console.WriteLine(("This is the message you received " + returnData.ToString()))
      Console.WriteLine(("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()))
   Catch e As Exception
      Console.WriteLine(e.ToString())
   End Try
End Sub
 

注解

Receive 方法将一直阻止到数据报从远程主机到达之前。The Receive method will block until a datagram arrives from a remote host. 当数据可用时,该 Receive 方法将读取第一个排队数据报,并将数据部分作为字节数组返回。When data is available, the Receive method will read the first enqueued datagram and return the data portion as a byte array. 此方法 remoteEPIPAddress 发送方的和端口号填充参数。This method populates the remoteEP parameter with the IPAddress and port number of the sender.

如果在方法中指定默认远程主机 Connect ,则该 Receive 方法将仅接受来自该主机的数据报。If you specify a default remote host in the Connect method, the Receive method will accept datagrams from that host only. 所有其他数据报将被丢弃。All other datagrams will be discarded.

如果收到 SocketException ,请使用 SocketException.ErrorCode 获取特定的错误代码。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 获取此代码后,可以参考 Windows 套接字版本 2 API 错误代码 文档,以获取有关错误的详细说明。Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

如果打算接收多播数据报,请不要在 Connect 调用方法之前调用方法 ReceiveIf you intend to receive multicasted datagrams, do not call the Connect method prior to calling the Receive method. UdpClient用于接收数据报的必须使用多播端口号来创建。The UdpClient you use to receive datagrams must be created using the multicast port number.

适用于

另请参阅