[ Team LiB ] Previous Section Next Section

14.10 Summary

There are three main ways to set a time limit on a socket operation:

  • Use the alarm function and the SIGALRM signal

  • Use the time limit that is provided by select

  • Use the newer SO_RCVTIMEO and SO_SNDTIMEO socket options

The first is easy to use, but involves signal handling, and as we will see in Section 20.5, can lead to race conditions. Using select means that we block in this function with its provided time limit instead of blocking in a call to read, write, or connect. The third alternative, to use the new socket options, is also easy, but is not provided by all implementations.

recvmsg and sendmsg are the most general of the five groups of I/O functions provided. They combine the ability to specify an MSG_xxx flag (from recv and send), plus employ the ability to return or specify the peer's protocol address (from recvfrom and sendto), with the ability to use multiple buffers (from readv and writev), along with two new features: returning flags to the application and receiving or sending ancillary data.

We describe ten different forms of ancillary data in the text, six of which are new with IPv6. Ancillary data consists of one or more ancillary data objects, each object preceded by a cmsghdr structure specifying its length, protocol level, and type of data. Five functions beginning with CMSG_ are used to build and parse ancillary data.

Sockets can be used with the C standard I/O library, but doing this adds another level of buffering to that already being performed by TCP. Indeed, a lack of understanding of the buffering performed by the standard I/O library is the most common problem with the library. Since a socket is not a terminal device, the common solution to this potential problem is to set the standard I/O stream to unbuffered, or to simply avoid standard I/O on sockets completely.

Many vendors provide advanced ways to poll for many events without the overhead required by select and poll. While writing nonportable code should be avoided whenever possible, sometimes the benefits of performance improvements outweigh the risk of nonportability.

    [ Team LiB ] Previous Section Next Section