UdpClient 构造函数

定义

初始化 UdpClient 类的新实例。

重载

UdpClient()

初始化 UdpClient 类的新实例。

UdpClient(Int32)

初始化 UdpClient 类的新实例,并将它绑定到所提供的本地端口号。

UdpClient(IPEndPoint)

初始化 UdpClient 类的新实例,并将其绑定到指定的本地终结点。

UdpClient(AddressFamily)

初始化 UdpClient 类的新实例。

UdpClient(Int32, AddressFamily)

初始化 UdpClient 类的新实例,并将它绑定到所提供的本地端口号。

UdpClient(String, Int32)

初始化 UdpClient 类的新实例,并建立默认远程主机。

UdpClient()

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

初始化 UdpClient 类的新实例。

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

例外

访问套接字时出错。

示例

下面的示例演示如何使用无参数构造函数创建 类的 UdpClient 实例。

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

注解

此构造函数创建新的 UdpClient ,并允许基础服务提供程序分配最合适的本地 IPv4 地址和端口号。 如果使用此构造函数,则会 UdpClient 使用 IPv4 地址系列设置实例,该地址系列不能由具有 IPv6 目标的连接方法调用更改或覆盖。

备注

如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

此构造函数不适合联接多播组,因为它不执行套接字绑定。 此外,它仅适用于 IPv4 地址类型。

适用于

UdpClient(Int32)

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

初始化 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 = 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

注解

此构造函数创建基础 Socket 并将其绑定到要从中进行通信的端口号。 如果只想设置本地端口号,请使用此构造函数。 基础服务提供程序将分配本地 IP 地址。 如果将 0 传递给构造函数,基础服务提供程序将分配端口号。 如果使用此构造函数,则会 UdpClient 使用 IPv4 地址系列设置实例,该地址系列不能由具有 IPv6 目标的连接方法调用更改或覆盖。

备注

如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

此构造函数仅适用于 IPv4 地址类型。

适用于

UdpClient(IPEndPoint)

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

初始化 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

一个 IPEndPoint,表示将 UDP 连接绑定到的本地终结点。

例外

localEPnull

访问套接字时出错。

示例

以下示例演示如何使用本地终结点创建 类的 UdpClient 实例。

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

注解

此构造函数创建一个新的 UdpClient 并将其绑定到 IPEndPoint 参数指定的 localEP 。 在调用此构造函数之前,必须使用要从中发送和接收数据的 IP 地址和端口号创建 IPEndPoint 。 无需指定本地 IP 地址和端口号即可发送和接收数据。 否则,基础服务提供商将分配最合适的本地 IP 地址和端口号。

如果使用此构造函数,则 UdpClient 使用 参数指定的 localEP 地址系列设置实例,该地址系列不能由具有不同地址系列的 connect 方法调用更改或覆盖。

备注

如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

另请参阅

适用于

UdpClient(AddressFamily)

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

初始化 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 值之一,它指定套接字的寻址方案。

例外

访问套接字时出错。

注解

参数 family 确定侦听器是使用 IP 版本 4 地址 (IPv4) 还是使用 IP 版本 6 (IPv6) 地址。 若要使用 IPv4 地址,请 InterNetwork 传递 值。 若要使用 IPv6 地址,请 InterNetworkV6 传递 值。 传递任何其他值将导致 方法引发 ArgumentException

如果使用此构造函数,则 UdpClient 使用 参数指定的 family 地址系列设置实例,该地址系列不能由具有不同地址系列的 connect 方法调用更改或覆盖。

备注

如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

UdpClient.UdpClient(AddressFamily)不适合加入多播组,因为它不执行套接字绑定。

适用于

UdpClient(Int32, AddressFamily)

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

初始化 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 值之一,它指定套接字的寻址方案。

例外

port 大于 MaxPort 或小于 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 = 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)

注解

此构造函数创建基础 Socket 并将其绑定到要从中进行通信的端口号。

参数 family 确定侦听器是使用 IP 版本 4 地址 (IPv4) 还是使用 IP 版本 6 (IPv6) 地址。 若要使用 IPv4 地址,请 InterNetwork 传递 值。 若要使用 IPv6 地址,请 InterNetworkV6 传递 值。 传递任何其他值将导致 方法引发 ArgumentException

如果使用此构造函数,则 UdpClient 使用 参数指定的 family 地址系列设置实例,该地址系列不能由具有不同地址系列的 connect 方法调用更改或覆盖。

备注

如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,请参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

适用于

UdpClient(String, Int32)

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

初始化 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

port 不在 MinPortMaxPort 之间。

访问套接字时出错。

示例

以下示例演示如何使用主机名和端口号创建 类的 UdpClient 实例。

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

注解

此构造函数初始化hostname新的 UdpClient 并使用 和 port 参数建立远程主机。 建立默认远程主机是可选的。 如果使用此构造函数,则不必在每次调用 Send 方法时指定远程主机。 指定默认远程主机仅限于该主机。 可以随时通过调用 Connect 方法更改默认远程主机。 如果要在调用 Send 方法时指定远程主机,请不要使用此构造函数。

备注

如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

另请参阅

适用于