UdpClient.Receive メソッド
アセンブリ: System (system.dll 内)

Dim instance As UdpClient Dim remoteEP As IPEndPoint Dim returnValue As Byte() returnValue = instance.Receive(remoteEP)
戻り値
データグラム データを格納する Byte 型の配列


Receive メソッドは、リモート ホストからデータグラムが到達するまでブロックします。データが使用可能な場合、Receive メソッドは、最初にキューに格納されたデータグラムを読み取ってデータ部分をバイト配列として返します。このメソッドは、remoteEP パラメータに送信側の IPAddress とポート番号を読み込みます。
Connect メソッドで既定のリモート ホストを指定すると、Receive メソッドはホストからのデータグラムだけを受信します。他のデータグラムはすべて破棄されます。
SocketException が発生した場合は、SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認できます。これは MSDN から入手できます。
![]() |
---|
マルチキャスト データグラムを受信する場合は、Receive メソッドの前に Connect メソッドを呼び出さないでください。データグラムの受信に使用する UdpClient は、マルチキャスト ポート番号を使用して作成する必要があります。 |

Receive メソッドの例を次に示します。Receive メソッドはメッセージを受信するまで実行をブロックします。Receive に渡された IPEndPoint を使用して、応答するホストの ID が明らかになります。
'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 'MyUdpClientCommunicator
//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. 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(); // 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. ubyte receiveBytes[] = receivingUdpClient.Receive(remoteIpEndPoint); String returnData = Encoding.get_ASCII().GetString(receiveBytes); Console.WriteLine(("This is the message you received " + returnData.ToString())); Console.WriteLine(("This message was sent from " + remoteIpEndPoint.get_Address().ToString() + " on their port number " + System.Convert.ToString(remoteIpEndPoint.get_Port()))); } catch (System.Exception e) { Console.WriteLine(e.ToString()); }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からUdpClient.Receive メソッドを検索する場合は、下記のリンクをクリックしてください。

- UdpClient.Receive メソッドのページへのリンク