UdpClient コンストラクター

定義

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

オーバーロード

名前 説明
UdpClient()

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

UdpClient(Int32)

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

UdpClient(IPEndPoint)

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

UdpClient(AddressFamily)

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

UdpClient(Int32, AddressFamily)

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

UdpClient(String, Int32)

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

UdpClient()

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

public:
 UdpClient();
public UdpClient();
Public Sub New ()

例外

ソケットにアクセスするときにエラーが発生しました。

次の例では、パラメーターなしのコンストラクターを使用して、 UdpClient クラスのインスタンスを作成する方法を示します。

//Creates an instance of the UdpClient class using the default constructor.
UdpClient udpClient = new UdpClient();
'Creates an instance of the UdpClient class using the default constructor.
Dim udpClient As New UdpClient()

注釈

このコンストラクターは、新しい UdpClient を作成し、基になるサービス プロバイダーが最も適切なローカル IPv4 アドレスとポート番号を割り当てできるようにします。 このコンストラクターを使用する場合、 UdpClient インスタンスは IPv4 のアドレス ファミリで設定され、IPv6 ターゲットを使用した connect メソッド呼び出しで変更または上書きすることはできません。

Note

SocketExceptionを受け取った場合は、SocketException.ErrorCodeを使用して特定のエラー コードを取得します。 このコードを取得したら、エラーの詳細な説明については、 Windows ソケット バージョン 2 API のエラー コード ドキュメントを参照してください。

このコンストラクターは、ソケット バインドを実行しないため、マルチキャスト グループの結合には適していません。 また、IPv4 アドレスの種類でのみ機能します。

適用対象

UdpClient(Int32)

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

public:
 UdpClient(int port);
public UdpClient(int port);
new System.Net.Sockets.UdpClient : int -> System.Net.Sockets.UdpClient
Public Sub New (port As Integer)

パラメーター

port
Int32

通信元のローカル ポート番号。

例外

port パラメーターが MaxPort より大きいか、MinPort未満です。

ソケットにアクセスするときにエラーが発生しました。

次の例では、ローカル ポート番号を使用して、 UdpClient クラスのインスタンスを作成する方法を示します。

//Creates an instance of the UdpClient class to listen on
// the default interface using a particular port.
try{
         UdpClient udpClient = new UdpClient(11000);
}
catch (Exception e ) {
          Console.WriteLine(e.ToString());
  }
'Creates an instance of the UdpClient class to listen on 
'the default interface using a particular port.
Try
   Dim udpClient As New UdpClient(11000)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

注釈

このコンストラクターは、基になる Socket を作成し、通信元のポート番号にバインドします。 ローカル ポート番号の設定のみに関心がある場合は、このコンストラクターを使用します。 基になるサービス プロバイダーは、ローカル IP アドレスを割り当てます。 コンストラクターに 0 を渡すと、基になるサービス プロバイダーによってポート番号が割り当てられます。 このコンストラクターを使用する場合、 UdpClient インスタンスは IPv4 のアドレス ファミリで設定され、IPv6 ターゲットを使用した connect メソッド呼び出しで変更または上書きすることはできません。

Note

SocketExceptionを受け取った場合は、SocketException.ErrorCodeを使用して特定のエラー コードを取得します。 このコードを取得したら、エラーの詳細な説明については、 Windows ソケット バージョン 2 API のエラー コード ドキュメントを参照してください。

このコンストラクターは、IPv4 アドレスの種類でのみ機能します。

適用対象

UdpClient(IPEndPoint)

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

public:
 UdpClient(System::Net::IPEndPoint ^ localEP);
public UdpClient(System.Net.IPEndPoint localEP);
new System.Net.Sockets.UdpClient : System.Net.IPEndPoint -> System.Net.Sockets.UdpClient
Public Sub New (localEP As IPEndPoint)

パラメーター

localEP
IPEndPoint

UDP 接続をバインドするローカル エンドポイントを表す IPEndPoint

例外

localEPnullです。

ソケットにアクセスするときにエラーが発生しました。

次の例では、ローカル エンドポイントを使用して UdpClient クラスのインスタンスを作成する方法を示します。


//Creates an instance of the UdpClient class using a local endpoint.
 IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
 IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);

try{
     UdpClient udpClient = new UdpClient(ipLocalEndPoint);
}
catch (Exception e ) {
           Console.WriteLine(e.ToString());
}
'Creates an instance of the UdpClient class using a local endpoint.
Dim ipAddress As IPAddress = Dns.Resolve(Dns.GetHostName()).AddressList(0)
Dim ipLocalEndPoint As New IPEndPoint(ipAddress, 11000)

Try
   Dim udpClient As New UdpClient(ipLocalEndPoint)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

注釈

このコンストラクターは、新しいUdpClientを作成し、localEP パラメーターで指定されたIPEndPointにバインドします。 このコンストラクターを呼び出す前に、データの送受信元となる IP アドレスとポート番号を使用して IPEndPoint を作成する必要があります。 データの送受信にローカル IP アドレスとポート番号を指定する必要はありません。 そうしないと、基になるサービス プロバイダーによって、最も適切なローカル IP アドレスとポート番号が割り当てられます。

このコンストラクターを使用する場合、 UdpClient インスタンスは、別のアドレス ファミリを持つ connect メソッド呼び出しで変更または上書きできない localEP パラメーターで指定されたアドレス ファミリで設定されます。

Note

SocketExceptionを受け取った場合は、SocketException.ErrorCodeを使用して特定のエラー コードを取得します。 このコードを取得したら、エラーの詳細な説明については、 Windows ソケット バージョン 2 API のエラー コード ドキュメントを参照してください。

こちらもご覧ください

適用対象

UdpClient(AddressFamily)

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

public:
 UdpClient(System::Net::Sockets::AddressFamily family);
public UdpClient(System.Net.Sockets.AddressFamily family);
new System.Net.Sockets.UdpClient : System.Net.Sockets.AddressFamily -> System.Net.Sockets.UdpClient
Public Sub New (family As AddressFamily)

パラメーター

family
AddressFamily

ソケットのアドレス指定スキームを指定する AddressFamily 値の 1 つ。

例外

familyInterNetwork または InterNetworkV6ではありません。

ソケットにアクセスするときにエラーが発生しました。

注釈

family パラメーターは、リスナーが IP バージョン 4 アドレス (IPv4) アドレスと IP バージョン 6 (IPv6) アドレスのどちらを使用するかを決定します。 IPv4 アドレスを使用するには、 InterNetwork 値を渡します。 IPv6 アドレスを使用するには、 InterNetworkV6 値を渡します。 その他の値を渡すと、メソッドは ArgumentExceptionをスローします。

このコンストラクターを使用する場合、 UdpClient インスタンスは、別のアドレス ファミリを持つ connect メソッド呼び出しで変更または上書きできない family パラメーターで指定されたアドレス ファミリで設定されます。

Note

SocketExceptionを受け取った場合は、SocketException.ErrorCodeを使用して特定のエラー コードを取得します。 このコードを取得したら、エラーの詳細な説明については、 Windows ソケット バージョン 2 API のエラー コード ドキュメントを参照してください。

UdpClient.UdpClient(AddressFamily)は、ソケット バインドを実行しないため、マルチキャスト グループへの参加には適していません。

適用対象

UdpClient(Int32, AddressFamily)

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

public:
 UdpClient(int port, System::Net::Sockets::AddressFamily family);
public UdpClient(int port, System.Net.Sockets.AddressFamily family);
new System.Net.Sockets.UdpClient : int * System.Net.Sockets.AddressFamily -> System.Net.Sockets.UdpClient
Public Sub New (port As Integer, family As AddressFamily)

パラメーター

port
Int32

着信接続の試行をリッスンするポート。

family
AddressFamily

ソケットのアドレス指定スキームを指定する AddressFamily 値の 1 つ。

例外

familyInterNetwork または InterNetworkV6ではありません。

portMaxPort より大きいか、 MinPort未満です。

ソケットにアクセスするときにエラーが発生しました。

次のコード例は、マルチキャスト グループで使用する UDP クライアントを作成する方法を示しています。


// Bind and listen on port 2000. This constructor creates a socket
// and binds it to the port on which to receive data. The family
// parameter specifies that this connection uses an IPv6 address.
clientOriginator = new UdpClient(2000, AddressFamily.InterNetworkV6);

// Join or create a multicast group. The multicast address ranges
// to use are specified in RFC#2375. You are free to use
// different addresses.

// Transform the string address into the internal format.
m_GrpAddr = IPAddress.Parse("FF01::1");

// Display the multicast address used.
Console.WriteLine("Multicast Address: [" + m_GrpAddr.ToString() + "]");

// Exercise the use of the IPv6MulticastOption.
Console.WriteLine("Instantiate IPv6MulticastOption(IPAddress)");

// Instantiate IPv6MulticastOption using one of the
// overloaded constructors.
IPv6MulticastOption ipv6MulticastOption = new IPv6MulticastOption(m_GrpAddr);

// Store the IPAdress multicast options.
IPAddress group =  ipv6MulticastOption.Group;
long interfaceIndex = ipv6MulticastOption.InterfaceIndex;

// Display IPv6MulticastOption properties.
Console.WriteLine("IPv6MulticastOption.Group: [" + group  + "]");
Console.WriteLine("IPv6MulticastOption.InterfaceIndex: [" + interfaceIndex + "]");



// Instantiate IPv6MulticastOption using another
// overloaded constructor.
IPv6MulticastOption ipv6MulticastOption2 = new IPv6MulticastOption(group, interfaceIndex);

// Store the IPAdress multicast options.
group =  ipv6MulticastOption2.Group;
interfaceIndex = ipv6MulticastOption2.InterfaceIndex;

// Display the IPv6MulticastOption2 properties.
Console.WriteLine("IPv6MulticastOption.Group: [" + group  + "]");
Console.WriteLine("IPv6MulticastOption.InterfaceIndex: [" + interfaceIndex + "]");

// Join the specified multicast group using one of the
// JoinMulticastGroup overloaded methods.
clientOriginator.JoinMulticastGroup((int)interfaceIndex, group);


// Define the endpoint data port. Note that this port number
// must match the ClientTarget UDP port number which is the
// port on which the ClientTarget is receiving data.
m_ClientTargetdest = new IPEndPoint(m_GrpAddr, 1000);
' Bind and listen on port 2000. This constructor creates a socket 
' and binds it to the port on which to receive data. The family 
' parameter specifies that this connection uses an IPv6 address.
clientOriginator = New UdpClient(2000, AddressFamily.InterNetworkV6)

' Join or create a multicast group. The multicast address ranges 
' to use are specified in RFC#2375. You are free to use 
' different addresses.
' Transform the string address into the internal format.
m_GrpAddr = IPAddress.Parse("FF01::1")

' Display the multicast address used.
Console.WriteLine(("Multicast Address: [" + m_GrpAddr.ToString() + "]"))

' Exercise the use of the IPv6MulticastOption.
Console.WriteLine("Instantiate IPv6MulticastOption(IPAddress)")

' Instantiate IPv6MulticastOption using one of the 
' overloaded constructors.
Dim ipv6MulticastOption As New IPv6MulticastOption(m_GrpAddr)

' Store the IPAdress multicast options.
Dim group As IPAddress = ipv6MulticastOption.Group
Dim interfaceIndex As Long = ipv6MulticastOption.InterfaceIndex

' Display IPv6MulticastOption properties.
Console.WriteLine(("IPv6MulticastOption.Group: [" + group.ToString() + "]"))
Console.WriteLine(("IPv6MulticastOption.InterfaceIndex: [" + interfaceIndex.ToString() + "]"))

' Instantiate IPv6MulticastOption using another 
' overloaded constructor.
Dim ipv6MulticastOption2 As New IPv6MulticastOption(group, interfaceIndex)

' Store the IPAdress multicast options.
group = ipv6MulticastOption2.Group
interfaceIndex = ipv6MulticastOption2.InterfaceIndex

' Display the IPv6MulticastOption2 properties.
Console.WriteLine(("IPv6MulticastOption.Group: [" + group.ToString() + "]"))
Console.WriteLine(("IPv6MulticastOption.InterfaceIndex: [" + interfaceIndex.ToString() + "]"))

' Join the specified multicast group using one of the 
' JoinMulticastGroup overloaded methods.
clientOriginator.JoinMulticastGroup(Fix(interfaceIndex), group)

' Define the endpoint data port. Note that this port number
' must match the ClientTarget UDP port number which is the
' port on which the ClientTarget is receiving data.
m_ClientTargetdest = New IPEndPoint(m_GrpAddr, 1000)

注釈

このコンストラクターは、基になる Socket を作成し、通信元のポート番号にバインドします。

family パラメーターは、リスナーが IP バージョン 4 アドレス (IPv4) アドレスと IP バージョン 6 (IPv6) アドレスのどちらを使用するかを決定します。 IPv4 アドレスを使用するには、 InterNetwork 値を渡します。 IPv6 アドレスを使用するには、 InterNetworkV6 値を渡します。 その他の値を渡すと、メソッドは ArgumentExceptionをスローします。

このコンストラクターを使用する場合、 UdpClient インスタンスは、別のアドレス ファミリを持つ connect メソッド呼び出しで変更または上書きできない family パラメーターで指定されたアドレス ファミリで設定されます。

Note

SocketExceptionを受け取った場合は、SocketException.ErrorCodeを使用して特定のエラー コードを取得します。 このコードを取得したら、エラーの詳細な説明については、Windows Sockets バージョン 2 の API エラー コード ドキュメントを参照してください。

適用対象

UdpClient(String, Int32)

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

public:
 UdpClient(System::String ^ hostname, int port);
public UdpClient(string hostname, int port);
new System.Net.Sockets.UdpClient : string * int -> System.Net.Sockets.UdpClient
Public Sub New (hostname As String, port As Integer)

パラメーター

hostname
String

接続先のリモート DNS ホストの名前。

port
Int32

接続先のリモート ポート番号。

例外

hostnamenullです。

portMinPortMaxPortの間ではありません。

ソケットにアクセスするときにエラーが発生しました。

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


//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
   Dim udpClient As New UdpClient("www.contoso.com", 11000)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

注釈

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

Note

SocketExceptionを受け取った場合は、SocketException.ErrorCodeを使用して特定のエラー コードを取得します。 このコードを取得したら、エラーの詳細な説明については、 Windows ソケット バージョン 2 API のエラー コード ドキュメントを参照してください。

こちらもご覧ください

適用対象