UdpClient クラスとは? わかりやすく解説

UdpClient クラス

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

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

Public Class UdpClient
    Implements IDisposable
public class UdpClient implements IDisposable
public class UdpClient implements IDisposable
解説解説

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

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

メモメモ

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

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

使用例使用例

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

   ' This constructor arbitrarily assigns the local port number.
   Dim udpClient As New
 UdpClient(11000)
   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)
      
      ' IPEndPoint object will allow us to read datagrams sent from
 any source.
      Dim RemoteIpEndPoint As New
 IPEndPoint(IPAddress.Any, 0)
      
      ' UdpClient.Receive blocks until a message is received 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 
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient(11000);
    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());
        }
// With this constructor the local port number is arbitrarily assigned.
UdpClient^ udpClient = gcnew UdpClient;
try
{
   udpClient->Connect( "host.contoso.com", 11000 );

   // Send message to the host to which you have connected.
   array<Byte>^sendBytes = Encoding::ASCII->GetBytes( "Is anybody there?"
 );
   udpClient->Send( sendBytes, sendBytes->Length );

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

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

   // Block until a message returns on this socket from a remote host.
   array<Byte>^receiveBytes = udpClient->Receive( RemoteIpEndPoint );
   String^ returnData = Encoding::ASCII->GetString( receiveBytes );

   // Use the IPEndPoint object to determine which of these two hosts
 responded.
   Console::WriteLine( String::Concat( "This is the message you received ",
 returnData->ToString() ) );
   Console::WriteLine( String::Concat( "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() );
}
// 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.
    ubyte sendBytes[] = 
        Encoding.get_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.
    ubyte receiveBytes[] = udpClient.Receive(remoteIpEndPoint);
    String returnData = Encoding.get_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.get_Address().ToString() 
        + " on their port number " 
        + System.Convert.ToString(remoteIpEndPoint.get_Port())));
    udpClient.Close();
    udpClientB.Close();
}
catch (System.Exception e) {
    Console.WriteLine(e.ToString());
}
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
  System.Net.Sockets.UdpClient
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UdpClient メンバ
System.Net.Sockets 名前空間
TcpClient クラス
その他の技術情報
TCP/UDP



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「UdpClient クラス」の関連用語

UdpClient クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



UdpClient クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS