13.07.2015 Views

Beej's Guide to Network Programming Using Internet Sockets

Beej's Guide to Network Programming Using Internet Sockets

Beej's Guide to Network Programming Using Internet Sockets

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Beej’s <strong>Guide</strong> <strong>to</strong> <strong>Network</strong> <strong>Programming</strong> <strong>Using</strong> <strong>Internet</strong> <strong>Sockets</strong> 518.9. fcntl()Control socket descrip<strong>to</strong>rsPro<strong>to</strong>types#include #include int fcntl(int s, int cmd, long arg);DescriptionThis function is typically used <strong>to</strong> do file locking and other file-oriented stuff, but it also has acouple socket-related functions that you might see or use from time <strong>to</strong> time.Parameter s is the socket descrip<strong>to</strong>r you wish <strong>to</strong> operate on, cmd should be set <strong>to</strong> F_SETFL, andarg can be one of the following commands. (Like I said, there’s more <strong>to</strong> fcntl() than I’m lettingon here, but I’m trying <strong>to</strong> stay socket-oriented.)O_NONBLOCKSet the socket <strong>to</strong> be non-blocking. See the section on blocking formore details.O_ASYNCSet the socket <strong>to</strong> do asynchronous I/O. When data is ready <strong>to</strong> berecv()’d on the socket, the signal SIGIO will be raised. This is rare <strong>to</strong>see, and beyond the scope of the guide. And I think it’s only availableon certain systems.Return ValueReturns zero on success, or -1 on error (and errno will be set accordingly.)Different uses of the fcntl() actually have different return values, but I haven’t covered themhere because they’re not socket-related. See your local fcntl() man page for more information.Exampleint s = socket(PF_INET, SOCK_STREAM, 0);fcntl(s, F_SETFL, O_NONBLOCK); // set <strong>to</strong> non-blockingfcntl(s, F_SETFL, O_ASYNC); // set <strong>to</strong> asynchronous I/OSee AlsoBlocking, send()

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

Saved successfully!

Ooh no, something went wrong!