setsockopt

setsockoptSOCKET,LEVEL,OPTNAME,OPTVAL
This function sets the socket option requested. The function
returns undef on error. The Socket module provides the needed constants for
LEVEL and OPNAME,
although those for LEVEL can all be obtained
from getprotobyname.
LEVEL specifies which protocol layer you’re
aiming the call at, or SOL_SOCKET for
the socket itself at the top of all the layers.
OPTVAL might either be a packed string or an
integer. An integer OPTVAL is shorthand for
pack("i", OPTVAL).
OPTVAL may be specified as undef if you don’t want to pass an
argument.
One common option to set on a socket is SO_REUSEADDR, which gets around the problem of
not being able to bind to a particular address while the previous TCP
connection on that port is still making up its mind to shut down. That
would look like this:
use Socket; socket(SOCK, ...) || die "Can't make socket: $!"; setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, 1) || warn "Can't do setsockopt: $!\n";
Another common option is to disable Nagle’s algorithm on a socket:
use Socket qw(IPPROTO_TCP TCP_NODELAY); setsockopt($socket, IPPROTO_TCP, TCP_NODELAY, 1);
See setsockopt(2) for other possible values.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access