UdpClient.Receive(IPEndPoint) メソッド

定義

リモート ホストによって送信された UDP データグラムを返します。

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

パラメーター

remoteEP
IPEndPoint

データの送信元のリモート ホストを表す IPEndPoint

返品

Byte[]

データグラム データを含む Byte 型の配列。

例外

基になる Socket が閉じられました。

ソケットにアクセスするときにエラーが発生しました。

次の例では、 Receive メソッドを示します。 Receive メソッドは、メッセージを受信するまで実行をブロックします。 IPEndPointに渡されたReceiveを使用して、応答するホストの ID が表示されます。

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

注釈

Receive メソッドは、データグラムがリモート ホストから到着するまでブロックします。 データが使用可能な場合、 Receive メソッドは最初にエンキューされたデータグラムを読み取り、データ部分をバイト配列として返します。 このメソッドは、 remoteEP パラメーターに送信者の IPAddress とポート番号を設定します。

Connect メソッドで既定のリモート ホストを指定した場合、Receive メソッドはそのホストからのデータグラムのみを受け入れます。 他のすべてのデータグラムは破棄されます。

SocketExceptionを受け取った場合は、SocketException.ErrorCodeを使用して特定のエラー コードを取得します。 このコードを取得したら、エラーの詳細な説明については、 Windows ソケット バージョン 2 API のエラー コード ドキュメントを参照してください。

Note

マルチキャスト データグラムを受信する場合は、Connect メソッドを呼び出す前に、Receive メソッドを呼び出さないでください。 データグラムの受信に使用する UdpClient は、マルチキャスト ポート番号を使用して作成する必要があります。

適用対象

こちらもご覧ください