SendTo Method (Byte[], SocketFlags, EndPoint)
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Sends data to a specific endpoint using the specified SocketFlags.
Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Syntax
'Declaration
Public Function SendTo ( _
buffer As Byte(), _
socketFlags As SocketFlags, _
remoteEP As EndPoint _
) As Integer
public int SendTo(
byte[] buffer,
SocketFlags socketFlags,
EndPoint remoteEP
)
public:
int SendTo(
array<unsigned char>^ buffer,
SocketFlags socketFlags,
EndPoint^ remoteEP
)
member SendTo :
buffer:byte[] *
socketFlags:SocketFlags *
remoteEP:EndPoint -> int
public function SendTo(
buffer : byte[],
socketFlags : SocketFlags,
remoteEP : EndPoint
) : int
Parameters
- buffer
Type: array<System. . :: . .Byte> [] () [] []
An array of type Byte that contains the data to be sent.
- socketFlags
Type: System.Net.Sockets. . :: . .SocketFlags
A bitwise combination of the SocketFlags values.
- remoteEP
Type: System.Net. . :: . .EndPoint
The EndPoint that represents the destination location for the data.
Return Value
Type: System. . :: . .Int32
The number of bytes sent.
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 in the MSDN library for a detailed description of the error.
.NET Framework Security
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see dd66cd4c-b087-415f-9c3e-94e3a1835f74.