0% found this document useful (0 votes)
7 views3 pages

Socket I/O Readiness with fd_set

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Socket I/O Readiness with fd_set

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

void FD_ZERO(fd_set *fdset); /* clear all bits in fdset */

void FD_SET(int fd, fd_set *fdset); /* turn on the bit for fd in fdset
*/
void FD_CLR(int fd, fd_set *fdset); /* turn off the bit for fd in
fdset */
int FD_ISSET(int fd, fd_set *fdset); /* is the bit for fd on in fdset ?
*/
d_set rset;

FD_ZERO(&rset); /* initialize the set: all bits off */


FD_SET(1, &rset); /* turn on bit for fd 1 */
FD_SET(4, &rset); /* turn on bit for fd 4 */
FD_SET(5, &rset); /* turn on bit for fd 5 */

Conditions for a Ready Descriptor


Previous sections discusses waiting for a descriptor to become ready for I/O (reading or
writing) or to have an exception condition pending on it (out-of-band data). The following
discussion are specific about the conditions that cause select to return "ready" for
sockets

1. A socket is ready for reading if any of the following four conditions is true:
o The number of bytes of data in the socket receive buffer is greater than or
equal to the current size of the low-water mark for the socket receive
buffer. A read operation on the socket will not block and will return a value
greater than 0 (i.e., the data that is ready to be read). We can set this low-
water mark using the SO_RCVLOWAT socket option. It defaults to 1 for TCP
and UDP sockets.
o The read half of the connection is closed (i.e., a TCP connection that has
received a FIN). A read operation on the socket will not block and will
return 0 (i.e., EOF).
o The socket is a listening socket and the number of completed connections
is nonzero.
o A socket error is pending. A read operation on the socket will not block
and will return an error (–1) with errno set to the specific error condition.
These pending errors can also be fetched and cleared by
calling getsockopt and specifying the SO_ERROR socket option.
2. A socket is ready for writing if any of the following four conditions is true:
o The number of bytes of available space in the socket send buffer is
greater than or equal to the current size of the low-water mark for the
socket send buffer and either: (i) the socket is connected, or (ii) the socket
does not require a connection (e.g., UDP). This means that if we set the
socket to nonblocking (Chapter 16), a write operation will not block and
will return a positive value (e.g., the number of bytes accepted by the
transport layer). We can set this low-water mark using
the SO_SNDLOWAT socket option. This low-water mark normally defaults to
2048 for TCP and UDP sockets.
o The write half of the connection is closed. A write operation on the socket
will generate SIGPIPE (Section 5.12).
o A socket using a non-blocking connect has completed the connection, or
the connect has failed.
o A socket error is pending. A write operation on the socket will not block
and will return an error (–1) with errno set to the specific error condition.
These pending errors can also be fetched and cleared by calling
getsockopt with the SO_ERROR socket option.
3. A socket has an exception condition pending if there is out-of-band data for
the socket or the socket is still at the out-of-band mark

void
str_cli(FILE *fp, int sockfd)
{
int maxfdp1;
fd_set rset;
char sendline[MAXLINE], recvline[MAXLINE];
FD_ZERO(&rset);
for ( ; ; ) {
FD_SET(fileno(fp), &rset);
FD_SET(sockfd, &rset);
maxfdp1 = max(fileno(fp), sockfd) + 1;
Select(maxfdp1, &rset, NULL, NULL, NULL);

if (FD_ISSET(sockfd, &rset)) { /* socket is readable */


if (Readline(sockfd, recvline, MAXLINE) == 0)
err_quit("str_cli: server terminated prematurely");
Fputs(recvline, stdout);
}

You might also like