UdpClient.Receive(IPEndPoint) Metoda

Definicja

Zwraca datagram UDP, który został wysłany przez hosta zdalnego.

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

Element IPEndPoint reprezentujący zdalny host, z którego zostały wysłane dane.

Zwraca

Byte[]

Tablica typu Byte , która zawiera dane datagramu.

Wyjątki

Podstawy Socket zostały zamknięte.

Wystąpił błąd podczas uzyskiwania dostępu do gniazda.

Przykłady

W poniższym przykładzie pokazano metodę Receive . Metoda Receive blokuje wykonywanie, dopóki nie otrzyma komunikatu. Przy użyciu przekazanego IPEndPoint do Receiveelementu zostanie ujawniona tożsamość odpowiadającego hosta.

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

Uwagi

Metoda Receive zostanie zablokowana do momentu odebrania datagramu z hosta zdalnego. Gdy dane są dostępne, Receive metoda odczytuje pierwszy w kolejce datagram i zwraca część danych jako tablicę bajtów. Ta metoda wypełnia remoteEP parametr numerem IPAddress portu i nadawcy.

Jeśli określisz domyślny host zdalny w metodzie Connect , Receive metoda zaakceptuje tylko datagramy z tego hosta. Wszystkie inne datagramy zostaną odrzucone.

Jeśli zostanie wyświetlony komunikat SocketException, użyj polecenia SocketException.ErrorCode , aby uzyskać określony kod błędu. Po uzyskaniu tego kodu możesz zapoznać się z dokumentacją kodu błędu interfejsu API gniazd systemu Windows w wersji 2 , aby uzyskać szczegółowy opis błędu.

Uwaga

Jeśli zamierzasz odbierać multiemisji datagramy, nie należy wywoływać metody przed wywołaniem ConnectReceive metody . Dane UdpClient używane do odbierania datagramów należy utworzyć przy użyciu numeru portu multiemisji.

Dotyczy

Zobacz też