UdpClient 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供使用者資料包通訊協定 (UDP) 網路服務。
public ref class UdpClient : IDisposable
public class UdpClient : IDisposable
type UdpClient = class
interface IDisposable
Public Class UdpClient
Implements IDisposable
- 繼承
-
UdpClient
- 實作
範例
下列範例會使用埠 11000 上的主機名www.contoso.com
建立UdpClient連線。 小型字串訊息會傳送至兩個不同的遠端主電腦。 方法 Receive 會封鎖執行,直到收到訊息為止。
IPEndPoint使用 傳遞至 Receive的 ,就會顯示回應主機的身分識別。
// With this constructor the local port number is arbitrarily assigned.
UdpClient^ udpClient = gcnew UdpClient;
try
{
udpClient->Connect( "host.contoso.com", 11000 );
// Send message to the host to which you have connected.
array<Byte>^sendBytes = Encoding::ASCII->GetBytes( "Is anybody there?" );
udpClient->Send( sendBytes, sendBytes->Length );
// Send message to a different host using optional hostname and port parameters.
UdpClient^ udpClientB = gcnew UdpClient;
udpClientB->Send( sendBytes, sendBytes->Length, "AlternateHostMachineName", 11000 );
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint( IPAddress::Any,0 );
// Block until a message returns on this socket from a remote host.
array<Byte>^receiveBytes = udpClient->Receive( RemoteIpEndPoint );
String^ returnData = Encoding::ASCII->GetString( receiveBytes );
// Use the IPEndPoint object to determine which of these two hosts responded.
Console::WriteLine( String::Concat( "This is the message you received ", returnData->ToString() ) );
Console::WriteLine( String::Concat( "This message was sent from ", RemoteIpEndPoint->Address->ToString(), " on their port number ", RemoteIpEndPoint->Port.ToString() ) );
udpClient->Close();
udpClientB->Close();
}
catch ( Exception^ e )
{
Console::WriteLine( e->ToString() );
}
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient(11000);
try{
udpClient.Connect("www.contoso.com", 11000);
// Sends a message to the host to which you have connected.
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
udpClient.Send(sendBytes, sendBytes.Length);
// Sends a message to a different host using optional hostname and port parameters.
UdpClient udpClientB = new UdpClient();
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded.
Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());
udpClient.Close();
udpClientB.Close();
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}
' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient(11000)
Try
udpClient.Connect("www.contoso.com", 11000)
' Sends a message to the host to which you have connected.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
udpClient.Send(sendBytes, sendBytes.Length)
' Sends message to a different host using optional hostname and port parameters.
Dim udpClientB As New UdpClient()
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000)
' IPEndPoint object will allow us to read datagrams sent from any source.
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
' UdpClient.Receive blocks until a message is received from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
' Which one of these two hosts responded?
Console.WriteLine(("This is the message you received " + _
returnData.ToString()))
Console.WriteLine(("This message was sent from " + _
RemoteIpEndPoint.Address.ToString() + _
" on their port number " + _
RemoteIpEndPoint.Port.ToString()))
udpClient.Close()
udpClientB.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub
備註
類別 UdpClient 提供在封鎖同步模式中傳送和接收無連線 UDP 數據報的簡單方法。 由於 UDP 是無連線傳輸通訊協定,因此您不需要在傳送和接收數據之前建立遠端主機連線。 不過,您可以選擇使用下列兩種方式之一建立預設遠端主機:
您可以使用 中 UdpClient 提供的任何傳送方法,將數據傳送至遠端裝置。 Receive使用方法來接收來自遠端主機的數據。
注意
請勿 Send 使用主機名呼叫,或者 IPEndPoint 如果您已經指定預設遠端主機, 如果您這麼做, UdpClient 將會擲回例外狀況。
UdpClient 方法也可讓您傳送和接收多播數據報。 JoinMulticastGroup使用方法來訂閱UdpClient多播群組。 DropMulticastGroup使用 方法從多播群組取消訂閱 UdpClient 。
建構函式
UdpClient() |
初始化 UdpClient 類別的新執行個體。 |
UdpClient(AddressFamily) |
初始化 UdpClient 類別的新執行個體。 |
UdpClient(Int32) |
初始化 UdpClient 類別的新執行個體,並將它繫結至提供的本機通訊埠編號。 |
UdpClient(Int32, AddressFamily) |
初始化 UdpClient 類別的新執行個體,並將它繫結至提供的本機通訊埠編號。 |
UdpClient(IPEndPoint) |
初始化 UdpClient 類別的新執行個體,並將它繫結至指定的本機端點。 |
UdpClient(String, Int32) |
初始化 UdpClient 類別的新執行個體,並建立預設遠端主機。 |
屬性
Active |
取得或設定值,指出是否已經建立預設的遠端主機。 |
Available |
取得已從網路接收且可供讀取的資料量。 |
Client |
取得或設定基礎網路 Socket。 |
DontFragment | |
EnableBroadcast | |
ExclusiveAddressUse | |
MulticastLoopback |
取得或設定 Boolean 值,指定輸出多點傳送封包是否會傳遞至傳送應用程式。 |
Ttl |
取得或設定值,指定由 UdpClient 傳送之網際網路通訊協定 (IP) 封包的存留時間 (TTL) 值。 |
方法
明確介面實作
IDisposable.Dispose() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 釋放 UdpClient 所使用的所有資源。 |