UdpClient UdpClient UdpClient UdpClient Constructors

定義

多載

UdpClient() UdpClient() UdpClient()

初始化 UdpClient 類別的新執行個體。Initializes a new instance of the UdpClient class.

UdpClient(Int32) UdpClient(Int32) UdpClient(Int32) UdpClient(Int32)

初始化 UdpClient 類別的新執行個體,並將它繫結至提供的本機通訊埠編號。Initializes a new instance of the UdpClient class and binds it to the local port number provided.

UdpClient(IPEndPoint) UdpClient(IPEndPoint) UdpClient(IPEndPoint) UdpClient(IPEndPoint)

初始化 UdpClient 類別的新執行個體,並將它繫結至指定的本機端點。Initializes a new instance of the UdpClient class and binds it to the specified local endpoint.

UdpClient(AddressFamily) UdpClient(AddressFamily) UdpClient(AddressFamily) UdpClient(AddressFamily)

初始化 UdpClient 類別的新執行個體。Initializes a new instance of the UdpClient class.

UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily)

初始化 UdpClient 類別的新執行個體,並將它繫結至提供的本機通訊埠編號。Initializes a new instance of the UdpClient class and binds it to the local port number provided.

UdpClient(String, Int32) UdpClient(String, Int32) UdpClient(String, Int32) UdpClient(String, Int32)

初始化 UdpClient 類別的新執行個體,並建立預設遠端主機。Initializes a new instance of the UdpClient class and establishes a default remote host.

UdpClient() UdpClient() UdpClient()

初始化 UdpClient 類別的新執行個體。Initializes a new instance of the UdpClient class.

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

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列範例示範如何使用無參數的函式來建立UdpClient類別的實例。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()

備註

此函式會建立UdpClient新的,並允許基礎服務提供者指派最適當的本機 IPv4 位址和埠號碼。This constructor creates a new UdpClient and allows the underlying service provider to assign the most appropriate local IPv4 address and port number. 如果使用此函式,則UdpClient會使用 IPv4 的位址系列來設定實例,而此類型無法由具有 IPv6 目標的 connect 方法呼叫加以變更或覆寫。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.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。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. 此外,它僅適用于 IPv4 網址類別型。Also, it works only with IPv4 address types.

UdpClient(Int32) UdpClient(Int32) UdpClient(Int32) UdpClient(Int32)

初始化 UdpClient 類別的新執行個體,並將它繫結至提供的本機通訊埠編號。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)

參數

port
Int32 Int32 Int32 Int32

您想通訊的本機通訊埠編號。The local port number from which you intend to communicate.

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列範例示範如何使用本機埠號碼來建立UdpClient類別的實例。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

備註

此函式會建立Socket基礎,並將其系結至您要進行通訊的埠號碼。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. 基礎服務提供者會指派本機 IP 位址。The underlying service provider will assign the local IP address. 如果您將0傳遞給此函式,基礎服務提供者將會指派通訊埠編號。If you pass 0 to the constructor, the underlying service provider will assign a port number. 如果使用此函式,則UdpClient會使用 IPv4 的位址系列來設定實例,而此類型無法由具有 IPv6 目標的 connect 方法呼叫加以變更或覆寫。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.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。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.

此函式僅適用于 IPv4 網址類別型。This constructor works only with IPv4 address types.

UdpClient(IPEndPoint) UdpClient(IPEndPoint) UdpClient(IPEndPoint) UdpClient(IPEndPoint)

初始化 UdpClient 類別的新執行個體,並將它繫結至指定的本機端點。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)

參數

localEP
IPEndPoint IPEndPoint IPEndPoint IPEndPoint

IPEndPoint,表示您要繫結 UDP 連線的本機端點。An IPEndPoint that represents the local endpoint to which you bind the UDP connection.

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列範例示範如何使用本機端點來建立UdpClient類別的實例。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

備註

此函式會建立UdpClient新的,並將IPEndPoint它系結localEP至參數所指定的。This constructor creates a new UdpClient and binds it to the IPEndPoint specified by the localEP parameter. 呼叫此函式IPEndPoint之前,您必須使用您要用來傳送和接收資料的 IP 位址和埠號碼來建立。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. 您不需要指定用來傳送和接收資料的本機 IP 位址和埠號碼。You do not need to specify a local IP address and port number for sending and receiving data. 如果不這麼做,基礎服務提供者會指派最適當的本機 IP 位址和埠號碼。If you do not, the underlying service provider will assign the most appropriate local IP address and port number.

如果使用此函式,則UdpClient會使用localEP參數所指定的位址系列來設定實例,而該參數無法透過不同位址系列的 connect 方法呼叫來變更或覆寫。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.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。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.

另請參閱

UdpClient(AddressFamily) UdpClient(AddressFamily) UdpClient(AddressFamily) UdpClient(AddressFamily)

初始化 UdpClient 類別的新執行個體。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)

參數

family
AddressFamily AddressFamily AddressFamily AddressFamily

其中一個 AddressFamily 值,指定通訊端的定址配置。One of the AddressFamily values that specifies the addressing scheme of the socket.

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

備註

family參數會決定接聽程式是使用 ip 第4版位址(IPv4)或 ip 版本6(IPv6)位址。The family parameter determines whether the listener uses an IP version 4 address (IPv4) or an IP version 6 (IPv6) address. 若要使用 IPv4 位址,請傳遞InterNetwork值。To use an IPv4 address, pass the InterNetwork value. 若要使用 IPv6 位址,請傳遞InterNetworkV6值。To use an IPv6 address, pass the InterNetworkV6 value. 傳遞任何其他值會導致方法擲ArgumentException回。Passing any other value will cause the method to throw an ArgumentException.

如果使用此函式,則UdpClient會使用family參數所指定的位址系列來設定實例,而該參數無法透過不同位址系列的 connect 方法呼叫來變更或覆寫。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.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。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.

UdpClient.UdpClient(AddressFamily)不適合加入多播群組,因為它不會執行通訊端系結。The UdpClient.UdpClient(AddressFamily) is not suitable for joining a multicast group because it does not perform socket binding.

UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily)

初始化 UdpClient 類別的新執行個體,並將它繫結至提供的本機通訊埠編號。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)

參數

port
Int32 Int32 Int32 Int32

通訊埠,將會在此處接聽連入的連接嘗試。The port on which to listen for incoming connection attempts.

family
AddressFamily AddressFamily AddressFamily AddressFamily

其中一個 AddressFamily 值,指定通訊端的定址配置。One of the AddressFamily values that specifies the addressing scheme of the socket.

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列程式碼範例示範如何建立要在多播群組中使用的 UDP 用戶端。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)

備註

此函式會建立Socket基礎,並將其系結至您要進行通訊的埠號碼。This constructor creates an underlying Socket and binds it to the port number from which you intend to communicate.

family參數會決定接聽程式是使用 ip 第4版位址(IPv4)或 ip 版本6(IPv6)位址。The family parameter determines whether the listener uses an IP version 4 address (IPv4) or an IP version 6 (IPv6) address. 若要使用 IPv4 位址,請傳遞InterNetwork值。To use an IPv4 address, pass the InterNetwork value. 若要使用 IPv6 位址,請傳遞InterNetworkV6值。To use an IPv6 address, pass the InterNetworkV6 value. 傳遞任何其他值會導致方法擲ArgumentException回。Passing any other value will cause the method to throw an ArgumentException.

如果使用此函式,則UdpClient會使用family參數所指定的位址系列來設定實例,而該參數無法透過不同位址系列的 connect 方法呼叫來變更或覆寫。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.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,請參閱Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。Once you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

UdpClient(String, Int32) UdpClient(String, Int32) UdpClient(String, Int32) UdpClient(String, Int32)

初始化 UdpClient 類別的新執行個體,並建立預設遠端主機。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)

參數

hostname
String String String String

您想連接的遠端 DNS 主機名稱。The name of the remote DNS host to which you intend to connect.

port
Int32 Int32 Int32 Int32

您想連接的遠端通訊埠編號。The remote port number to which you intend to connect.

例外狀況

存取通訊端時發生錯誤。An error occurred when accessing the socket.

範例

下列範例示範如何使用主機名稱和埠號碼來UdpClient建立類別的實例。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

備註

此函式會初始化UdpClient新的,並hostname使用和port參數來建立遠端主機。This constructor initializes a new UdpClient and establishes a remote host using the hostname and port parameters. 建立預設遠端主機是選擇性的。Establishing a default remote host is optional. 如果您使用此函式,則不需要在每次呼叫Send方法時指定遠端主機。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. 您可以隨時呼叫Connect方法來變更預設遠端主機。You can change the default remote host at any time by calling the Connect method. 如果您想要在呼叫Send方法時指定遠端主機,請勿使用這個函式。If you want to specify a remote host in your call to the Send method, do not use this constructor.

注意

如果您收到SocketException,請使用SocketException.ErrorCode來取得特定的錯誤碼。If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. 取得此程式碼之後,您可以參考Windows socket 第2版 API 錯誤碼檔,以取得錯誤的詳細描述。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.

另請參閱

適用於