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> 648.17. setsockopt(), getsockopt()Set various options for a socketPro<strong>to</strong>types#include #include int getsockopt(int s, int level, int optname, void *optval,socklen_t *optlen);int setsockopt(int s, int level, int optname, const void *optval,socklen_t optlen);Description<strong>Sockets</strong> are fairly configurable beasts. In fact, they are so configurable, I’m not even going <strong>to</strong>cover it all here. It’s probably system-dependent anyway. But I will talk about the basics.Obviously, these functions get and set certain options on a socket. On a Linux box, all thesocket information is in the man page for socket in section 7. (Type: “man 7 socket” <strong>to</strong> get allthese goodies.)As for parameters, s is the socket you’re talking about, level should be set <strong>to</strong> SOL_SOCKET.Then you set the optname <strong>to</strong> the name you’re interested in. Again, see your man page for all theoptions, but here are some of the most fun ones:SO_BINDTODEVICEBind this socket <strong>to</strong> a symbolic device name like eth0 insteadof using bind() <strong>to</strong> bind it <strong>to</strong> an IP address. Type the commandifconfig under Unix <strong>to</strong> see the device names.SO_REUSEADDRAllows other sockets <strong>to</strong> bind() <strong>to</strong> this port, unless there is anactive listening socket bound <strong>to</strong> the port already. This enables you<strong>to</strong> get around those “Address already in use” error messages whenyou try <strong>to</strong> restart your server after a crash.SO_BROADCASTAllows UDP datagram (SOCK_DGRAM) sockets <strong>to</strong> send and recievepackets sent <strong>to</strong> and from the broadcast address. Does nothing–NOTHING!!–<strong>to</strong> TCP stream sockets! Hahaha!As for the parameter optval, it’s usually a pointer <strong>to</strong> an int indicating the value in question.For booleans, zero is false, and non-zero is true. And that’s an absolute fact, unless it’s differen<strong>to</strong>n your system. If there is no parameter <strong>to</strong> be passed, optval can be NULL.The final parameter, optlen, is filled out for you by getsockopt() and you have <strong>to</strong> specifyit for getsockopt(), where it will probably be sizeof(int).Warning: on some systems (notably Sun and Windows), the option can be a char instead ofan int, and is set <strong>to</strong>, for example, a character value of ’1’ instead of an int value of 1. Again,check your own man pages for more info with “man setsockopt” and “man 7 socket”!Return ValueReturns zero on success, or -1 on error (and errno will be set accordingly.)Example

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

Saved successfully!

Ooh no, something went wrong!