Bagikan melalui


UdpClient.Receive(IPEndPoint) Metode

Definisi

Mengembalikan datagram UDP yang dikirim oleh host jarak jauh.

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

Parameter

remoteEP
IPEndPoint

Yang IPEndPoint mewakili host jarak jauh tempat data dikirim.

Mengembalikan

Byte[]

Array jenis Byte yang berisi datagram data.

Pengecualian

Yang mendasar Socket telah ditutup.

Terjadi kesalahan saat mengakses soket.

Contoh

Contoh berikut menunjukkan Receive metode . Metode Receive memblokir eksekusi sampai menerima pesan. Menggunakan yang diteruskan IPEndPoint ke Receive, identitas host yang merespons terungkap.

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

Keterangan

Metode ini Receive akan memblokir hingga datagram tiba dari host jarak jauh. Ketika data tersedia, Receive metode akan membaca datagram antrean pertama dan mengembalikan bagian data sebagai array byte. Metode ini mengisi remoteEP parameter dengan IPAddress nomor port dan pengirim.

Jika Anda menentukan host jarak jauh default dalam Connect metode , metode akan menerima datagram dari host tersebut Receive saja. Semua datagram lainnya akan dibuang.

Jika Anda menerima SocketException, gunakan SocketException.ErrorCode untuk mendapatkan kode kesalahan tertentu. Setelah mendapatkan kode ini, Anda dapat merujuk ke dokumentasi kode kesalahan API Windows Sockets versi 2 untuk deskripsi terperinci tentang kesalahan tersebut.

Catatan

Jika Anda berniat menerima datagram multicasted, jangan panggil Connect metode sebelum memanggil Receive metode . Yang UdpClient Anda gunakan untuk menerima datagram harus dibuat menggunakan nomor port multicast.

Berlaku untuk

Lihat juga