Редагувати

Поділитися через


UdpClient Constructors

Definition

Initializes a new instance of the UdpClient class.

Overloads

UdpClient()

Initializes a new instance of the UdpClient class.

UdpClient(Int32)

Initializes a new instance of the UdpClient class and binds it to the local port number provided.

UdpClient(IPEndPoint)

Initializes a new instance of the UdpClient class and binds it to the specified local endpoint.

UdpClient(AddressFamily)

Initializes a new instance of the UdpClient class.

UdpClient(Int32, AddressFamily)

Initializes a new instance of the UdpClient class and binds it to the local port number provided.

UdpClient(String, Int32)

Initializes a new instance of the UdpClient class and establishes a default remote host.

UdpClient()

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Initializes a new instance of the UdpClient class.

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

Exceptions

An error occurred when accessing the socket.

Examples

The following example demonstrates how to use the parameterless constructor to create an instance of the UdpClient class.

//Creates an instance of the UdpClient class using the default constructor.
UdpClient^ udpClient = gcnew 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()

Remarks

This constructor creates a new UdpClient and allows the underlying service provider to assign the most appropriate local IPv4 address and port number. If this constructor is used, the UdpClient instance is set with an address family of IPv4 that cannot be changed or overwritten by a connect method call with an IPv6 target.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

This constructor is not suitable for joining a multicast group because it does not perform socket binding. Also, it works only with IPv4 address types.

Applies to

UdpClient(Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Initializes a new instance of the UdpClient class and binds it to the local port number provided.

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

Parameters

port
Int32

The local port number from which you intend to communicate.

Exceptions

The port parameter is greater than MaxPort or less than MinPort.

An error occurred when accessing the socket.

Examples

The following example demonstrates using a local port number to create an instance of the UdpClient class.

//Creates an instance of the UdpClient class to listen on
// the default interface using a particular port.
try
{
   UdpClient^ udpClient = gcnew 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{
         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

Remarks

This constructor creates an underlying Socket and binds it to the port number from which you intend to communicate. Use this constructor if you are only interested in setting the local port number. The underlying service provider will assign the local IP address. If you pass 0 to the constructor, the underlying service provider will assign a port number. If this constructor is used, the UdpClient instance is set with an address family of IPv4 that cannot be changed or overwritten by a connect method call with an IPv6 target.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

This constructor works only with IPv4 address types.

Applies to

UdpClient(IPEndPoint)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Initializes a new instance of the UdpClient class and binds it to the specified local endpoint.

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)

Parameters

localEP
IPEndPoint

An IPEndPoint that represents the local endpoint to which you bind the UDP connection.

Exceptions

localEP is null.

An error occurred when accessing the socket.

Examples

The following example demonstrates how to create an instance of the UdpClient class using a local endpoint.

//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()).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

Remarks

This constructor creates a new UdpClient and binds it to the IPEndPoint specified by the localEP parameter. Before you call this constructor, you must create an IPEndPoint using the IP address and port number from which you intend to send and receive data. You do not need to specify a local IP address and port number for sending and receiving data. If you do not, the underlying service provider will assign the most appropriate local IP address and port number.

If this constructor is used, the UdpClient instance is set with the address family specified by the localEP parameter that cannot be changed or overwritten by a connect method call with a different address family.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

See also

Applies to

UdpClient(AddressFamily)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Initializes a new instance of the UdpClient class.

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)

Parameters

family
AddressFamily

One of the AddressFamily values that specifies the addressing scheme of the socket.

Exceptions

An error occurred when accessing the socket.

Remarks

The family parameter determines whether the listener uses an IP version 4 address (IPv4) or an IP version 6 (IPv6) address. To use an IPv4 address, pass the InterNetwork value. To use an IPv6 address, pass the InterNetworkV6 value. Passing any other value will cause the method to throw an ArgumentException.

If this constructor is used, the UdpClient instance is set with the address family specified by the family parameter that cannot be changed or overwritten by a connect method call with a different address family.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

The UdpClient.UdpClient(AddressFamily) is not suitable for joining a multicast group because it does not perform socket binding.

Applies to

UdpClient(Int32, AddressFamily)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Initializes a new instance of the UdpClient class and binds it to the local port number provided.

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)

Parameters

port
Int32

The port on which to listen for incoming connection attempts.

family
AddressFamily

One of the AddressFamily values that specifies the addressing scheme of the socket.

Exceptions

port is greater than MaxPort or less than MinPort.

An error occurred when accessing the socket.

Examples

The following code example shows how to create a UDP client to use in a multicast group.

// 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.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)

Remarks

This constructor creates an underlying Socket and binds it to the port number from which you intend to communicate.

The family parameter determines whether the listener uses an IP version 4 address (IPv4) or an IP version 6 (IPv6) address. To use an IPv4 address, pass the InterNetwork value. To use an IPv6 address, pass the InterNetworkV6 value. Passing any other value will cause the method to throw an ArgumentException.

If this constructor is used, the UdpClient instance is set with the address family specified by the family parameter that cannot be changed or overwritten by a connect method call with a different address family.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Applies to

UdpClient(String, Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Initializes a new instance of the UdpClient class and establishes a default remote host.

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)

Parameters

hostname
String

The name of the remote DNS host to which you intend to connect.

port
Int32

The remote port number to which you intend to connect.

Exceptions

hostname is null.

port is not between MinPort and MaxPort.

An error occurred when accessing the socket.

Examples

The following example demonstrates how to create an instance of the UdpClient class using a host name and port number.

//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 (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

Remarks

This constructor initializes a new UdpClient and establishes a remote host using the hostname and port parameters. Establishing a default remote host is optional. If you use this constructor, you do not have to specify a remote host in each call to the Send method. Specifying a default remote host limits you to that host only. You can change the default remote host at any time by calling the Connect method. If you want to specify a remote host in your call to the Send method, do not use this constructor.

Note

If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

See also

Applies to