UdpClient 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 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
- 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 = 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
- 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
要从中通信的本地端口号。
例外
访问套接字时出错。
示例
以下示例演示如何使用本地端口号创建类的 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 目标的连接方法调用更改或覆盖。
注释
如果收到, SocketException则用于 SocketException.ErrorCode 获取特定错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。
此构造函数仅适用于 IPv4 地址类型。
适用于
UdpClient(IPEndPoint)
- Source:
- UDPClient.cs
- Source:
- UDPClient.cs
- 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 连接的本地终结点。
例外
localEP 是 null。
访问套接字时出错。
示例
以下示例演示如何使用本地终结点创建类的 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 构造函数,并将其 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
- 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 不是 InterNetwork 或 InterNetworkV6。
访问套接字时出错。
注解
该 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
- 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指定套接字寻址方案的值之一。
例外
family 不是 InterNetwork 或 InterNetworkV6。
访问套接字时出错。
示例
下面的代码示例演示如何创建 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 使用参数指定的 family 地址系列设置实例,该参数不能更改或覆盖具有不同地址系列的 connect 方法调用。
注释
如果收到, SocketException则用于 SocketException.ErrorCode 获取特定错误代码。 获取此代码后,请参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误详细说明。
适用于
UdpClient(String, Int32)
- Source:
- UDPClient.cs
- Source:
- UDPClient.cs
- 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
要连接到的远程端口号。
例外
hostname 是 null。
访问套接字时出错。
示例
以下示例演示如何使用主机名和端口号创建类的 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 方法时指定远程主机,请不要使用此构造函数。
注释
如果收到, SocketException则用于 SocketException.ErrorCode 获取特定错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。