UdpClient 類別

定義

提供使用者資料包通訊協定 (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 實例。

  • 建立 類別的 UdpClient 實例,然後呼叫 Connect 方法。

您可以使用 中 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

取得或設定 Boolean 值,指定 UdpClient 是否允許將網際網路通訊協定 (IP) 資料包分散。

EnableBroadcast

取得或設定 Boolean 值,指定 是否可以 UdpClient 傳送廣播封包。

ExclusiveAddressUse

取得或設定 Boolean 值,指定 UdpClient 是否只允許一個用戶端使用連接埠。

MulticastLoopback

取得或設定 Boolean 值,指定輸出多點傳送封包是否會傳遞至傳送應用程式。

Ttl

取得或設定值,指定由 UdpClient 傳送之網際網路通訊協定 (IP) 封包的存留時間 (TTL) 值。

方法

AllowNatTraversal(Boolean)

啟用或停用在 UdpClient 執行個體上的網路位址轉譯 (NAT) 周遊。

BeginReceive(AsyncCallback, Object)

非同步接收遠端主機的資料包。

BeginSend(Byte[], Int32, AsyncCallback, Object)

將資料包非同步傳送至遠端主機。 目的端由先前對 Connect 的呼叫指定。

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

將資料包非同步傳送至目的端。 目的端由 EndPoint 指定。

BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

將資料包非同步傳送至目的端。 目的端由主機名稱和通訊埠編號指定。

Close()

關閉 UDP 連接。

Connect(IPAddress, Int32)

使用指定的 IP 位址和通訊埠編號,建立預設遠端主機。

Connect(IPEndPoint)

使用指定的網路端點,建立預設的遠端主機。

Connect(String, Int32)

使用指定的主機名稱和通訊埠編號,建立預設遠端主機。

Dispose()

釋放 UdpClient 使用的受控與非受控資源。

Dispose(Boolean)

釋放 UdpClient 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

DropMulticastGroup(IPAddress)

保留多點傳送 (Multicast) 群組。

DropMulticastGroup(IPAddress, Int32)

保留多點傳送 (Multicast) 群組。

EndReceive(IAsyncResult, IPEndPoint)

結束暫止的非同步接收。

EndSend(IAsyncResult)

結束暫止的非同步傳送。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
JoinMulticastGroup(Int32, IPAddress)

UdpClient 加入至多點傳送群組。

JoinMulticastGroup(IPAddress)

UdpClient 加入至多點傳送群組。

JoinMulticastGroup(IPAddress, Int32)

使用指定的存活時間 (Time to Live,TTL) 將 UdpClient 加入至多點傳送群組。

JoinMulticastGroup(IPAddress, IPAddress)

UdpClient 加入至多點傳送群組。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Receive(IPEndPoint)

傳回由遠端主機傳送的 UDP 資料包。

ReceiveAsync()

非同步傳回由遠端主機傳送的 UDP 資料包。

ReceiveAsync(CancellationToken)

非同步傳回由遠端主機傳送的 UDP 資料包。

Send(Byte[], Int32)

將 UDP 資料包傳送至遠端主機。

Send(Byte[], Int32, IPEndPoint)

將 UDP 資料包傳送至指定遠端端點上的主機。

Send(Byte[], Int32, String, Int32)

將 UDP 資料包傳送至指定遠端主機上的指定通訊埠。

Send(ReadOnlySpan<Byte>)

將 UDP 資料包傳送至遠端主機。

Send(ReadOnlySpan<Byte>, IPEndPoint)

將 UDP 資料包傳送至指定遠端端點上的主機。

Send(ReadOnlySpan<Byte>, String, Int32)

將 UDP 資料包傳送至指定遠端主機上的指定通訊埠。

SendAsync(Byte[], Int32)

將 UDP 資料包非同步傳送至遠端主機。

SendAsync(Byte[], Int32, IPEndPoint)

將 UDP 資料包非同步傳送至遠端主機。

SendAsync(Byte[], Int32, String, Int32)

將 UDP 資料包非同步傳送至遠端主機。

SendAsync(ReadOnlyMemory<Byte>, CancellationToken)

將 UDP 資料包非同步傳送至遠端主機。

SendAsync(ReadOnlyMemory<Byte>, IPEndPoint, CancellationToken)

將 UDP 資料包非同步傳送至遠端主機。

SendAsync(ReadOnlyMemory<Byte>, String, Int32, CancellationToken)

將 UDP 資料包非同步傳送至遠端主機。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

IDisposable.Dispose()

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

釋放 UdpClient 所使用的所有資源。

適用於

另請參閱