UdpClient.Receive(IPEndPoint) Metod

Definition

Returnerar ett UDP-datagram som skickades av en fjärrvärd.

public:
 cli::array <System::Byte> ^ Receive(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()

Parametrar

remoteEP
IPEndPoint

En IPEndPoint som representerar fjärrvärden som data skickades från.

Returer

Byte[]

En matris av typen Byte som innehåller datagramdata.

Undantag

Socket Underliggande har stängts.

Ett fel uppstod vid åtkomst till socketen.

Exempel

I följande exempel visas Receive metoden. Metoden Receive blockerar körningen tills den tar emot ett meddelande. Med hjälp av skickad IPEndPoint till Receivevisas identiteten för den svarande värden.

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

Kommentarer

Metoden Receive blockeras tills ett datagram kommer från en fjärrvärd. När data är tillgängliga Receive läser metoden det första kodade datagrammet och returnerar datadelen som en bytematris. Den här metoden fyller parametern remoteEP med IPAddress avsändarens portnummer och portnummer.

Om du anger en standardvärd i Connect metoden Receive accepterar metoden endast datagram från den värden. Alla andra datagram tas bort.

Om du får en SocketExceptionanvänder SocketException.ErrorCode du för att hämta den specifika felkoden. När du har fått den här koden kan du läsa felkoden Windows Sockets version 2 API för en detaljerad beskrivning av felet.

Note

Om du tänker ta emot multicastade datagram ska du inte anropa Connect metoden innan du Receive anropar metoden. Du UdpClient måste skapa det du använder för att ta emot datagram med multicast-portnumret.

Gäller för

Se även