Socket.SendTo Method

Definition

Sends data to a specific endpoint.

Overloads

SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint)

Sends the specified number of bytes of data to the specified endpoint, starting at the specified location in the buffer, and using the specified SocketFlags.

SendTo(Byte[], Int32, SocketFlags, EndPoint)

Sends the specified number of bytes of data to the specified endpoint using the specified SocketFlags.

SendTo(ReadOnlySpan<Byte>, SocketFlags, SocketAddress)

Sends data to a specific endpoint using the specified SocketFlags.

SendTo(ReadOnlySpan<Byte>, EndPoint)

Sends data to the specified endpoint.

SendTo(Byte[], SocketFlags, EndPoint)

Sends data to a specific endpoint using the specified SocketFlags.

SendTo(Byte[], EndPoint)

Sends data to the specified endpoint.

SendTo(ReadOnlySpan<Byte>, SocketFlags, EndPoint)

Sends data to a specific endpoint using the specified SocketFlags.

SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint)

Sends the specified number of bytes of data to the specified endpoint, starting at the specified location in the buffer, and using the specified SocketFlags.

public:
 int SendTo(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP);
public int SendTo (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP);
member this.SendTo : byte[] * int * int * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> int
Public Function SendTo (buffer As Byte(), offset As Integer, size As Integer, socketFlags As SocketFlags, remoteEP As EndPoint) As Integer

Parameters

buffer
Byte[]

An array of type Byte that contains the data to be sent.

offset
Int32

The position in the data buffer at which to begin sending data.

size
Int32

The number of bytes to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

The EndPoint that represents the destination location for the data.

Returns

The number of bytes sent.

Exceptions

buffer is null.

-or-

remoteEP is null.

offset is less than 0.

-or-

offset is greater than the length of buffer.

-or-

size is less than 0.

-or-

size is greater than the length of buffer minus the value of the offset parameter.

socketFlags is not a valid combination of values.

-or-

An operating system error occurs while accessing the Socket.

The Socket has been closed.

A caller in the call stack does not have the required permissions.

Examples

The following code example sends a connectionless datagram to the specified remote host. The offset, size, and SocketFlags are passed to the SendTo method.

static void SendTo4()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );

   array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
   Console::WriteLine( "Sending data." );
   // This call blocks. 
   s->SendTo( msg, 0, msg->Length, SocketFlags::None, endPoint );
   s->Close();
}
public static void SendTo4()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);

    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks.
    s.SendTo(msg, 0, msg.Length, SocketFlags.None, endPoint);
    s.Close();
}
Public Shared Sub SendTo4() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, 0, msg.Length, SocketFlags.None, endPoint)
    s.Close()

End Sub

Remarks

In this overload, if you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect method prior to calling SendTo. You only need to do this if you intend to call the Send method. If you do call the Connect method prior to calling SendTo, the remoteEP parameter will override the specified default remote host for that send operation only. You are also not required to call the Bind method, because the underlying service provider will assign the most appropriate local network address and port number. If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the SendTo method successfully completes.

Although intended for connectionless protocols, SendTo also works with connection-oriented protocols. If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the Connect method or accept an incoming connection request using the Accept method. If you do not establish or accept a remote host connection, SendTo will throw a SocketException. You can also establish a default remote host for a connectionless protocol prior to calling the SendTo method. In either of these cases, SendTo will ignore the remoteEP parameter and only send data to the connected or default remote host.

Blocking sockets will block until the requested number of bytes are sent. Since a non-blocking Socket completes immediately, it might not send all of the bytes requested in a single operation. It is your applications responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the requested number of bytes. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of out-going data is collected. A successful completion of the SendTo method means that the underlying system has had room to buffer your data for a network send.

If you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. If you want to send data to a broadcast address, you must first call the SetSocketOption method and set the socket option to SocketOptionName.Broadcast. You must also be sure that the size does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and SendTo will throw a SocketException.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

SendTo(Byte[], Int32, SocketFlags, EndPoint)

Sends the specified number of bytes of data to the specified endpoint using the specified SocketFlags.

public:
 int SendTo(cli::array <System::Byte> ^ buffer, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP);
public int SendTo (byte[] buffer, int size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP);
member this.SendTo : byte[] * int * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> int
Public Function SendTo (buffer As Byte(), size As Integer, socketFlags As SocketFlags, remoteEP As EndPoint) As Integer

Parameters

buffer
Byte[]

An array of type Byte that contains the data to be sent.

size
Int32

The number of bytes to send.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

The EndPoint that represents the destination location for the data.

Returns

The number of bytes sent.

Exceptions

buffer is null.

-or-

remoteEP is null.

The specified size exceeds the size of buffer.

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

The following code example sends a connectionless datagram to the specified remote host. The size and SocketFlags are passed to the SendTo method.

static void SendTo3()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );

   array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
   Console::WriteLine( "Sending data." );
   // This call blocks. 
   s->SendTo( msg, msg->Length, SocketFlags::None, endPoint );
   s->Close();
}
public static void SendTo3()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);

    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks.
    s.SendTo(msg, msg.Length, SocketFlags.None, endPoint);
    s.Close();
}
Public Shared Sub SendTo3() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, msg.Length, SocketFlags.None, endPoint)
    s.Close()

End Sub

Remarks

In this overload, the buffer offset defaults to 0. If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect method prior to calling SendTo. You only need to do this if you intend to call the Send method. If you do call the Connect method prior to calling SendTo, the remoteEP parameter will override the specified default remote host for that send operation only. You are also not required to call the Bind method, because the underlying service provider will assign the most appropriate local network address and port number. If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the SendTo method successfully completes.

Although intended for connectionless protocols, SendTo also works with connection-oriented protocols. If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the Connect method or accept an incoming connection request using the Accept method. If you do not establish or accept a remote host connection, SendTo will throw a SocketException. You can also establish a default remote host for a connectionless protocol prior to calling the SendTo method. In either of these cases, SendTo will ignore the remoteEP parameter and only send data to the connected or default remote host.

Blocking sockets will block until the requested number of bytes are sent. Since a nonblocking Socket completes immediately, it might not send all of the bytes requested in a single operation. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends the requested number of bytes. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of out-going data is collected. A successful completion of the SendTo method means that the underlying system has had room to buffer your data for a network send.

If you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. If you want to send data to a broadcast address, you must first call the SetSocketOption method and set the socket option to SocketOptionName.Broadcast. You must also be sure that the number of bytes sent does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and SendTo will throw a SocketException.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

SendTo(ReadOnlySpan<Byte>, SocketFlags, SocketAddress)

Sends data to a specific endpoint using the specified SocketFlags.

public:
 int SendTo(ReadOnlySpan<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::SocketAddress ^ socketAddress);
public int SendTo (ReadOnlySpan<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.SocketAddress socketAddress);
member this.SendTo : ReadOnlySpan<byte> * System.Net.Sockets.SocketFlags * System.Net.SocketAddress -> int
Public Function SendTo (buffer As ReadOnlySpan(Of Byte), socketFlags As SocketFlags, socketAddress As SocketAddress) As Integer

Parameters

buffer
ReadOnlySpan<Byte>

A span of bytes that contains the data to be sent.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values that will be used when sending the data.

socketAddress
SocketAddress

The SocketAddress that represents the destination for the data.

Returns

The number of bytes sent.

Exceptions

socketAddress is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

SendTo(ReadOnlySpan<Byte>, EndPoint)

Sends data to the specified endpoint.

public:
 int SendTo(ReadOnlySpan<System::Byte> buffer, System::Net::EndPoint ^ remoteEP);
public int SendTo (ReadOnlySpan<byte> buffer, System.Net.EndPoint remoteEP);
member this.SendTo : ReadOnlySpan<byte> * System.Net.EndPoint -> int
Public Function SendTo (buffer As ReadOnlySpan(Of Byte), remoteEP As EndPoint) As Integer

Parameters

buffer
ReadOnlySpan<Byte>

A span of bytes that contains the data to be sent.

remoteEP
EndPoint

The EndPoint that represents the destination for the data.

Returns

The number of bytes sent.

Exceptions

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

SendTo(Byte[], SocketFlags, EndPoint)

Sends data to a specific endpoint using the specified SocketFlags.

public:
 int SendTo(cli::array <System::Byte> ^ buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP);
public int SendTo (byte[] buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP);
member this.SendTo : byte[] * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> int
Public Function SendTo (buffer As Byte(), socketFlags As SocketFlags, remoteEP As EndPoint) As Integer

Parameters

buffer
Byte[]

An array of type Byte that contains the data to be sent.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

The EndPoint that represents the destination location for the data.

Returns

The number of bytes sent.

Exceptions

buffer is null.

-or-

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

The following code example sends a connectionless datagram to the specified remote host. SocketFlags are passed to the SendTo method.

static void SendTo2()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );

   array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
   Console::WriteLine( "Sending data." );
   // This call blocks. 
   s->SendTo( msg, SocketFlags::None, endPoint );
   s->Close();
}
public static void SendTo2()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);

    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks.
    s.SendTo(msg, SocketFlags.None, endPoint);
    s.Close();
}
Public Shared Sub SendTo2() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, SocketFlags.None, endPoint)
    s.Close()

End Sub

Remarks

In this overload, the buffer offset defaults to 0, and the number of bytes to send defaults to the size of the buffer. If you specify the DontRoute flag as the socketflags parameter, the data you are sending will not be routed.

If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect method prior to calling SendTo. You only need to do this if you intend to call the Send method. If you do call the Connect method prior to calling SendTo, the remoteEP parameter will override the specified default remote host for that send operation only. You are also not required to call the Bind method, because the underlying service provider will assign the most appropriate local network address and port number. If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the SendTo method successfully completes.

Although intended for connectionless protocols, SendTo also works with connection-oriented protocols. If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the Connect method or accept an incoming connection request using the Accept method. If you do not establish or accept a remote host connection, SendTo will throw a SocketException. You can also establish a default remote host for a connectionless protocol prior to calling the SendTo method. In either of these cases, SendTo will ignore the remoteEP parameter and only send data to the connected or default remote host.

Blocking sockets will block until the requested all of the bytes in the buffer are sent. Since a nonblocking Socket completes immediately, it might not send all of the bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends all of the bytes in the buffer. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of out-going data is collected. A successful completion of the SendTo method means that the underlying system has had room to buffer your data for a network send.

If you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. If you want to send data to a broadcast address, you must first call the SetSocketOption method and set the socket option to SocketOptionName.Broadcast. You must also be sure that the number of bytes sent does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and SendTo will throw a SocketException.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

SendTo(Byte[], EndPoint)

Sends data to the specified endpoint.

public:
 int SendTo(cli::array <System::Byte> ^ buffer, System::Net::EndPoint ^ remoteEP);
public int SendTo (byte[] buffer, System.Net.EndPoint remoteEP);
member this.SendTo : byte[] * System.Net.EndPoint -> int
Public Function SendTo (buffer As Byte(), remoteEP As EndPoint) As Integer

Parameters

buffer
Byte[]

An array of type Byte that contains the data to be sent.

remoteEP
EndPoint

The EndPoint that represents the destination for the data.

Returns

The number of bytes sent.

Exceptions

buffer is null.

-or-

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Examples

The following code example sends a connectionless datagram to the specified remote host.

static void SendTo1()
{
   IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() );
   IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 );

   Socket^ s = gcnew Socket( endPoint->Address->AddressFamily,
      SocketType::Dgram,
      ProtocolType::Udp );

   array<Byte>^ msg = Encoding::ASCII->GetBytes( "This is a test" );
   Console::WriteLine( "Sending data." );
   // This call blocks. 
   s->SendTo( msg, endPoint );
   s->Close();
}
public static void SendTo1()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);

    byte[] msg = Encoding.ASCII.GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks.
    s.SendTo(msg, endPoint);
    s.Close();
}
Public Shared Sub SendTo1() 
    Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
    Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)
    
    Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
    
    Dim msg As Byte() = Encoding.ASCII.GetBytes("This is a test")
    Console.WriteLine("Sending data.")
    ' This call blocks. 
    s.SendTo(msg, endPoint)
    s.Close()

End Sub

Remarks

In this overload, the buffer offset defaults to 0, the number of bytes to send defaults to the size of the buffer parameter, and the SocketFlags value defaults to 0.

If you are using a connectionless protocol, you do not need to establish a default remote host with the Connect method prior to calling SendTo. You only need to do this if you intend to call the Send method. If you do call the Connect method prior to calling SendTo, the remoteEP parameter will override the specified default remote host for that send operation only. You are also not required to call the Bind method, because the underlying service provider will assign the most appropriate local network address and port number. If you need to identify the assigned local network address and port number, you can use the LocalEndPoint property after the SendTo method successfully completes.

Although intended for connectionless protocols, SendTo also works with connection-oriented protocols. If you are using a connection-oriented protocol, you must first establish a remote host connection by calling the Connect method or accept an incoming connection request using the Accept method. If you do not establish or accept a remote host connection, SendTo will throw a SocketException. You can also establish a default remote host for a connectionless protocol prior to calling the SendTo method. In either of these cases, SendTo will ignore the remoteEP parameter and only send data to the connected or default remote host.

Blocking sockets will block until the all of the bytes in the buffer are sent. Since a nonblocking Socket completes immediately, it might not send all of the bytes in the buffer. It is your application's responsibility to keep track of the number of bytes sent and to retry the operation until the application sends all of the bytes in the buffer. There is also no guarantee that the data you send will appear on the network immediately. To increase network efficiency, the underlying system may delay transmission until a significant amount of outgoing data is collected. A successful completion of the SendTo method means that the underlying system has had room to buffer your data for a network send.

If you are using a connectionless protocol in blocking mode, SendTo will block until the datagram is sent. If you want to send data to a broadcast address, you must first call the SetSocketOption method and set the socket option to SocketOptionName.Broadcast. You must also be sure that the number of bytes sent does not exceed the maximum packet size of the underlying service provider. If it does, the datagram will not be sent and SendTo will throw a SocketException.

Note

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

SendTo(ReadOnlySpan<Byte>, SocketFlags, EndPoint)

Sends data to a specific endpoint using the specified SocketFlags.

public:
 int SendTo(ReadOnlySpan<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ remoteEP);
public int SendTo (ReadOnlySpan<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP);
member this.SendTo : ReadOnlySpan<byte> * System.Net.Sockets.SocketFlags * System.Net.EndPoint -> int
Public Function SendTo (buffer As ReadOnlySpan(Of Byte), socketFlags As SocketFlags, remoteEP As EndPoint) As Integer

Parameters

buffer
ReadOnlySpan<Byte>

A span of bytes that contains the data to be sent.

socketFlags
SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
EndPoint

The EndPoint that represents the destination for the data.

Returns

The number of bytes sent.

Exceptions

remoteEP is null.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to