The socket call sends data on a socket.
Syntax
#include <types.h> #include <sys\socket.h> int sendto(s, msg, len, flags, to, tolen) int s; char *msg; int len; int flags; struct sockaddr *to; int tolen;
Parameters
s
MSG_DONTROUTE
Description
This call sends data on the socket with descriptor s. The sendto() call applies to connected or unconnected sockets. For unconnected datagram and raw sockets, the sendto() call sends data to the specified destination address. For stream and sequenced packet sockets the destination address is ignored.
To broadcast on a socket, first issue a setsockopt() call using the SO_BROADCAST option to gain broadcast permissions.
Provide the address of the target using the to parameter. Specify the length of the message with the tolen parameter. If the message is too long to pass through the underlying protocol, the error SOCEMSGSIZE is returned and the message is not transmitted.
If the sending socket has no space to hold the message to be transmitted, the sendto() call blocks the message, unless the socket is in a nonblocking I/O mode.
Use the select() call to determine when it is possible to send more data.
Datagram sockets are connected by calling connect(). This identifies the peer to send/receive the datagram. Once a datagram socket is connected to a peer, you may still use the sendto() call but a destination address cannot be included.
To change the peer address when using connected datagram sockets, issue a connect() call with a null address. Specifying a null address on a connected datagram socket removes the peer address specification. You can then either issue a sendto() call specifying a different destination address or issue a connect() call to connect to a different peer. For more information on connecting datagram sockets and specifying null addresses, see Datagram or Raw Sockets.
If the to parameter is specified and this sendto() call was preceded by a connect() call, the dst parameter must be NULL. If not NULL, the error SOCEISCONN is returned and the message is not sent. If the to parameter is specified and this sendto() call was not preceded by a connect() call, this sendto() call results in socket s being connected to dst, the message being sent, and socket s being disconnected from dst.
Return Values
When successful, the number of bytes of data sent is returned. Successful completion does not guarantee delivery of the data to the receiver. The return value -1 indicates an error was detected on the sending side. You can get the specific error code by calling sock_errno() or psock_errno().
Error Code
Related Calls
getsockopt() readv()
recv()
recvfrom()
recvmsg()
select()
send()
sendmsg()
setsockopt()
shutdown()
sock_errno()
socket()
writev()