UdpClient.Send 方法

定義

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

多載

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

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

Send(ReadOnlySpan<Byte>, String, Int32)

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

Send(Byte[], Int32, IPEndPoint)

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

Send(Byte[], Int32)

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

Send(ReadOnlySpan<Byte>)

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

Send(ReadOnlySpan<Byte>, IPEndPoint)

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

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

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

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

public:
 int Send(cli::array <System::Byte> ^ dgram, int bytes, System::String ^ hostname, int port);
public int Send (byte[] dgram, int bytes, string? hostname, int port);
public int Send (byte[] dgram, int bytes, string hostname, int port);
member this.Send : byte[] * int * string * int -> int
Public Function Send (dgram As Byte(), bytes As Integer, hostname As String, port As Integer) As Integer

參數

dgram
Byte[]

Byte 類型的陣列,指定您想傳送的 UDP 資料包,並以位元組陣列表示。

bytes
Int32

資料包中的位元組數目。

hostname
String

您要傳送資料包之目標遠端主機的名稱。

port
Int32

您要用來通訊的遠端連接埠號碼。

傳回

已傳送的位元組數。

例外狀況

dgramnull

UdpClient 已建立預設遠端主機。

存取通訊端時發生錯誤。

範例

下列範例示範 Send 方法。 此範例會使用主機名和埠號碼來識別目標主機。

UdpClient^ udpClient = gcnew UdpClient;

array<Byte>^ sendBytes = Encoding::ASCII->GetBytes( "Is anybody there" );
try
{
   udpClient->Send( sendBytes, sendBytes->Length, "www.contoso.com", 11000 );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
UdpClient udpClient = new UdpClient();

Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
try{
    udpClient.Send(sendBytes, sendBytes.Length, "www.contoso.com", 11000);
}
catch ( Exception e ){
    Console.WriteLine(e.ToString());	
}
Dim udpClient As New UdpClient()

Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
Try
   udpClient.Send(sendBytes, sendBytes.Length, "www.contoso.com", 11000)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

備註

方法Send會將數據報傳送至 和 port 參數所hostname指定的值,並傳回成功傳送的位元組數目。 您可以指定參數值的 「255.255.255.255」, hostname 以將數據報傳送至預設廣播位址。

如果您要將資料報傳送至任何其他廣播位址,請使用 Client 方法來取得基礎 Socket,並將套接字選項設定為 SocketOptionName.Broadcast。 您也可以使用 Socket 類別還原為 。

注意

如果您已經使用 方法建立遠端主機,請勿提供這個方法的 Connect 主機名或埠號碼。 如果您這樣做, Send 方法會擲回 SocketException。 如果您收到 SocketException,請使用 SocketException.ErrorCode 來取得特定的錯誤碼。 取得此程式代碼之後,您可以參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

另請參閱

適用於

Send(ReadOnlySpan<Byte>, String, Int32)

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

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

public:
 int Send(ReadOnlySpan<System::Byte> datagram, System::String ^ hostname, int port);
public int Send (ReadOnlySpan<byte> datagram, string? hostname, int port);
member this.Send : ReadOnlySpan<byte> * string * int -> int
Public Function Send (datagram As ReadOnlySpan(Of Byte), hostname As String, port As Integer) As Integer

參數

datagram
ReadOnlySpan<Byte>

ReadOnlySpan<T>Byte ,指定您想要傳送的 UDP 數據報。

hostname
String

您要傳送資料包之目標遠端主機的名稱。

port
Int32

您要用來通訊的遠端連接埠號碼。

傳回

已傳送的位元組數。

例外狀況

UdpClient 已建立預設遠端主機。

存取通訊端時發生錯誤。

適用於

Send(Byte[], Int32, IPEndPoint)

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

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

public:
 int Send(cli::array <System::Byte> ^ dgram, int bytes, System::Net::IPEndPoint ^ endPoint);
public int Send (byte[] dgram, int bytes, System.Net.IPEndPoint? endPoint);
public int Send (byte[] dgram, int bytes, System.Net.IPEndPoint endPoint);
member this.Send : byte[] * int * System.Net.IPEndPoint -> int
Public Function Send (dgram As Byte(), bytes As Integer, endPoint As IPEndPoint) As Integer

參數

dgram
Byte[]

Byte 類型的陣列,指定您想傳送的 UDP 資料包,並以位元組陣列表示。

bytes
Int32

資料包中的位元組數目。

endPoint
IPEndPoint

IPEndPoint,表示傳送資料包的目標主機和通訊埠。

傳回

已傳送的位元組數。

例外狀況

dgramnull

UdpClient 已建立預設遠端主機。

存取通訊端時發生錯誤。

範例

下列範例示範 Send 方法。 這個範例會使用 IPEndPoint 來指定目標主機。

UdpClient^ udpClient = gcnew UdpClient;
IPAddress^ ipAddress = Dns::Resolve( "www.contoso.com" )->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddress,11004 );

array<Byte>^ sendBytes = Encoding::ASCII->GetBytes( "Is anybody there?" );
try
{
   udpClient->Send( sendBytes, sendBytes->Length, ipEndPoint );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
UdpClient udpClient = new UdpClient();
IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004);	

Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
try{
    udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint);
}
catch ( Exception e ){
    Console.WriteLine(e.ToString());	
}
Dim udpClient As New UdpClient()
Dim ipAddress As IPAddress = Dns.Resolve("www.contoso.com").AddressList(0)
Dim ipEndPoint As New IPEndPoint(ipAddress, 11004)

Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
Try
   udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

備註

方法 Send 會將數據報傳送至指定的端點,並傳回成功傳送的位元元組數目。 撥號此多載之前,您必須先使用傳送資料報的遠端主機IP位址和埠號碼來建立 IPEndPoint 。 您可以指定 SocketOptionName.BroadcastAddress 的 屬性 IPEndPoint,將數據報傳送至預設廣播位址 255.255.255.255。 建立這個 IPEndPoint之後,請將它傳遞至 Send 方法做為 endPoint 參數。

如果您要將資料報傳送至任何其他廣播位址,請使用 Client 方法來取得基礎 Socket,並將套接字選項設定為 SocketOptionName.Broadcast。 您也可以使用 Socket 類別還原為 。

注意

如果您已經使用 方法建立遠端主機,請勿提供 endPoint 這個方法的參數 Connect 。 如果您這樣做, Send 方法會擲回 SocketException。 如果您收到 SocketException,請使用 SocketException.ErrorCode 來取得特定的錯誤碼。 取得此程式代碼之後,您可以參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

另請參閱

適用於

Send(Byte[], Int32)

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

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

public:
 int Send(cli::array <System::Byte> ^ dgram, int bytes);
public int Send (byte[] dgram, int bytes);
member this.Send : byte[] * int -> int
Public Function Send (dgram As Byte(), bytes As Integer) As Integer

參數

dgram
Byte[]

Byte 類型的陣列,指定您想傳送的 UDP 資料包,並以位元組陣列表示。

bytes
Int32

資料包中的位元組數目。

傳回

已傳送的位元組數。

例外狀況

dgramnull

UdpClient 已建立預設遠端主機。

存取通訊端時發生錯誤。

範例

下列範例示範 Send 方法。 您必須先建立默認遠端主機,才能使用此多載。

UdpClient^ udpClient = gcnew UdpClient( "www.contoso.com",11000 );
array<Byte>^ sendBytes = Encoding::ASCII->GetBytes( "Is anybody there" );
try
{
   udpClient->Send( sendBytes, sendBytes->Length );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
UdpClient udpClient = new UdpClient("www.contoso.com", 11000);
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
try{
    udpClient.Send(sendBytes, sendBytes.Length);
}
catch ( Exception e ){
    Console.WriteLine( e.ToString());
}
Dim udpClient As New UdpClient("www.contoso.com", 11000)
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
Try
   udpClient.Send(sendBytes, sendBytes.Length)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try

備註

此多載會將數據報傳送至 方法中 Connect 建立的遠端主機,並傳回傳送的位元組數目。 如果您在呼叫這個多載之前未呼叫 Connect ,方法 Send 會擲回 SocketException。 如果您收到 SocketException,請使用 SocketException.ErrorCode 來取得特定的錯誤碼。 取得此程式代碼之後,您可以參閱 Windows Sockets 第 2 版 API 錯誤碼 檔,以取得錯誤的詳細描述。

如果您想要將數據報傳送至不同的遠端主機,您必須呼叫 Connect 方法,並指定所需的遠端主機。 使用其他 Send 任一方法多載,將數據報傳送至廣播位址。

另請參閱

適用於

Send(ReadOnlySpan<Byte>)

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

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

public:
 int Send(ReadOnlySpan<System::Byte> datagram);
public int Send (ReadOnlySpan<byte> datagram);
member this.Send : ReadOnlySpan<byte> -> int
Public Function Send (datagram As ReadOnlySpan(Of Byte)) As Integer

參數

datagram
ReadOnlySpan<Byte>

ReadOnlySpan<T>Byte ,指定您想要傳送的 UDP 數據報。

傳回

已傳送的位元組數。

例外狀況

UdpClient尚未建立預設遠端主機。

存取通訊端時發生錯誤。

適用於

Send(ReadOnlySpan<Byte>, IPEndPoint)

來源:
UDPClient.cs
來源:
UDPClient.cs
來源:
UDPClient.cs

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

public:
 int Send(ReadOnlySpan<System::Byte> datagram, System::Net::IPEndPoint ^ endPoint);
public int Send (ReadOnlySpan<byte> datagram, System.Net.IPEndPoint? endPoint);
member this.Send : ReadOnlySpan<byte> * System.Net.IPEndPoint -> int
Public Function Send (datagram As ReadOnlySpan(Of Byte), endPoint As IPEndPoint) As Integer

參數

datagram
ReadOnlySpan<Byte>

ReadOnlySpan<T>Byte ,指定您想要傳送的 UDP 數據報。

endPoint
IPEndPoint

IPEndPoint,表示傳送資料包的目標主機和通訊埠。

傳回

已傳送的位元組數。

例外狀況

UdpClient 已建立預設遠端主機,而不是 endPointnull

存取通訊端時發生錯誤。

適用於