次の方法で共有


UdpClient クラス

ユーザー データグラム プロトコル (UDP) ネットワーク サービスを提供します。

この型のすべてのメンバの一覧については、UdpClient メンバ を参照してください。

System.Object
   System.Net.Sockets.UdpClient

Public Class UdpClient
   Implements IDisposable
[C#]
public class UdpClient : IDisposable
[C++]
public __gc class UdpClient : public IDisposable
[JScript]
public class UdpClient implements IDisposable

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

UdpClient クラスは、同期ブロッキング モードのときにコネクションレスの UDP データグラムを送受信するための単純なメソッドを提供します。UDP はコネクションレスのトランスポート プロトコルであるため、データを送受信する前にリモート ホスト接続を確立する必要はありません。ただし、オプションとして次の 2 つのいずれかの方法で既定のリモート ホストを設定できます。

  • リモート ホスト名とポート番号をパラメータとして使用して UdpClient クラスのインスタンスを作成します。
  • UdpClient クラスのインスタンスを作成し、 Connect メソッドを呼び出します。

UdpClient が用意している送信メソッドのうちの任意のメソッドを使用して、リモート デバイスにデータを送信できます。 Receive メソッドを使用して、リモート ホストからデータを受信します。

メモ   既定のリモート ホストが既に指定されている場合は、ホスト名または IPEndPoint を使用して Send を呼び出すことはできません。この呼び出しを行った場合は、 UdpClient が例外をスローします。

また、 UdpClient メソッドを使用すると、マルチキャスト データグラムを送受信することもできます。 JoinMulticastGroup メソッドを使用して、 UdpClient がマルチキャスト グループをサブスクライブするようにします。 DropMulticastGroup メソッドは、マルチキャスト グループから UdpClient をアンサブスクライブするときに使用します。

使用例

[Visual Basic, C#, C++] ポート 11000 上のホスト名 www.contoso.com を使用して UdpClient 接続を確立する例を次に示します。2 つの別個のリモート ホスト コンピュータに小さな文字列メッセージが送信されます。 Receive メソッドはメッセージを受信するまで実行をブロックします。 Receive に渡された IPEndPoint を使用して、応答するホストの ID が明らかになります。

 
' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient()
Try
   udpClient.Connect("www.contoso.com", 11000)
   
   ' Sends a message to the host to which you have connected.
   Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
   
   udpClient.Send(sendBytes, sendBytes.Length)
   
   ' Sends message to a different host using optional hostname and port parameters.
   Dim udpClientB As New UdpClient()
   udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000)
   
   'Blocks until a message returns on this socket from a remote host.
   Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
   
   ' Blocks until a message returns on this socket from a remote host.
   Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
   Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
   
   ' Which one of these two hosts responded?
   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()))
   udpClient.Close()
   udpClientB.Close()
 
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try
   End Sub 

[C#] 
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient();
    try{
         udpClient.Connect("www.contoso.com", 11000);

         // Sends a message to the host to which you have connected.
         Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
      
         udpClient.Send(sendBytes, sendBytes.Length);

         // Sends a message to a different host using optional hostname and port parameters.
         UdpClient udpClientB = new UdpClient();
         udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);

         //IPEndPoint object will allow us to read datagrams sent from any source.
         IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

         // Blocks until a message returns on this socket from a remote host.
         Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); 
         string returnData = Encoding.ASCII.GetString(receiveBytes);
   
         // Uses the IPEndPoint object to determine which of these two hosts responded.
         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());

          udpClient.Close();
          udpClientB.Close();
          
          }  
       catch (Exception e ) {
                  Console.WriteLine(e.ToString());
        }

[C++] 
// With this constructor the local port number is arbitrarily assigned.
UdpClient __gc *udpClient = new UdpClient();
try {
    udpClient->Connect(S"host.contoso.com", 11000);

    // Send message to the host to which you have connected.
    Byte sendBytes __gc[] = Encoding::ASCII->GetBytes(S"Is anybody there?");

    udpClient->Send(sendBytes, sendBytes->Length);

    // Send message to a different host using optional hostname and port parameters.
    UdpClient __gc *udpClientB = new UdpClient();
    udpClientB->Send(sendBytes, sendBytes->Length, S"AlternateHostMachineName", 11000);

    //IPEndPoint object will allow us to read datagrams sent from any source.
    IPEndPoint __gc *RemoteIpEndPoint = new IPEndPoint(IPAddress::Any, 0);

    // Block until a message returns on this socket from a remote host.
    Byte receiveBytes __gc[] = udpClient->Receive(&RemoteIpEndPoint); 
    String __gc *returnData = Encoding::ASCII->GetString(receiveBytes);

    // Use the IPEndPoint object to determine which of these two hosts responded.
    Console::WriteLine(String::Concat(S"This is the message you received ", returnData->ToString()));
    Console::WriteLine(String::Concat(S"This message was sent from ", RemoteIpEndPoint->Address->ToString(),
                                      S" on their port number ", __box(RemoteIpEndPoint->Port)->ToString()));

    udpClient->Close();
    udpClientB->Close();

}  
catch (Exception __gc *e ) {
    Console::WriteLine(e->ToString());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Net.Sockets

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: System (System.dll 内)

.NET Framework セキュリティ:

  • SocketPermission 送信接続を確立するか、受信要求を受け入れるための 。

参照

UdpClient メンバ | System.Net.Sockets 名前空間 | TcpClient | TCP/UDP