UdpClient 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 UdpClient 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| UdpClient() |
初始化 UdpClient 類別的新執行個體。 |
| UdpClient(Int32) |
初始化該類別的新實例 UdpClient ,並將其綁定到當地提供的埠號。 |
| UdpClient(IPEndPoint) |
初始化該類別的新實例 UdpClient 並將其綁定到指定的本地端點。 |
| UdpClient(AddressFamily) |
初始化 UdpClient 類別的新執行個體。 |
| UdpClient(Int32, AddressFamily) |
初始化該類別的新實例 UdpClient ,並將其綁定到當地提供的埠號。 |
| UdpClient(String, Int32) |
初始化該類別的新實例 UdpClient 並建立預設遠端主機。 |
UdpClient()
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- 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 目標的 connect 方法呼叫更改或覆寫。
Note
如果您收到 SocketException,請使用 SocketException.ErrorCode 以取得特定的錯誤代碼。 取得這些程式碼後,你可以參考 Windows Sockets 版本 2 的 API 錯誤程式碼 文件,了解錯誤的詳細說明。
此建構器不適合加入多播群組,因為它不執行套接字綁定。 而且它只支援 IPv4 位址類型。
適用於
UdpClient(Int32)
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- 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 目標的 connect 方法呼叫更改或覆寫。
Note
如果您收到 SocketException,請使用 SocketException.ErrorCode 以取得特定的錯誤代碼。 取得這些程式碼後,你可以參考 Windows Sockets 版本 2 的 API 錯誤程式碼 文件,了解錯誤的詳細說明。
此建構器僅適用於 IPv4 位址類型。
適用於
UdpClient(IPEndPoint)
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- 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下。 在呼叫這個建構器之前,你必須先建立 IPEndPoint 一個使用 IP 位址和埠號,並用來傳送和接收資料。 你不需要指定本地 IP 位址和埠號來傳送和接收資料。 如果沒有,底層服務提供商會分配最合適的本地 IP 位址和埠號。
若使用此建構子, UdpClient 實例會以參數指定的 localEP 地址族設定,該族無法被不同位址族的 connect 方法呼叫更改或覆寫。
Note
如果您收到 SocketException,請使用 SocketException.ErrorCode 以取得特定的錯誤代碼。 取得這些程式碼後,你可以參考 Windows Sockets 版本 2 的 API 錯誤程式碼 文件,了解錯誤的詳細說明。
另請參閱
適用於
UdpClient(AddressFamily)
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- 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 方法呼叫更改或覆寫。
Note
如果您收到 SocketException,請使用 SocketException.ErrorCode 以取得特定的錯誤代碼。 取得這些程式碼後,你可以參考 Windows Sockets 版本 2 的 API 錯誤程式碼 文件,了解錯誤的詳細說明。
不 UdpClient.UdpClient(AddressFamily) 適合加入多播群組,因為它不執行套接字綁定。
適用於
UdpClient(Int32, AddressFamily)
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- 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 方法呼叫更改或覆寫。
Note
如果您收到 SocketException,請使用 SocketException.ErrorCode 以取得特定的錯誤代碼。 取得此代碼後,請參考 Windows Sockets 版本 2 API 錯誤代碼文件,以獲得錯誤的詳細說明。
適用於
UdpClient(String, Int32)
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- UDPClient.cs
- 來源:
- 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 and port 參數建立遠端主機。 建立預設遠端主機是可選的。 如果你使用這個建構子,就不必在每次呼叫 Send 方法時指定遠端主機。 指定預設遠端主機會限制你只能使用該主機。 你可以隨時透過呼叫 Connect 方法來更改預設的遠端主機。 如果你想在呼叫 Send 方法時指定遠端主機,請不要使用這個建構子。
Note
如果您收到 SocketException,請使用 SocketException.ErrorCode 以取得特定的錯誤代碼。 取得這些程式碼後,你可以參考 Windows Sockets 版本 2 的 API 錯誤程式碼 文件,了解錯誤的詳細說明。