Compartir a través de


UdpClient.Receive(IPEndPoint) Método

Definición

Devuelve un datagrama UDP enviado por un host remoto.

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()

Parámetros

remoteEP
IPEndPoint

IPEndPoint que representa el host remoto desde el que se enviaron los datos.

Devoluciones

Byte[]

Matriz de tipo Byte que contiene datos de datagramas.

Excepciones

El objeto Socket subyacente se ha cerrado.

Se produjo un error al acceder al socket.

Ejemplos

En el siguiente ejemplo se muestra el Receive método. El método bloquea la Receive ejecución hasta que recibe un mensaje. Con el IPEndPoint objeto pasado a Receive, se revela la identidad del host que responde.

//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

Comentarios

El Receive método se bloqueará hasta que llegue un datagrama desde un host remoto. Cuando los datos están disponibles, el Receive método leerá el primer datagrama en cola y devolverá la parte de datos como una matriz de bytes. Este método rellena el remoteEP parámetro con el IPAddress número de puerto y del remitente.

Si especifica un host remoto predeterminado en el Connect método , el Receive método solo aceptará datagramas de ese host. Se descartarán todos los demás datagramas.

Si recibe un SocketException, use SocketException.ErrorCode para obtener el código de error específico. Una vez que haya obtenido este código, puede consultar la documentación del código de error de la API de Windows Sockets versión 2 para obtener una descripción detallada del error.

Nota

Si piensa recibir datagramas multidifusión, no llame al Connect método antes de llamar al Receive método . El UdpClient que se usa para recibir datagramas debe crearse mediante el número de puerto de multidifusión.

Se aplica a

Consulte también