UdpClient.Receive(IPEndPoint) Metoda

Definice

Vrátí datagram UDP odeslaný vzdáleným hostitelem.

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

Parametry

remoteEP
IPEndPoint

Představuje IPEndPoint vzdáleného hostitele, ze kterého byla data odeslána.

Návraty

Byte[]

Pole typu Byte , které obsahuje datagramová data.

Výjimky

Podkladová služba Socket byla uzavřena.

Při přístupu k soketu došlo k chybě.

Příklady

Následující příklad ukazuje metodu Receive . Metoda Receive blokuje provádění, dokud neobdrží zprávu. IPEndPoint Pomocí příkazu předaného do Receivese odhalí identita odpovídajícího hostitele.

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

Poznámky

Metoda Receive bude blokovat, dokud datagram nepřijde ze vzdáleného hostitele. Pokud jsou data k dispozici, Receive metoda načte první datagram ve frontě a vrátí datovou část jako pole bajtů. Tato metoda naplní remoteEP parametr číslem IPAddress a portu odesílatele.

Pokud v metodě zadáte výchozího vzdáleného Connect hostitele, Receive metoda bude přijímat datagramy pouze z daného hostitele. Všechny ostatní datagramy budou zahozeny.

Pokud se zobrazí SocketException, použijte SocketException.ErrorCode k získání konkrétního kódu chyby. Jakmile tento kód získáte, můžete se podívat do dokumentace k chybovému kódu rozhraní API rozhraní Windows Sockets verze 2 , kde najdete podrobný popis chyby.

Poznámka

Pokud chcete přijímat datagramy vícesměrového vysílání, nevolejte metodu Connect před voláním Receive metody . Objekt UdpClient , který používáte k příjmu datagramů, musí být vytvořen pomocí čísla portu vícesměrového vysílání.

Platí pro

Viz také