Input/Output Multiplexing
Requests can be multiplexed among multiple sockets or files. Use select(3C) to multiplex:
#include <sys/time.h> #include <sys/types.h> #include <sys/select.h> ... fd_set readmask, writemask, exceptmask; struct timeval timeout; ... select(nfds, &readmask, &writemask, &exceptmask, &timeout); |
The first argument of select(3C) is the number of file descriptors in the lists pointed to by the next three arguments.
The second, third, and fourth arguments of select(3C) point to three sets of file descriptors: a set of descriptors to read on, a set to write on, and a set on which exception conditions are accepted. Out-of-band data is the only exceptional condition. You can designate any of these pointers as a properly cast null. Each set is a structure containing an array of long integer bit masks. Set the size of the array with FD_SETSIZE (defined in select.h). The array is long enough to hold one bit for each FD_SETSIZE file descriptor.
The macros FD_SET (fd, &mask) and FD_CLR (fd, &mask) add and delete, respectively, the file descriptor fd in the set mask. The set should be zeroed before use and the macro FD_ZERO (&mask) clears the set mask.
The fifth argument of select(3C) enables the specification of a time-out value. If the timeout pointer is NULL, select(3C) blocks until a descriptor is selectable, or until a signal is received. If the fields in timeout are set to 0, select(3C) polls and returns immediately.
The select(3C) routine normally returns the number of file descriptors selected, or a 0 if the time-out has expired. The select(3C) routine returns -1 for an error or interrupt, with the error number in errno and the file descriptor masks unchanged. For a successful return, the three sets indicate which file descriptors are ready to be read from, written to, or have exceptional conditions pending.
Test the status of a file descriptor in a select mask with the FD_ISSET (fd, &mask) macro. It returns a nonzero value if fd is in the set mask and 0 if it is not. Use select(3C) followed by a FD_ISSET (fd, &mask) macro on the read set to check for queued connect requests on a socket.
The following example shows how to select on a listening socket for readability to determine when a new connection can be picked up with a call to accept(3SOCKET). The program accepts connection requests, reads data, and disconnects on a single socket.
Example 6-3 Using select(3C) to Check for Pending Connections
In previous versions of the select(3C) routine, its arguments were pointers to integers instead of pointers to fd_sets. This style of call still works if the number of file descriptors is smaller than the number of bits in an integer.
The select(3C) routine provides a synchronous multiplexing scheme. The SIGIO and SIGURG signals (described in "Advanced Socket Topics") provide asynchronous notification of output completion, input availability, and exceptional conditions.
Datagram Sockets
A datagram socket provides a symmetric data exchange interface without requiring connection establishment. Each message carries the destination address. The following figure shows the flow of communication between server and client.
The bind(3SOCKET) step for the server is optional.
Figure 6-2 Connectionless Communication Using Datagram Sockets
Create datagram sockets as described in "Socket Creation". If a particular local address is needed, the bind(3SOCKET) operation must precede the first data transmission. Otherwise, the system sets the local address and/or port when data is first sent. Use sendto(3SOCKET) to send data.
sendto(s, buf, buflen, flags, (struct sockaddr *) &to, tolen); |
The s, buf, buflen, and flags parameters are the same as in connection-oriented sockets. The to and tolen values indicate the address of the intended recipient of the message. A locally detected error condition (such as an unreachable network) causes a return of -1 and errno to be set to the error number.
recvfrom(s, buf, buflen, flags, (struct sockaddr *) &from, &fromlen); |
To receive messages on a datagram socket, recvfrom(3SOCKET) is used. Before the call, fromlen is set to the size of the from buffer. On return, fromlen is set to the size of the address from which the datagram was received.
Datagram sockets can also use the connect(3SOCKET) call to associate a socket with a specific destination address. The socket can then use the send(3SOCKET) call. Any data sent on the socket that does not explicitly specify a destination address is addressed to the connected peer and only data received from that peer is delivered. A socket can have only one connected address at a time. A second connect(3SOCKET) call changes the destination address. Connect requests on datagram sockets return immediately. The system records the peer's address. Neither accept(3SOCKET) nor listen(3SOCKET) are used with datagram sockets.
A datagram socket can return errors from previous send(3SOCKET) calls asynchronously while the socket is connected. The socket can report these errors on subsequent socket operations. Alternately, the socket can use an option of getsockopt(3SOCKET), SO_ERROR to interrogate the error status.
The following example code shows how to send an Internet call by creating a socket, binding a name to the socket, and sending the message to the socket.
Example 6-4 Sending an Internet Family Datagram