次の方法で共有


UdpClient コンストラクタ (String, Int32)

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

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

構文

'宣言
Public Sub New ( _
    hostname As String, _
    port As Integer _
)
'使用
Dim hostname As String
Dim port As Integer

Dim instance As New UdpClient(hostname, port)
public UdpClient (
    string hostname,
    int port
)
public:
UdpClient (
    String^ hostname, 
    int port
)
public UdpClient (
    String hostname, 
    int port
)
public function UdpClient (
    hostname : String, 
    port : int
)

パラメータ

  • hostname
    接続先のリモート DNS ホスト名。
  • port
    接続先のリモート ポート番号。

例外

例外の種類 条件

ArgumentNullException

hostname が null 参照 (Visual Basic では Nothing) です。

ArgumentOutOfRangeException

port が MinPortMaxPort の間の値ではありません。

SocketException

ソケットへのアクセス中にエラーが発生しました。詳細については「解説」を参照してください。

解説

このコンストラクタは新しい UdpClient を初期化し、hostname パラメータおよび port パラメータを使用してリモート ホストを確立します。既定のリモート ホストの確立は省略できます。このコンストラクタを使用すると、Send メソッドへの各呼び出しでリモート ホストを指定する必要がありません。既定のリモート ホストを指定すると、そのホストに限定されるようになります。既定のリモート ホストは、Connect を呼び出すことによっていつでも変更できます。Send メソッドへの呼び出しでリモート ホストを指定する場合は、このコンストラクタを使用しないでください。

注意

SocketException が発生した場合は、SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認できます。これは MSDN から入手できます。

使用例

ホスト名とポート番号を使用して UdpClient クラスのインスタンスを作成する方法を次の例に示します。

'Creates an instance of the UdpClient class with a remote host name and a port number.
Try
   Dim udpClient As New UdpClient("www.contoso.com", 11000)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try
//Creates an instance of the UdpClient class with a remote host name and a port number.
try{
     UdpClient udpClient = new UdpClient("www.contoso.com",11000);
}
catch (Exception e ) {
           Console.WriteLine(e.ToString());
}
//Creates an instance of the UdpClient class with a remote host name and a port number.
try
{
   UdpClient^ udpClient = gcnew UdpClient( "www.contoso.com",11000 );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
    // Creates an instance of the UdpClient class 
    // with a remote host name and a port number.
    try {
        UdpClient udpClient = 
            new UdpClient("www.contoso.com", 11000);
    }
    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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

UdpClient クラス
UdpClient メンバ
System.Net.Sockets 名前空間
Send
Connect