System.Net.Sockets.Socket.SendTo Method

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

Syntax

public int SendTo (byte[] buffer, int offset, int size, SocketFlags flags, System.Net.EndPoint remote_end)

Parameters

flags
Documentation for this section has not yet been entered.
remote_end
Documentation for this section has not yet been entered.
buffer
An array of type byte that contains the data to be sent.
offset
The position in the data buffer at which to begin sending data.
size
The number of bytes to send.

Returns

The number of bytes sent.

Exceptions

TypeReason
ArgumentNullException buffer or remoteEP is null.
ArgumentOutOfRangeException

offset < 0.

-or-

offset > buffer.Length.

-or-

size < 0.

-or-

size > buffer.Length - offset.

InvalidOperationExceptionAn asynchronous call is pending and a blocking method has been called.
System.Net.Sockets.SocketException

socketFlags is not a valid combination of values.

-or-

An error occurred while accessing the socket.

Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.

System.Security.SecurityException A caller in the call stack does not have the required permissions.
ObjectDisposedExceptionThe current instance has been disposed.

Remarks

In this overload, if you specify the SocketFlags.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 Socket.Connect(System.Net.EndPoint) method prior to calling Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint). You only need to do this if you intend to call the Socket.Send(Byte[], int, SocketFlags) method. If you do call the Socket.Connect(System.Net.EndPoint) method prior to calling Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint), the remoteEP parameter will override the specified default remote host for that send operation only. You are also not required to call the Socket.Bind(System.Net.EndPoint) 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 Socket.LocalEndPoint property after the Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) method successfully completes.

Although intended for connectionless protocols, Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) 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 Socket.Connect(System.Net.EndPoint) method or accept an incoming connection request using the Socket.Accept method. If you do not establish or accept a remote host connection, Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) will throw a System.Net.Sockets.SocketException. You can also establish a default remote host for a connectionless protocol prior to calling the Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) method. In either of these cases, Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) 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 System.Net.Sockets.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 Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) 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, Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) will block until the datagram is sent. If you want to send data to a broadcast address, you must first call the Socket.SetSocketOption(SocketOptionLevel, SocketOptionName, int) 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 Socket.SendTo(Byte[], int, int, SocketFlags, System.Net.EndPoint) will throw a System.Net.Sockets.SocketException.

Note:

If you receive a System.Net.Sockets.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.

Note:

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

Permissions

TypeReason
System.Net.SocketPermissionRequires permission to make a connection to the endpoint defined by remoteEP. See System.Net.NetworkAccess.Connect.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0