Saturday, August 13, 2011

socket sendto(2) Function

The sendto(2) function allows you to write a datagram and specify the destination address of the recipient at the same time. The function synopsis is as follows:

#include <sys/types.h>
#include <sys/socket.h>
int sendto(int s,
           const void *msg,
           unsigned flags,
           const struct sockaddr *to,
           int tolen);

Don't let the number of arguments intimidate you about this function. They are quite easy to understand, after they are described:
  1. The first argument s is the socket number. You received this value from the socket(2) function.
  2. Argument msg is a pointer to the buffer holding the datagram message that you wish to send.
  3. Argument len is the length, in bytes, of the datagram that starts at the pointer given by msg.
  4. The flags argument allows you to specify some option bits. In many cases, you will simply supply a value of zero.
  5. The argument to is a pointer to a generic socket address that you have established. This is the address of the recipient of the datagram.
  6. Argument tolen is the length of the address argument to.

The value returned by the function sendto(2), when successful, is the number of bytes sent (note that this is no guarantee that they were received at the remote end.) When an error occurs, the
function returns a value of -1 and the value errno can be consulted to find out why. The second-to-last function argument allows you to identify where the datagram must be sent. The argument to must point to a valid socket address, and the argument tolen should contain the correct length of the address. You have become an expert at forming addresses by reading my preceding posts, so you should feel right at home here.
Table below lists the values that the flags argument can have, though most of the time you will simply supply the value zero instead.

Normal— no special options
Processes out-of-band data
Bypasses routing; uses direct
Does not block; waiting to write
Does not raise SIGPIPE when the other end has disconnected

Always use the macro names, such as MSG_DONTWAIT for example, when specifying constants and flag bits. While Table above shows the hexadecimal values for each C macro constant, only use the macro name. In this manner, if a change to the constant should be made in a later release of Linux, your program can simply be recompiled and still function correctly without any source code modification.

The man page for sendto(2) for Red Hat Linux 6.0 lists the value of C macro constant MSG_NOSIGNAL as 0x2000. However, the
include/linux/socket.h file within the kernel sources for 2.2.10 lists the value as 0x4000. A comment in this include file also indicates that not all of the flags listed there are supported yet.
Many of the flag values listed in Table below are for advanced use and won't be covered at this time.They have been listed here for your convenience as a reference. Normally, you will simply supply the value zero. Later in this post, you will put the sendto(2) function to work. Before you do this, however, you should learn about the recvfrom(2) function. Follow the link to understand the function.

Using sendto function

Example code using sendto function

1 comment: