Socket.SendTo Method (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.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

Public Function SendTo ( _
	buffer As Byte(), _
	offset As Integer, _
	size As Integer, _
	socketFlags As SocketFlags, _
	remoteEP As EndPoint _
) As Integer
Dim instance As Socket
Dim buffer As Byte()
Dim offset As Integer
Dim size As Integer
Dim socketFlags As SocketFlags
Dim remoteEP As EndPoint
Dim returnValue As Integer

returnValue = instance.SendTo(buffer, offset, size, socketFlags, remoteEP)
public int SendTo (
	byte[] buffer, 
	int offset, 
	int size, 
	SocketFlags socketFlags, 
	EndPoint remoteEP
public function SendTo (
	buffer : byte[], 
	offset : int, 
	size : int, 
	socketFlags : SocketFlags, 
	remoteEP : EndPoint
) : int



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


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


The number of bytes to send.


A bitwise combination of the SocketFlags values.


The EndPoint that represents the destination location for the data.

Return Value

The number of bytes sent.

Exception typeCondition


buffer is a null reference (Nothing in Visual Basic).


remoteEP is a null reference (Nothing in Visual Basic).


offset is less than 0.


offset is greater than the length of buffer.


size is less than 0.


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


socketFlags is not a valid combination of values.


An operating system error occurs while accessing the Socket. See the Remarks section for more information.


The Socket has been closed.


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

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.


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 in the MSDN library for a detailed description of the error.


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

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: The consequence of sending zero-length data with this method is determined by the native operating system and not by the .NET Compact Framework.

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

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)

End Sub 'SendTo4

public static void SendTo4()
    IPHostEntry hostEntry = Dns.Resolve(Dns.GetHostName());
    IPEndPoint endPoint =
        new IPEndPoint(hostEntry.get_AddressList()[0], 11000);

    Socket s = new Socket(endPoint.get_Address().get_AddressFamily(),
        SocketType.Dgram, ProtocolType.Udp);

    ubyte msg[] = Encoding.get_ASCII().GetBytes("This is a test");
    Console.WriteLine("Sending data.");
    // This call blocks. 
    s.SendTo(msg, 0, msg.length, SocketFlags.None, endPoint);
} //SendTo4

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0