21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SPC_FD_SET read_mask;<br />

for (;;) {<br />

spc_fd_zero(&read_mask);<br />

spc_fd_set(server_fd, &read_mask);<br />

for (i = 0; i < client_count; i++) spc_fd_set(client_fds[i], &read_mask);<br />

select(spc_fd_size(&read_mask), (fd_set *)&read_mask, 0, 0, 0);<br />

if (spc_fd_isset(server_fd, &read_mask)) {<br />

/* Do something with the server_fd such as call accept( ) */<br />

}<br />

for (i = 0; i < client_count; i++)<br />

if (spc_fd_isset(client_fds[i], &read_mask)) {<br />

/* Read some data from the client's socket descriptor */<br />

}<br />

spc_fd_free(&read_mask);<br />

}<br />

}<br />

As you can see, the code that uses SPC_FD_SET is not all that different from the code<br />

that uses fd_set. Naming issues aside, the only real differences are the need to cast<br />

the SPC_FD_SET object to an fd_set object, and to call spc_fd_free( ).<br />

See Also<br />

Recipe 3.3<br />

Preventing File Descriptor Overflows When Using select( ) | 115<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!