UdpClient.Receive(IPEndPoint) Méthode

Définition

Retourne un datagramme UDP qui a été envoyé par un hôte distant.

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

Paramètres

remoteEP
IPEndPoint

IPEndPoint qui représente l'hôte distant à partir duquel les données ont été envoyées.

Retours

Byte[]

Tableau de type Byte qui contient les données du datagramme.

Exceptions

Le Socket sous-jacent a été fermé.

Une erreur s’est produite pendant l’accès au socket.

Exemples

L’exemple suivant illustre la Receive méthode. La méthode bloque l’exécution Receive jusqu’à ce qu’elle reçoive un message. À l’aide du IPEndPoint passage à Receive, l’identité de l’hôte répond est révélée.

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

Remarques

La Receive méthode se bloque jusqu’à ce qu’un datagramme arrive d’un hôte distant. Lorsque des données sont disponibles, la Receive méthode lit le premier datagramme mis en file d’attente et retourne la partie de données sous la forme d’un tableau d’octets. Cette méthode remplit le remoteEP paramètre avec le numéro de port et le IPAddress numéro de port de l’expéditeur.

Si vous spécifiez un hôte distant par défaut dans la Connect méthode, la Receive méthode accepte uniquement les datagrammes de cet hôte. Tous les autres datagrammes seront ignorés.

Si vous recevez un SocketException, utilisez-le SocketException.ErrorCode pour obtenir le code d’erreur spécifique. Une fois que vous avez obtenu ce code, vous pouvez vous référer à la documentation du code d’erreur de l’API Windows Sockets version 2 pour obtenir une description détaillée de l’erreur.

Notes

Si vous envisagez de recevoir des datagrammes multidiffusion, n’appelez pas la Connect méthode avant d’appeler la Receive méthode. Vous UdpClient devez créer des datagrammes à l’aide du numéro de port de multidiffusion.

S’applique à

Voir aussi