UdpClient クラス

定義

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

public ref class UdpClient : IDisposable
public class UdpClient : IDisposable
type UdpClient = class
    interface IDisposable
Public Class UdpClient
Implements IDisposable
継承
UdpClient
実装

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

// 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(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());
        }
     ' 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

注釈

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

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

  • クラスのインスタンスを UdpClient 作成し、 メソッドを Connect 呼び出します。

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

Note

ホスト名IPEndPointを使用するか、既定のリモート ホストを既に指定している場合は、 を呼び出Sendさないでください。 その場合、 UdpClient は例外をスローします。

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

コンストラクター

UdpClient()

UdpClient クラスの新しいインスタンスを初期化します。

UdpClient(AddressFamily)

UdpClient クラスの新しいインスタンスを初期化します。

UdpClient(Int32)

UdpClient クラスの新しいインスタンスを初期化し、指定したローカル ポート番号にバインドします。

UdpClient(Int32, AddressFamily)

UdpClient クラスの新しいインスタンスを初期化し、指定したローカル ポート番号にバインドします。

UdpClient(IPEndPoint)

UdpClient クラスの新しいインスタンスを初期化し、指定したローカル エンドポイントにバインドします。

UdpClient(String, Int32)

UdpClient クラスの新しいインスタンスを初期化し、既定のリモート ホストを確立します。

プロパティ

Active

既定のリモート ホストが確立されたかどうかを示す値を取得または設定します。

Available

読み取りが可能なネットワークから受信したデータの量を取得します。

Client

基になるネットワーク Socket を取得または設定します。

DontFragment

UdpClient でインターネット プロトコル (IP) データグラムの断片化を許可するかどうかを指定する Boolean 値を、取得または設定します。

EnableBroadcast

がブロードキャスト パケットを Boolean 送信できるかどうかを示す値を UdpClient 取得または設定します。

ExclusiveAddressUse

UdpClient で 1 つのクライアントだけがポートを使用できるかどうかを指定する Boolean 値を取得または設定します。

MulticastLoopback

発信マルチキャスト パケットが送信元アプリケーションに配信されるかどうかを指定する Boolean 値を取得または設定します。

Ttl

UdpClient によって送信されたインターネット プロトコル (IP) パケットの有効期間 (TTL) の値を指定する値を取得または設定します。

メソッド

AllowNatTraversal(Boolean)

UdpClient インスタンスのネットワーク アドレス変換 (NAT: Network Address Translation) トラバーサルを有効または無効にします。

BeginReceive(AsyncCallback, Object)

データグラムをリモート ホストから非同期的に受信します。

BeginSend(Byte[], Int32, AsyncCallback, Object)

データグラムをリモート ホストに非同期的に送信します。 送信先は、Connect の呼び出しであらかじめ指定されています。

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

データグラムを送信先に非同期的に送信します。 送信先は、EndPoint で指定されます。

BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

データグラムを送信先に非同期的に送信します。 送信先は、ホスト名とポート番号で指定されます。

Close()

UDP 接続を終了します。

Connect(IPAddress, Int32)

指定した IP アドレスとポート番号を使用して、既定のリモート ホストを確立します。

Connect(IPEndPoint)

指定されたネットワーク エンドポイントを使用して、既定のリモート ホストを確立します。

Connect(String, Int32)

指定したホスト名とポート番号を使用して、既定のリモート ホストを確立します。

Dispose()

UdpClient によって使用されているマネージド リソースおよびアンマネージド リソースを解放します。

Dispose(Boolean)

UdpClient によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

DropMulticastGroup(IPAddress)

マルチキャスト グループへの参加を取り消します。

DropMulticastGroup(IPAddress, Int32)

マルチキャスト グループへの参加を取り消します。

EndReceive(IAsyncResult, IPEndPoint)

保留中の非同期受信を終了します。

EndSend(IAsyncResult)

保留中の非同期送信を終了します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
JoinMulticastGroup(Int32, IPAddress)

UdpClient をマルチキャスト グループに追加します。

JoinMulticastGroup(IPAddress)

UdpClient をマルチキャスト グループに追加します。

JoinMulticastGroup(IPAddress, Int32)

指定された有効期間 (TTL: Time to Live) で UdpClient をマルチキャスト グループに追加します。

JoinMulticastGroup(IPAddress, IPAddress)

UdpClient をマルチキャスト グループに追加します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Receive(IPEndPoint)

リモート ホストが送信した UDP データグラムを返します。

ReceiveAsync()

リモート ホストが送信した UDP データグラムを非同期的に返します。

ReceiveAsync(CancellationToken)

リモート ホストが送信した UDP データグラムを非同期的に返します。

Send(Byte[], Int32)

UDP データグラムをリモート ホストに送信します。

Send(Byte[], Int32, IPEndPoint)

指定したリモート エンドポイントにあるホストに UDP データグラムを送信します。

Send(Byte[], Int32, String, Int32)

指定したリモート ホストの指定したポートに UDP データグラムを送信します。

Send(ReadOnlySpan<Byte>)

UDP データグラムをリモート ホストに送信します。

Send(ReadOnlySpan<Byte>, IPEndPoint)

指定したリモート エンドポイントにあるホストに UDP データグラムを送信します。

Send(ReadOnlySpan<Byte>, String, Int32)

指定したリモート ホストの指定したポートに UDP データグラムを送信します。

SendAsync(Byte[], Int32)

UDP データグラムをリモート ホストに非同期的に送信します。

SendAsync(Byte[], Int32, IPEndPoint)

UDP データグラムをリモート ホストに非同期的に送信します。

SendAsync(Byte[], Int32, String, Int32)

UDP データグラムをリモート ホストに非同期的に送信します。

SendAsync(ReadOnlyMemory<Byte>, CancellationToken)

UDP データグラムをリモート ホストに非同期的に送信します。

SendAsync(ReadOnlyMemory<Byte>, IPEndPoint, CancellationToken)

UDP データグラムをリモート ホストに非同期的に送信します。

SendAsync(ReadOnlyMemory<Byte>, String, Int32, CancellationToken)

UDP データグラムをリモート ホストに非同期的に送信します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

IDisposable.Dispose()

この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。

UdpClient によって使用されているすべてのリソースを解放します。

適用対象

こちらもご覧ください