Aracılığıyla paylaş


UdpClient.Receive(IPEndPoint) Yöntem

Tanım

Uzak bir konak tarafından gönderilen bir UDP veri birimi döndürür.

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

Parametreler

remoteEP
IPEndPoint

IPEndPoint Verilerin gönderildiği uzak konağı temsil eden.

Döndürülenler

Byte[]

Veri birimi verilerini içeren tür Byte dizisi.

Özel durumlar

Temel alınan Socket kapatıldı.

Yuvaya erişilirken bir hata oluştu.

Örnekler

Aşağıdaki örnekte yöntemi gösterilmektedir Receive . yöntemi, Receive bir ileti alıncaya kadar yürütmeyi engeller. IPEndPoint öğesine geçirilen Receivekullanılarak, yanıtlayan konağın kimliği gösterilir.

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

Açıklamalar

yöntemi, Receive uzak bir konaktan bir veri birimi gelene kadar engeller. Veriler kullanılabilir olduğunda, Receive yöntem ilk sıraya alınan veri birimi okur ve veri bölümünü bayt dizisi olarak döndürür. Bu yöntem parametresini remoteEP gönderenin IPAddress ve bağlantı noktası numarasıyla doldurur.

yönteminde Connect varsayılan bir uzak konak belirtirseniz, Receive yöntem yalnızca bu konaktan veri birimlerini kabul eder. Diğer tüm veri birimleri atılır.

bir alırsanız, belirli bir SocketExceptionhata kodunu almak için kullanın SocketException.ErrorCode . Bu kodu aldıktan sonra, hatanın ayrıntılı açıklaması için Windows Yuvaları sürüm 2 API hata kodu belgelerine bakabilirsiniz.

Not

Çok noktaya yayınlanmış veri birimleri almak istiyorsanız, yöntemini çağırmadan önce yöntemini çağırmayın ConnectReceive . UdpClient Veri birimlerini almak için kullandığınız, çok noktaya yayın bağlantı noktası numarası kullanılarak oluşturulmalıdır.

Şunlara uygulanır

Ayrıca bkz.