Редагувати

Поділитися через


UdpClient.Send Method

Definition

Sends a UDP datagram to a remote host.

Overloads

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

Sends a UDP datagram to a specified port on a specified remote host.

Send(ReadOnlySpan<Byte>, String, Int32)

Sends a UDP datagram to a specified port on a specified remote host.

Send(Byte[], Int32, IPEndPoint)

Sends a UDP datagram to the host at the specified remote endpoint.

Send(Byte[], Int32)

Sends a UDP datagram to a remote host.

Send(ReadOnlySpan<Byte>)

Sends a UDP datagram to a remote host.

Send(ReadOnlySpan<Byte>, IPEndPoint)

Sends a UDP datagram to the host at the specified remote endpoint.

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

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram to a specified port on a specified remote host.

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

Parameters

dgram
Byte[]

An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.

bytes
Int32

The number of bytes in the datagram.

hostname
String

The name of the remote host to which you intend to send the datagram.

port
Int32

The remote port number with which you intend to communicate.

Returns

The number of bytes sent.

Exceptions

dgram is null.

The UdpClient has already established a default remote host.

An error occurred when accessing the socket.

Examples

The following example demonstrates the Send method. This example uses a host name and a port number to identify the target host.

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

Remarks

The Send method sends datagrams to the values specified by the hostname and port parameters and returns the number of bytes successfully sent. You can send datagrams to the default broadcast address by specifying "255.255.255.255" for the hostname parameter value.

If you want to send datagrams to any other broadcast address, use the Client method to obtain the underlying Socket, and set the socket option to SocketOptionName.Broadcast. You can also revert to using the Socket class.

Note

Do not provide a host name or port number to this method if you have already established a remote host with the Connect method. If you do, the Send method will throw a SocketException. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

See also

Applies to

Send(ReadOnlySpan<Byte>, String, Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram to a specified port on a specified remote host.

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

Parameters

datagram
ReadOnlySpan<Byte>

An ReadOnlySpan<T> of Type Byte that specifies the UDP datagram that you intend to send.

hostname
String

The name of the remote host to which you intend to send the datagram.

port
Int32

The remote port number with which you intend to communicate.

Returns

The number of bytes sent.

Exceptions

The UdpClient has already established a default remote host.

An error occurred when accessing the socket.

Applies to

Send(Byte[], Int32, IPEndPoint)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram to the host at the specified remote endpoint.

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

Parameters

dgram
Byte[]

An array of type Byte that specifies the UDP datagram that you intend to send, represented as an array of bytes.

bytes
Int32

The number of bytes in the datagram.

endPoint
IPEndPoint

An IPEndPoint that represents the host and port to which to send the datagram.

Returns

The number of bytes sent.

Exceptions

dgram is null.

UdpClient has already established a default remote host.

An error occurred when accessing the socket.

Examples

The following example demonstrates the Send method. This example uses an IPEndPoint to specify the target host.

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

Remarks

The Send method sends datagrams to the specified endpoint and returns the number of bytes successfully sent. Before calling this overload, you must first create an IPEndPoint using the IP address and port number of the remote host to which your datagrams will be delivered. You can send datagrams to the default broadcast address, 255.255.255.255, by specifying SocketOptionName.Broadcast for the Address property of the IPEndPoint. After you have created this IPEndPoint, pass it to the Send method as the endPoint parameter.

If you want to send datagrams to any other broadcast address, use the Client method to obtain the underlying Socket, and set the socket option to SocketOptionName.Broadcast. You can also revert to using the Socket class.

Note

Do not provide an endPoint parameter to this method if you have already established a remote host with the Connect method. If you do, the Send method will throw a SocketException. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

See also

Applies to

Send(Byte[], Int32)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram to a remote host.

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

Parameters

dgram
Byte[]

An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.

bytes
Int32

The number of bytes in the datagram.

Returns

The number of bytes sent.

Exceptions

dgram is null.

The UdpClient has already established a default remote host.

An error occurred when accessing the socket.

Examples

The following example demonstrates the Send method. You must establish a default remote host prior to using this overload.

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

Remarks

This overload sends datagrams to the remote host established in the Connect method and returns the number of bytes sent. If you do not call Connect before calling this overload, the Send method will throw a SocketException. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

If you want to send datagrams to a different remote host, you must call the Connect method and specify the desired remote host. Use either of the other Send method overloads to send datagrams to a broadcast address.

See also

Applies to

Send(ReadOnlySpan<Byte>)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram to a remote host.

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

Parameters

datagram
ReadOnlySpan<Byte>

An ReadOnlySpan<T> of Type Byte that specifies the UDP datagram that you intend to send.

Returns

The number of bytes sent.

Exceptions

The UdpClient has not established a default remote host.

An error occurred when accessing the socket.

Applies to

Send(ReadOnlySpan<Byte>, IPEndPoint)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a UDP datagram to the host at the specified remote endpoint.

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

Parameters

datagram
ReadOnlySpan<Byte>

An ReadOnlySpan<T> of Type Byte that specifies the UDP datagram that you intend to send.

endPoint
IPEndPoint

An IPEndPoint that represents the host and port to which to send the datagram.

Returns

The number of bytes sent.

Exceptions

UdpClient has already established a default remote host and endPoint is not null.

An error occurred when accessing the socket.

Applies to