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)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
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 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

另请参阅

适用于

Send(ReadOnlySpan<Byte>, String, Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
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>

类型Byte为 的 ,ReadOnlySpan<T>指定要发送的 UDP 数据报。

hostname
String

要向其发送数据报的远程主机的名称。

port
Int32

要与之通信的远程端口号。

返回

已发送的字节数。

例外

UdpClient 已建立默认远程主机。

访问套接字时出错。

适用于

Send(Byte[], Int32, IPEndPoint)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
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后,将其作为 endPoint 参数传递给 Send 方法。

如果要将数据报发送到任何其他广播地址,请使用 Client 方法获取基础 Socket,并将套接字选项设置为 SocketOptionName.Broadcast。 还可以还原使用 Socket 类。

注意

如果已使用 Connect 方法建立了远程主机,请不要为此方法提供endPoint参数。 如果这样做, Send 方法将引发 SocketException。 如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

另请参阅

适用于

Send(Byte[], Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
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 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

如果要将数据报发送到其他远程主机,则必须调用 Connect 方法并指定所需的远程主机。 使用其他 Send 任一方法重载将数据报发送到广播地址。

另请参阅

适用于

Send(ReadOnlySpan<Byte>)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
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>

类型Byte为 的 ,ReadOnlySpan<T>指定要发送的 UDP 数据报。

返回

已发送的字节数。

例外

UdpClient尚未建立默认远程主机。

访问套接字时出错。

适用于

Send(ReadOnlySpan<Byte>, IPEndPoint)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
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>

类型Byte为 的 ,ReadOnlySpan<T>指定要发送的 UDP 数据报。

endPoint
IPEndPoint

一个 IPEndPoint,表示要将数据报发送到的主机和端口。

返回

已发送的字节数。

例外

UdpClient 已建立默认远程主机,并且 endPoint 不是 null

访问套接字时出错。

适用于