Freigeben über


UdpClient-Konstruktor (Int32, AddressFamily)

Initialisiert eine neue Instanz der UdpClient-Klasse und bindet sie an die angegebene lokale Anschlussnummer.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

Syntax

'Declaration
Public Sub New ( _
    port As Integer, _
    family As AddressFamily _
)
'Usage
Dim port As Integer
Dim family As AddressFamily

Dim instance As New UdpClient(port, family)
public UdpClient (
    int port,
    AddressFamily family
)
public:
UdpClient (
    int port, 
    AddressFamily family
)
public UdpClient (
    int port, 
    AddressFamily family
)
public function UdpClient (
    port : int, 
    family : AddressFamily
)

Parameter

  • port
    Der Anschluss, der auf eingehende Verbindungsversuche überwacht werden soll.
  • family
    Einer der AddressFamily-Werte, der das Adressierungsschema für den Socket angibt.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentException

family ist nicht InterNetwork oder InterNetworkV6.

ArgumentOutOfRangeException

port ist größer als MaxPort oder kleiner als MinPort.

SocketException

Fehler beim Zugriff auf den Socket. Weitere Informationen finden Sie im Abschnitt "Hinweise".

Hinweise

Dieser Konstruktor erstellt einen zugrunde liegenden Socket und bindet ihn an die Anschlussnummer, von der aus Sie kommunizieren möchten.

Mithilfe des family-Parameters wird bestimmt, ob der Listener eine Adresse der IP-Version 4 (IPv4) oder eine Adresse der IP-Version (IPv6) verwendet. Wenn eine IPv4-Adresse verwendet werden soll, übergeben Sie den InterNetwork-Wert. Wenn eine IPv6-Adresse verwendet werden soll, übergeben Sie den InterNetworkV6-Wert. Bei Übergabe eines anderen Werts löst die Methode eine ArgumentException aus.

Hinweis

Wenn Sie eine SocketException erhalten, können Sie mit SocketException.ErrorCode den spezifischen Fehlercode abrufen. Nachdem Sie diesen Code abgerufen haben, finden Sie in MSDN in der Dokumentation zu API-Fehlercodes unter Windows Sockets, Version 2, eine ausführliche Beschreibung des Fehlers.

Beispiel

Im folgenden Codebeispiel wird gezeigt, wie ein UDP-Client zur Verwendung in einer Multicastgruppe erstellt wird.

' 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);

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

UdpClient-Klasse
UdpClient-Member
System.Net.Sockets-Namespace