The sendto function sends data through a socket, and can be used on connection-oriented sockets (e.g. stream sockets) or connectionless sockets (e.g. datagram sockets). The behavior of this function may depend on whether the socket has its non-blocking mode enabled or disabled (see dealing with blocking for more information about the non-blocking mode of sockets).
When the sendto function is used on connectionless sockets, care must be taken not to exceed the maximum packet size of the underlying provider, which can be obtained by using the getsockopt function to retrieve the value of socket option SO_MAX_MSG_SIZE. If the data is too long to pass atomically through the underlying protocol, the sendto function will fail, and if you then call the WSAGetLastError function to find out the reason for the error WSAEMSGSIZE is returned, and no data is transmitted.
The system include file WinSock2.inc contains the following declaration for the sendto function:
function sendto(s : SOCKET; buf : address; len, flags : integer; toaddr : address; tolen : integer) : integer; external dll='ws2_32.dll';
The first argument passed to the sendto function is the socket that you want to send data through.
The second argument passed to the sendto function is the operating system address of the buffer containing the data to be sent through the socket. The type of this argument is address which means that you must use the built-in function addr to pass the operating system address of the buffer.
The third argument passed to the sendto function is the number of bytes of data to be sent through the socket. This argument can be zero, and this case will be treated by implementations as successful, and send will return zero as a valid value. If the socket is connectionless, a zero-length transport datagram is sent.
The fourth argument passed to the sendto function can specify flags which influence the behavior of the send function. The flags can be OR'd or added together if more than one flag is being specified. The flags which can be used are:
The fifth argument passed to the sendto function usually specifies the name of the destination socket or sockets that should receive the data being sent. If the connect function has been used previously on the socket that you are sending the data through, to specify a default destination socket or default destination sockets then:
The following rules apply when sending broadcast datagrams through sockets from the Internet Address Family:
The sixth argument passed to the sendto function is the length of the name of the destination socket or sockets, that was specified by the fifth argument. This argument is ignored when sending data through connection-oriented sockets.
The sendto function returns the number of bytes sent through the socket, if the call is successful. If the call fails the value SOCKET_ERROR is returned, and in this case you can use the WSAGetLastError function to retrieve a code that identifies the error that caused the call to fail. NOTE: The constants SOCKET_ERROR, WSAEACCES, SO_BROADCAST, MSG_DONTROUTE, MSG_OOB, WSAEMSGSIZE, and INADDR_BROADCAST are declared in the system include file WinSock2.inc.
The number of bytes sent through the socket can be less than the number of bytes you are trying to send. Also the fact that data has been successfully sent does not guarantee that the data was successfully received.
The authoritative source of information about the WinSock2 library is the Microsoft Developers Network (MSDN). You can access the MSDN on the Microsoft website at msdn.microsoft.com.