Sunday, September 4, 2011

fd_isset socket


Testing File Descriptors with FD_ISSET Macro

It is necessary, at times, to test to see if a particular file descriptor is present within a set (that is, to see if its corresponding bit has been set to one). To test whether socket c is set, you could write the following code:

Example

int c;             /* Client socket */
fd_set read_socks; /* Read set */
. . .
if ( FD_ISSET(c, &read_socks) ) {
   /* Socket c is in the set */
   . . .
}
else {
   /* Socket c is not in the set */
   . . .
}

The if statement shown invokes the macro FD_ISSET to test if the socket c is present in the file descriptor set read_socks. If the test returns true, then the socket c does have its corresponding
bit enabled within the set, and the first block of C code is executed. Otherwise, the socket c is not considered part of the set, and the else block of statements is executed instead.

No comments:

Post a Comment