UdpClient コンストラクタ ()
アセンブリ: System (system.dll 内)
構文
例外
解説
このコンストラクタは、新しい UdpClient を作成し、基になるサービス プロバイダが最も適切なローカル IPv4 アドレスおよびポート番号を割り当てられるようにします。
メモ |
---|
SocketException が発生した場合は、SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認できます。これは MSDN から入手できます。 |
このコンストラクタはマルチキャスト グループへの参加には適していません。ソケット バインディングを実行しないからです。なお、このコンストラクタは、IPv4 アドレス形式でだけ使用できます。
既定のコンストラクタを使用して UdpClient クラスのインスタンスを作成する方法を次の例に示します。
'Creates an instance of the UdpClient class using the default constructor. Dim udpClient As New UdpClient()
//Creates an instance of the UdpClient class using the default constructor. UdpClient udpClient = new UdpClient();
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
UdpClient コンストラクタ (Int32, AddressFamily)
アセンブリ: System (system.dll 内)
構文
例外
このコンストラクタは、基になる Socket を作成し、通信を開始するポート番号にバインドします。
family パラメータは、リスナが IPv4 (IP Version 4) アドレスと IPv6 (IP Version 6) アドレスのどちらを使用するかを決定します。IPv4 アドレスを使用するには、InterNetwork 値を渡します。IPv6 アドレスを使用するには、InterNetworkV6 値を渡します。その他の値を渡すと、メソッドは ArgumentException をスローします。
メモ |
---|
SocketException が発生した場合は、SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認してください。これは MSDN ライブラリから入手できます。 |
マルチキャスト グループで使用する 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. 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)
// 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 = gcnew 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: [ {0}]", m_GrpAddr ); // Exercise the use of the IPv6MulticastOption. Console::WriteLine( "Instantiate IPv6MulticastOption(IPAddress)" ); // Instantiate IPv6MulticastOption using one of the // overloaded constructors. IPv6MulticastOption^ ipv6MulticastOption = gcnew IPv6MulticastOption( m_GrpAddr ); // Store the IPAdress multicast options. IPAddress^ group = ipv6MulticastOption->Group; __int64 interfaceIndex = ipv6MulticastOption->InterfaceIndex; // Display IPv6MulticastOption properties. Console::WriteLine( "IPv6MulticastOption::Group: [ {0}]", group ); Console::WriteLine( "IPv6MulticastOption::InterfaceIndex: [ {0}]", interfaceIndex ); // Instantiate IPv6MulticastOption using another // overloaded constructor. IPv6MulticastOption^ ipv6MulticastOption2 = gcnew IPv6MulticastOption( group,interfaceIndex ); // Store the IPAdress multicast options. group = ipv6MulticastOption2->Group; interfaceIndex = ipv6MulticastOption2->InterfaceIndex; // Display the IPv6MulticastOption2 properties. Console::WriteLine( "IPv6MulticastOption::Group: [ {0} ]", group ); Console::WriteLine( "IPv6MulticastOption::InterfaceIndex: [ {0} ]", 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 = gcnew 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. IPv6MulticastOption ipv6MulticastOption = new IPv6MulticastOption(m_GrpAddr); // Store the IPAdress multicast options. IPAddress group = ipv6MulticastOption.get_Group(); long interfaceIndex = ipv6MulticastOption.get_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.get_Group(); interfaceIndex = ipv6MulticastOption2.get_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);
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
UdpClient コンストラクタ (Int32)
アセンブリ: System (system.dll 内)
構文
例外
このコンストラクタは、基になる Socket を作成し、通信を開始するポート番号にバインドします。このコンストラクタは、ローカル ポート番号を設定する場合にだけ使用します。ローカル IP アドレスは、基になるサービス プロバイダによって割り当てられます。コンストラクタに 0 を渡すと、基になるサービス プロバイダによってポート番号が割り当てられます。
メモ |
---|
SocketException が発生した場合は、SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認できます。これは MSDN から入手できます。 |
ローカル ポート番号を使用して UdpClient クラスのインスタンスを作成する方法を次の例に示します。
'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
//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()); }
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
UdpClient コンストラクタ (String, Int32)
アセンブリ: System (system.dll 内)
構文
例外
このコンストラクタは新しい 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()); }
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
UdpClient コンストラクタ (AddressFamily)
アセンブリ: System (system.dll 内)
構文
例外
family パラメータは、リスナが IPv4 (IP Version 4) アドレスと IPv6 (IP Version 6) アドレスのどちらを使用するかを決定します。IPv4 アドレスを使用するには、InterNetwork 値を渡します。IPv6 アドレスを使用するには、InterNetworkV6 値を渡します。その他の値を渡すと、メソッドは ArgumentException をスローします。
メモ |
---|
SocketException が発生した場合は、SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認できます。これは MSDN から入手できます。 |
System.Net.Sockets.UdpClient(AddressFamily) はマルチキャスト グループへの参加には適していません。ソケット バインディングを実行しないからです。
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
UdpClient コンストラクタ (IPEndPoint)
アセンブリ: System (system.dll 内)
構文
例外
このコンストラクタは、新しい UdpClient を作成し、これを localEP パラメータで指定した IPEndPoint にバインドします。このコンストラクタを呼び出す前に、データの送受信を開始する IP アドレスおよびポート番号を使用して IPEndPoint を作成する必要があります。データを送受信するローカル IP アドレスおよびポート番号を指定する必要はありません。指定しない場合は、基になるサービス プロバイダが最も適切なローカル IP アドレスとポート番号を割り当てます。
メモ |
---|
SocketException が発生した場合は、SocketException.ErrorCode を使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認できます。これは MSDN から入手できます。 |
ローカル エンドポイントを使用して UdpClient クラスのインスタンスを作成する方法を次の例に示します。
'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
//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. IPAddress^ ipAddress = Dns::Resolve( Dns::GetHostName() )->AddressList[ 0 ]; IPEndPoint^ ipLocalEndPoint = gcnew IPEndPoint( ipAddress,11000 ); try { UdpClient^ udpClient = gcnew UdpClient( ipLocalEndPoint ); } catch ( Exception^ e ) { Console::WriteLine( e->ToString() ); }
// Creates an instance of the UdpClient // class using a local endpoint. IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).get_AddressList()[0]; IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000); try { UdpClient udpClient = new UdpClient(ipLocalEndPoint); } 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
UdpClient コンストラクタ
オーバーロードの一覧
名前 | 説明 |
---|---|
UdpClient () | UdpClient クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
UdpClient (AddressFamily) | UdpClient クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
UdpClient (Int32) | UdpClient クラスの新しいインスタンスを初期化し、指定したローカル ポート番号にバインドします。 .NET Compact Framework によってサポートされています。 |
UdpClient (IPEndPoint) | UdpClient クラスの新しいインスタンスを初期化し、指定したローカル エンドポイントにバインドします。 .NET Compact Framework によってサポートされています。 |
UdpClient (Int32, AddressFamily) | UdpClient クラスの新しいインスタンスを初期化し、指定したローカル ポート番号にバインドします。 .NET Compact Framework によってサポートされています。 |
UdpClient (String, Int32) | UdpClient クラスの新しいインスタンスを初期化し、既定のリモート ホストを確立します。 .NET Compact Framework によってサポートされています。 |
- UdpClient コンストラクタ ()のページへのリンク