指定したリモート エンドポイントにあるホストに UDP データグラムを送信します。
Overloads Public Function Send( _
ByVal dgram() As Byte, _ ByVal bytes As Integer, _ ByVal endPoint As IPEndPoint _) As Integer
[C#]
public int Send(byte[] dgram,intbytes,IPEndPointendPoint);
[C++]
public: int Send(unsigned chardgram __gc[],intbytes,IPEndPoint* endPoint);
[JScript]
public function Send(
dgram : Byte[],bytes : int,endPoint : IPEndPoint) : int;
パラメータ
- dgram
バイトの配列として表された、送信する UDP データグラムを指定する Byte 型の配列。 - bytes
データグラム内のバイト数。 - endPoint
データグラムの送信先のホストとポートを表す IPEndPoint 。
戻り値
送信されたバイト数。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | dgram が null 参照 (Visual Basic では Nothing) です。 |
InvalidOperationException | UdpClient が既定のリモート ホストを既に確立しました。 |
ObjectDisposedException | UdpClient が閉じています。 |
SocketException | ソケットへのアクセス中にエラーが発生しました。詳細については、「解説」を参照してください。 |
解説
Send メソッドは、指定したエンドポイントにデータグラムを送信し、正常に送信されたバイト数を返します。このオーバーロードを呼び出す前に、データグラム配信先のリモート ホストの IP アドレスとポート番号を使用して、 IPEndPoint を作成しておく必要があります。 IPEndPoint の Address プロパティに SocketOptionName.Broadcast を指定すると、既定のブロードキャスト アドレスである 255.255.255.255 にデータグラムを送信できます。この IPEndPoint を作成したら、 endPoint パラメータとして Send メソッドに渡します。
他のブロードキャスト アドレスにデータグラムを送信する場合は、 Client メソッドを使用して基になる Socket を取得し、ソケット オプションを SocketOptionName.Broadcast に設定します。 Socket クラスに戻って使用することもできます。
メモ Connect メソッドで既にリモート ホストを確立している場合は、 endPoint パラメータをこのメソッドで指定しないでください。これを行うと、 Send メソッドは SocketException をスローします。 SocketException が発生した場合は、 SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのマニュアルから、エラーの詳細情報を確認できます。これは MSDN から入手できます。
使用例
[Visual Basic, C#, C++] Send メソッドの例を次に示します。この例では IPEndPoint を使用して対象のホストを指定します。
Dim udpClient As New UdpClient()
Dim ipAddress As IPAddress = Dns.Resolve("www.contoso.com").AddressList(0)
Dim ipEndPoint As New IPEndPoint(ipAddress, 11004)
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
Try
udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
[C#]
UdpClient udpClient = new UdpClient();
IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004);
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
try{
udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint);
}
catch ( Exception e ){
Console.WriteLine(e.ToString());
}
[C++]
UdpClient* udpClient = new UdpClient();
IPAddress* ipAddress = Dns::Resolve(S"www.contoso.com")->AddressList[0];
IPEndPoint* ipEndPoint = new IPEndPoint(ipAddress, 11004);
Byte sendBytes[] = Encoding::ASCII->GetBytes(S"Is anybody there?");
try{
udpClient->Send(sendBytes, sendBytes->Length, ipEndPoint);
}
catch ( Exception* e ){
Console::WriteLine(e->ToString());
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: 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
参照
UdpClient クラス | UdpClient メンバ | System.Net.Sockets 名前空間 | UdpClient.Send オーバーロードの一覧 | 基本型のエンコーディング | Broadcast | Connect | Socket | IPEndPoint