UdpClient 建構函式

定義

初始化 UdpClient 類別的新執行個體。

多載

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 類別的新執行個體。

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 Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

此建構函式不適用於聯結多播群組,因為它不會執行套接字系結。 此外,它只適用於 IPv4 位址類型。

適用於

UdpClient(Int32)

來源:
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

您想通訊的本機通訊埠編號。

例外狀況

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 Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

此建構函式只適用於 IPv4 位址類型。

適用於

UdpClient(IPEndPoint)

來源:
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 連線的本機端點。

例外狀況

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 指定的位址系列,這些參數無法使用不同位址系列來變更或覆寫。

注意

如果您收到 SocketException,請使用 SocketException.ErrorCode 來取得特定的錯誤碼。 取得此程式代碼之後,您可以參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

另請參閱

適用於

UdpClient(AddressFamily)

來源:
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 會決定接聽程式是否使用IP第4版位址 (IPv4) 或IP第6版 (IPv6) 位址。 若要使用 IPv4 位址,請傳遞 InterNetwork 值。 若要使用 IPv6 位址,請傳遞 InterNetworkV6 值。 傳遞任何其他值會導致 方法擲 ArgumentException回 。

如果使用這個建構函式, UdpClient 實例會設定為參數所 family 指定的位址系列,這些參數無法使用不同位址系列來變更或覆寫。

注意

如果您收到 SocketException,請使用 SocketException.ErrorCode 來取得特定的錯誤碼。 取得此程式代碼之後,您可以參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

UdpClient.UdpClient(AddressFamily)不適合加入多播群組,因為它不會執行套接字系結。

適用於

UdpClient(Int32, AddressFamily)

來源:
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 值,指定通訊端的定址配置。

例外狀況

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 指定的位址系列,這些參數無法使用不同位址系列來變更或覆寫。

注意

如果您收到 SocketException,請使用 SocketException.ErrorCode 來取得特定的錯誤碼。 取得此程式代碼之後,請參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

適用於

UdpClient(String, Int32)

來源:
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

您想連接的遠端通訊埠編號。

例外狀況

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

備註

這個建構函式會初始化新的 UdpClient ,並使用 和 port 參數建立遠端主機hostname。 建立預設遠端主機是選擇性的。 如果您使用這個建構函式,則不需要在每個方法呼叫 Send 中指定遠端主機。 指定預設遠端主機只會將您限制為該主機。 您可以隨時呼叫 Connect 方法來變更預設遠端主機。 如果您想要在呼叫 Send 方法時指定遠端主機,請勿使用此建構函式。

注意

如果您收到 SocketException,請使用 SocketException.ErrorCode 來取得特定的錯誤碼。 取得此程式代碼之後,您可以參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

另請參閱

適用於