1 .\" $NetBSD: 2.t,v 1.3 2003/08/07 10:30:51 agc Exp $
3 .\" Copyright (c) 1986, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)2.t 8.2 (Berkeley) 6/1/94
35 .\" The next line is a major hack to get around internal changes in the groff
36 .\" implementation of .NH.
48 The basic building block for communication is the \fIsocket\fP.
49 A socket is an endpoint of communication to which a name may
50 be \fIbound\fP. Each socket in use has a \fItype\fP
51 and one or more associated processes. Sockets exist within
52 \fIcommunication domains\fP.
53 A communication domain is an
54 abstraction introduced to bundle common properties of
55 processes communicating through sockets.
56 One such property is the scheme used to name sockets. For
57 example, in the UNIX communication domain sockets are
58 named with UNIX path names; e.g. a
59 socket may be named \*(lq/dev/foo\*(rq. Sockets normally
60 exchange data only with
61 sockets in the same domain (it may be possible to cross domain
62 boundaries, but only if some translation process is
64 4.4BSD IPC facilities support four separate communication domains:
65 the UNIX domain, for on-system communication;
66 the Internet domain, which is used by
67 processes which communicate
68 using the Internet standard communication protocols;
69 the NS domain, which is used by processes which
70 communicate using the Xerox standard communication
73 * See \fIInternet Transport Protocols\fP, Xerox System Integration
74 Standard (XSIS)028112 for more information. This document is
75 almost a necessity for one trying to write NS applications.
77 and the ISO OSI protocols, which are not documented in this tutorial.
78 The underlying communication
79 facilities provided by these domains have a significant influence
80 on the internal system implementation as well as the interface to
81 socket facilities available to a user. An example of the
82 latter is that a socket \*(lqoperating\*(rq in the UNIX domain
83 sees a subset of the error conditions which are possible
84 when operating in the Internet (or NS) domain.
89 typed according to the communication properties visible to a
91 Processes are presumed to communicate only between sockets of
92 the same type, although there is
93 nothing that prevents communication between sockets of different
94 types should the underlying communication
95 protocols support this.
97 Four types of sockets currently are available to a user.
98 A \fIstream\fP socket provides for the bidirectional, reliable,
99 sequenced, and unduplicated flow of data without record boundaries.
100 Aside from the bidirectionality of data flow, a pair of connected
101 stream sockets provides an interface nearly identical to that of pipes\(dg.
103 \(dg In the UNIX domain, in fact, the semantics are identical and,
104 as one might expect, pipes have been implemented internally
105 as simply a pair of connected stream sockets.
108 A \fIdatagram\fP socket supports bidirectional flow of data which
109 is not promised to be sequenced, reliable, or unduplicated.
111 receiving messages on a datagram socket may find messages duplicated,
113 in an order different from the order in which it was sent.
114 An important characteristic of a datagram
115 socket is that record boundaries in data are preserved. Datagram
116 sockets closely model the facilities found in many contemporary
117 packet switched networks such as the Ethernet.
119 A \fIraw\fP socket provides users access to
120 the underlying communication
121 protocols which support socket abstractions.
122 These sockets are normally datagram oriented, though their
123 exact characteristics are dependent on the interface provided by
124 the protocol. Raw sockets are not intended for the general user; they
125 have been provided mainly for those interested in developing new
126 communication protocols, or for gaining access to some of the more
127 esoteric facilities of an existing protocol. The use of raw sockets
128 is considered in section 5.
130 A \fIsequenced packet\fP socket is similar to a stream socket,
131 with the exception that record boundaries are preserved. This
132 interface is provided only as part of the NS socket abstraction,
133 and is very important in most serious NS applications.
134 Sequenced-packet sockets allow the user to manipulate the
135 SPP or IDP headers on a packet or a group of packets either
136 by writing a prototype header along with whatever data is
137 to be sent, or by specifying a default header to be used with
138 all outgoing data, and allows the user to receive the headers
139 on incoming packets. The use of these options is considered in
142 Another potential socket type which has interesting properties is
143 the \fIreliably delivered
145 The reliably delivered message socket has
146 similar properties to a datagram socket, but with
147 reliable delivery. There is currently no support for this
148 type of socket, but a reliably delivered message protocol
149 similar to Xerox's Packet Exchange Protocol (PEX) may be
150 simulated at the user level. More information on this topic
151 can be found in section 5.
156 To create a socket the \fIsocket\fP system call is used:
158 s = socket(domain, type, protocol);
160 This call requests that the system create a socket in the specified
161 \fIdomain\fP and of the specified \fItype\fP. A particular protocol may
162 also be requested. If the protocol is left unspecified (a value
163 of 0), the system will select an appropriate protocol from those
164 protocols which comprise the communication domain and which
165 may be used to support the requested socket type. The user is
166 returned a descriptor (a small integer number) which may be used
167 in later system calls which operate on sockets. The domain is specified as
168 one of the manifest constants defined in the file <\fIsys/socket.h\fP>.
169 For the UNIX domain the constant is AF_UNIX*; for the Internet
171 * The manifest constants are named AF_whatever as they indicate
172 the ``address format'' to use in interpreting names.
174 domain AF_INET; and for the NS domain, AF_NS.
175 The socket types are also defined in this file
176 and one of SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, or SOCK_SEQPACKET
178 To create a stream socket in the Internet domain the following
181 s = socket(AF_INET, SOCK_STREAM, 0);
183 This call would result in a stream socket being created with the TCP
184 protocol providing the underlying communication support. To
185 create a datagram socket for on-machine use the call might
188 s = socket(AF_UNIX, SOCK_DGRAM, 0);
191 The default protocol (used when the \fIprotocol\fP argument to the
192 \fIsocket\fP call is 0) should be correct for most every
193 situation. However, it is possible to specify a protocol
194 other than the default; this will be covered in
197 There are several reasons a socket call may fail. Aside from
198 the rare occurrence of lack of memory (ENOBUFS), a socket
199 request may fail due to a request for an unknown protocol
200 (EPROTONOSUPPORT), or a request for a type of socket for
201 which there is no supporting protocol (EPROTOTYPE).
205 A socket is created without a name. Until a name is bound
206 to a socket, processes have no way to reference it and, consequently,
207 no messages may be received on it.
208 Communicating processes are bound
209 by an \fIassociation\fP. In the Internet and NS domains,
211 is composed of local and foreign
212 addresses, and local and foreign ports,
213 while in the UNIX domain, an association is composed of
214 local and foreign path names (the phrase ``foreign pathname''
215 means a pathname created by a foreign process, not a pathname
216 on a foreign system).
217 In most domains, associations must be unique.
218 In the Internet domain there
219 may never be duplicate <protocol, local address, local port, foreign
220 address, foreign port> tuples. UNIX domain sockets need not always
221 be bound to a name, but when bound
222 there may never be duplicate <protocol, local pathname, foreign
224 The pathnames may not refer to files
225 already existing on the system
226 in 4.3; the situation may change in future releases.
228 The \fIbind\fP system call allows a process to specify half of
229 an association, <local address, local port>
230 (or <local pathname>), while the \fIconnect\fP
231 and \fIaccept\fP primitives are used to complete a socket's association.
233 In the Internet domain,
234 binding names to sockets can be fairly complex.
235 Fortunately, it is usually not necessary to specifically bind an
236 address and port number to a socket, because the
237 \fIconnect\fP and \fIsend\fP calls will automatically
238 bind an appropriate address if they are used with an
239 unbound socket. The process of binding names to NS
240 sockets is similar in most ways to that of
241 binding names to Internet sockets.
243 The \fIbind\fP system call is used as follows:
245 bind(s, name, namelen);
247 The bound name is a variable length byte string which is interpreted
248 by the supporting protocol(s). Its interpretation may vary from
249 communication domain to communication domain (this is one of
250 the properties which comprise the \*(lqdomain\*(rq).
252 Internet domain names contain an Internet address and port
253 number. NS domain names contain an NS address and
254 port number. In the UNIX domain, names contain a path name and
255 a family, which is always AF_UNIX. If one wanted to bind
256 the name \*(lq/tmp/foo\*(rq to a UNIX domain socket, the
257 following code would be used*:
259 * Note that, although the tendency here is to call the \*(lqaddr\*(rq
260 structure \*(lqsun\*(rq, doing so would cause problems if the code
261 were ever ported to a Sun workstation.
266 struct sockaddr_un addr;
268 strcpy(addr.sun_path, "/tmp/foo");
269 addr.sun_family = AF_UNIX;
270 bind(s, (struct sockaddr *) &addr, strlen(addr.sun_path) +
271 sizeof (addr.sun_len) + sizeof (addr.sun_family));
273 Note that in determining the size of a UNIX domain address null
274 bytes are not counted, which is why \fIstrlen\fP is used. In
275 the current implementation of UNIX domain IPC,
277 referred to in \fIaddr.sun_path\fP is created as a socket
278 in the system file space.
279 The caller must, therefore, have
280 write permission in the directory where
281 \fIaddr.sun_path\fP is to reside, and this file should be deleted by the
282 caller when it is no longer needed. Future versions of 4BSD
283 may not create this file.
285 In binding an Internet address things become more
286 complicated. The actual call is similar,
288 #include <sys/types.h>
289 #include <netinet/in.h>
291 struct sockaddr_in sin;
293 bind(s, (struct sockaddr *) &sin, sizeof (sin));
295 but the selection of what to place in the address \fIsin\fP
296 requires some discussion. We will come back to the problem
297 of formulating Internet addresses in section 3 when
298 the library routines used in name resolution are discussed.
300 Binding an NS address to a socket is even more
302 especially since the Internet library routines do not
303 work with NS hostnames. The actual call is again similar:
305 #include <sys/types.h>
306 #include <netns/ns.h>
308 struct sockaddr_ns sns;
310 bind(s, (struct sockaddr *) &sns, sizeof (sns));
312 Again, discussion of what to place in a \*(lqstruct sockaddr_ns\*(rq
313 will be deferred to section 3.
315 Connection establishment
317 Connection establishment is usually asymmetric,
318 with one process a \*(lqclient\*(rq and the other a \*(lqserver\*(rq.
319 The server, when willing to offer its advertised services,
320 binds a socket to a well-known address associated with the service
321 and then passively \*(lqlistens\*(rq on its socket.
322 It is then possible for an unrelated process to rendezvous
324 The client requests services from the server by initiating a
325 \*(lqconnection\*(rq to the server's socket.
326 On the client side the \fIconnect\fP call is
327 used to initiate a connection. Using the UNIX domain, this
330 struct sockaddr_un server;
332 connect(s, (struct sockaddr *)&server, strlen(server.sun_path) +
333 sizeof (server.sun_family));
335 while in the Internet domain,
337 struct sockaddr_in server;
339 connect(s, (struct sockaddr *)&server, sizeof (server));
341 and in the NS domain,
343 struct sockaddr_ns server;
345 connect(s, (struct sockaddr *)&server, sizeof (server));
347 where \fIserver\fP in the example above would contain either the UNIX
348 pathname, Internet address and port number, or NS address and
349 port number of the server to which the
350 client process wishes to speak.
351 If the client process's socket is unbound at the time of
353 the system will automatically select and bind a name to
354 the socket if necessary; c.f. section 5.4.
355 This is the usual way that local addresses are bound
358 An error is returned if the connection was unsuccessful
359 (any name automatically bound by the system, however, remains).
360 Otherwise, the socket is associated with the server and
361 data transfer may begin. Some of the more common errors returned
362 when a connection attempt fails are:
365 After failing to establish a connection for a period of time,
366 the system decided there was no point in retrying the
367 connection attempt any more. This usually occurs because
368 the destination host is down, or because problems in
369 the network resulted in transmissions being lost.
372 The host refused service for some reason.
374 due to a server process
375 not being present at the requested name.
376 .IP "ENETDOWN or EHOSTDOWN"
378 These operational errors are
379 returned based on status information delivered to
380 the client host by the underlying communication services.
381 .IP "ENETUNREACH or EHOSTUNREACH"
383 These operational errors can occur either because the network
384 or host is unknown (no route to the network or host is present),
385 or because of status information returned by intermediate
386 gateways or switching nodes. Many times the status returned
387 is not sufficient to distinguish a network being down from a
388 host being down, in which case the system
389 indicates the entire network is unreachable.
391 For the server to receive a client's connection it must perform
392 two steps after binding its socket.
393 The first is to indicate a willingness to listen for
394 incoming connection requests:
398 The second parameter to the \fIlisten\fP call specifies the maximum
399 number of outstanding connections which may be queued awaiting
400 acceptance by the server process; this number
401 may be limited by the system. Should a connection be
402 requested while the queue is full, the connection will not be
403 refused, but rather the individual messages which comprise the
404 request will be ignored. This gives a harried server time to
405 make room in its pending connection queue while the client
406 retries the connection request. Had the connection been returned
407 with the ECONNREFUSED error, the client would be unable to tell
408 if the server was up or not. As it is now it is still possible
409 to get the ETIMEDOUT error back, though this is unlikely. The
410 backlog figure supplied with the listen call is currently limited
411 by the system to a maximum of 5 pending connections on any
412 one queue. This avoids the problem of processes hogging system
413 resources by setting an infinite backlog, then ignoring
414 all connection requests.
416 With a socket marked as listening, a server may \fIaccept\fP
419 struct sockaddr_in from;
421 fromlen = sizeof (from);
422 newsock = accept(s, (struct sockaddr *)&from, &fromlen);
424 (For the UNIX domain, \fIfrom\fP would be declared as a
425 \fIstruct sockaddr_un\fP, and for the NS domain, \fIfrom\fP
426 would be declared as a \fIstruct sockaddr_ns\fP,
427 but nothing different would need
428 to be done as far as \fIfromlen\fP is concerned. In the examples
429 which follow, only Internet routines will be discussed.) A new
430 descriptor is returned on receipt of a connection (along with
431 a new socket). If the server wishes to find out who its client is,
432 it may supply a buffer for the client socket's name. The value-result
433 parameter \fIfromlen\fP is initialized by the server to indicate how
434 much space is associated with \fIfrom\fP, then modified on return
435 to reflect the true size of the name. If the client's name is not
436 of interest, the second parameter may be a null pointer.
438 \fIAccept\fP normally blocks. That is, \fIaccept\fP
439 will not return until a connection is available or the system call
440 is interrupted by a signal to the process. Further, there is no
441 way for a process to indicate it will accept connections from only
442 a specific individual, or individuals. It is up to the user process
443 to consider who the connection is from and close down the connection
444 if it does not wish to speak to the process. If the server process
445 wants to accept connections on more than one socket, or wants to avoid blocking
446 on the accept call, there are alternatives; they will be considered
451 With a connection established, data may begin to flow. To send
452 and receive data there are a number of possible calls.
453 With the peer entity at each end of a connection
454 anchored, a user can send or receive a message without specifying
455 the peer. As one might expect, in this case, then
456 the normal \fIread\fP and \fIwrite\fP system calls are usable,
458 write(s, buf, sizeof (buf));
459 read(s, buf, sizeof (buf));
461 In addition to \fIread\fP and \fIwrite\fP,
462 the new calls \fIsend\fP and \fIrecv\fP
465 send(s, buf, sizeof (buf), flags);
466 recv(s, buf, sizeof (buf), flags);
468 While \fIsend\fP and \fIrecv\fP are virtually identical to
469 \fIread\fP and \fIwrite\fP,
470 the extra \fIflags\fP argument is important. The flags,
471 defined in \fI<sys/socket.h>\fP, may be
472 specified as a non-zero value if one or more
473 of the following is required:
477 MSG_OOB send/receive out of band data
478 MSG_PEEK look at data without reading
479 MSG_DONTROUTE send data without routing packets
482 Out of band data is a notion specific to stream sockets, and one
483 which we will not immediately consider. The option to have data
484 sent without routing applied to the outgoing packets is currently
485 used only by the routing table management process, and is
486 unlikely to be of interest to the casual user. The ability
487 to preview data is, however, of interest. When MSG_PEEK
488 is specified with a \fIrecv\fP call, any data present is returned
489 to the user, but treated as still \*(lqunread\*(rq. That
490 is, the next \fIread\fP or \fIrecv\fP call applied to the socket will
491 return the data previously previewed.
495 Once a socket is no longer of interest, it may be discarded
496 by applying a \fIclose\fP to the descriptor,
500 If data is associated with a socket which promises reliable delivery
501 (e.g. a stream socket) when a close takes place, the system will
502 continue to attempt to transfer the data.
503 However, after a fairly long period of
504 time, if the data is still undelivered, it will be discarded.
505 Should a user have no use for any pending data, it may
506 perform a \fIshutdown\fP on the socket prior to closing it.
507 This call is of the form:
511 where \fIhow\fP is 0 if the user is no longer interested in reading
512 data, 1 if no more data will be sent, or 2 if no data is to
515 Connectionless sockets
517 To this point we have been concerned mostly with sockets which
518 follow a connection oriented model. However, there is also
519 support for connectionless interactions typical of the datagram
520 facilities found in contemporary packet switched networks.
521 A datagram socket provides a symmetric interface to data
522 exchange. While processes are still likely to be client
523 and server, there is no requirement for connection establishment.
524 Instead, each message includes the destination address.
526 Datagram sockets are created as before.
527 If a particular local address is needed,
528 the \fIbind\fP operation must precede the first data transmission.
529 Otherwise, the system will set the local address and/or port
530 when data is first sent.
531 To send data, the \fIsendto\fP primitive is used,
533 sendto(s, buf, buflen, flags, (struct sockaddr *)&to, tolen);
535 The \fIs\fP, \fIbuf\fP, \fIbuflen\fP, and \fIflags\fP
536 parameters are used as before.
537 The \fIto\fP and \fItolen\fP
538 values are used to indicate the address of the intended recipient of the
540 using an unreliable datagram interface, it is
541 unlikely that any errors will be reported to the sender. When
542 information is present locally to recognize a message that can
543 not be delivered (for instance when a network is unreachable),
544 the call will return \-1 and the global value \fIerrno\fP will
545 contain an error number.
547 To receive messages on an unconnected datagram socket, the
548 \fIrecvfrom\fP primitive is provided:
550 recvfrom(s, buf, buflen, flags, (struct sockaddr *)&from, &fromlen);
552 Once again, the \fIfromlen\fP parameter is handled in
553 a value-result fashion, initially containing the size of
554 the \fIfrom\fP buffer, and modified on return to indicate
555 the actual size of the address from which the datagram was received.
557 In addition to the two calls mentioned above, datagram
558 sockets may also use the \fIconnect\fP call to associate
559 a socket with a specific destination address. In this case, any
560 data sent on the socket will automatically be addressed
561 to the connected peer, and only data received from that
562 peer will be delivered to the user. Only one connected
563 address is permitted for each socket at one time;
564 a second connect will change the destination address,
565 and a connect to a null address (family AF_UNSPEC)
567 Connect requests on datagram sockets return immediately,
568 as this simply results in the system recording
569 the peer's address (as compared to a stream socket, where a
570 connect request initiates establishment of an end to end
571 connection). \fIAccept\fP and \fIlisten\fP are not
572 used with datagram sockets.
574 While a datagram socket socket is connected,
575 errors from recent \fIsend\fP calls may be returned
577 These errors may be reported on subsequent operations
579 or a special socket option used with \fIgetsockopt\fP, SO_ERROR,
580 may be used to interrogate the error status.
581 A \fIselect\fP for reading or writing will return true
582 when an error indication has been received.
583 The next operation will return the error, and the error status is cleared.
585 important details of datagram sockets are described
588 Input/Output multiplexing
590 One last facility often used in developing applications
591 is the ability to multiplex i/o requests among multiple
592 sockets and/or files. This is done using the \fIselect\fP
595 #include <sys/time.h>
596 #include <sys/types.h>
599 fd_set readmask, writemask, exceptmask;
600 struct timeval timeout;
602 select(nfds, &readmask, &writemask, &exceptmask, &timeout);
604 \fISelect\fP takes as arguments pointers to three sets, one for
605 the set of file descriptors for which the caller wishes to
606 be able to read data on, one for those descriptors to which
607 data is to be written, and one for which exceptional conditions
608 are pending; out-of-band data is the only
609 exceptional condition currently implemented by the socket
610 If the user is not interested
611 in certain conditions (i.e., read, write, or exceptions),
612 the corresponding argument to the \fIselect\fP should
615 Each set is actually a structure containing an array of
616 long integer bit masks; the size of the array is set
617 by the definition FD_SETSIZE.
619 long enough to hold one bit for each of FD_SETSIZE file descriptors.
621 The macros FD_SET(\fIfd, &mask\fP) and
622 FD_CLR(\fIfd, &mask\fP)
623 have been provided for adding and removing file descriptor
624 \fIfd\fP in the set \fImask\fP. The
625 set should be zeroed before use, and
626 the macro FD_ZERO(\fI&mask\fP) has been provided
627 to clear the set \fImask\fP.
628 The parameter \fInfds\fP in the \fIselect\fP call specifies the range
629 of file descriptors (i.e. one plus the value of the largest
630 descriptor) to be examined in a set.
632 A timeout value may be specified if the selection
633 is not to last more than a predetermined period of time. If
634 the fields in \fItimeout\fP are set to 0, the selection takes
636 \fIpoll\fP, returning immediately. If the last parameter is
637 a null pointer, the selection will block indefinitely*.
639 * To be more specific, a return takes place only when a
640 descriptor is selectable, or when a signal is received by
641 the caller, interrupting the system call.
643 \fISelect\fP normally returns the number of file descriptors selected;
644 if the \fIselect\fP call returns due to the timeout expiring, then
645 the value 0 is returned.
646 If the \fIselect\fP terminates because of an error or interruption,
647 a \-1 is returned with the error number in \fIerrno\fP,
648 and with the file descriptor masks unchanged.
650 Assuming a successful return, the three sets will
652 file descriptors are ready to be read from, written to, or
653 have exceptional conditions pending.
654 The status of a file descriptor in a select mask may be
655 tested with the \fIFD_ISSET(fd, &mask)\fP macro, which
656 returns a non-zero value if \fIfd\fP is a member of the set
657 \fImask\fP, and 0 if it is not.
659 To determine if there are connections waiting
660 on a socket to be used with an \fIaccept\fP call,
661 \fIselect\fP can be used, followed by
662 a \fIFD_ISSET(fd, &mask)\fP macro to check for read
663 readiness on the appropriate socket. If \fIFD_ISSET\fP
664 returns a non-zero value, indicating permission to read, then a
665 connection is pending on the socket.
667 As an example, to read data from two sockets, \fIs1\fP and
668 \fIs2\fP as it is available from each and with a one-second
669 timeout, the following code
672 #include <sys/time.h>
673 #include <sys/types.h>
675 fd_set read_template;
679 wait.tv_sec = 1; /* one second */
682 FD_ZERO(&read_template);
684 FD_SET(s1, &read_template);
685 FD_SET(s2, &read_template);
687 nb = select(s1 > s2 ? s1 + 1 : s2 + 1, &read_template, (fd_set *) 0,
688 (fd_set *) 0, &wait);
690 \fIAn error occurred during the \fPselect\fI, or
691 the \fPselect\fI timed out.\fP
694 if (FD_ISSET(s1, &read_template)) {
695 \fISocket #1 is ready to be read from.\fP
698 if (FD_ISSET(s2, &read_template)) {
699 \fISocket #2 is ready to be read from.\fP
704 In 4.2, the arguments to \fIselect\fP were pointers to integers
705 instead of pointers to \fIfd_set\fPs. This type of call
706 will still work as long as the number of file descriptors
707 being examined is less than the number of bits in an
708 integer; however, the methods illustrated above should
709 be used in all current programs.
711 \fISelect\fP provides a synchronous multiplexing scheme.
712 Asynchronous notification of output completion, input availability,
713 and exceptional conditions is possible through use of the
714 SIGIO and SIGURG signals described in section 5.