Perl 5 version 10.1 documentation



    Queries the option named OPTNAME associated with SOCKET at a given LEVEL. Options may exist at multiple protocol levels depending on the socket type, but at least the uppermost socket level SOL_SOCKET (defined in the Socket module) will exist. To query options at another level the protocol number of the appropriate protocol controlling the option should be supplied. For example, to indicate that an option is to be interpreted by the TCP protocol, LEVEL should be set to the protocol number of TCP, which you can get using getprotobyname.

    The call returns a packed string representing the requested socket option, or undef if there is an error (the error reason will be in $!). What exactly is in the packed string depends in the LEVEL and OPTNAME, consult your system documentation for details. A very common case however is that the option is an integer, in which case the result will be a packed integer which you can decode using unpack with the i (or I ) format.

    An example testing if Nagle's algorithm is turned on on a socket:

    1. use Socket qw(:all);
    2. defined(my $tcp = getprotobyname("tcp"))
    3. or die "Could not determine the protocol number for tcp";
    4. # my $tcp = IPPROTO_TCP; # Alternative
    5. my $packed = getsockopt($socket, $tcp, TCP_NODELAY)
    6. or die "Could not query TCP_NODELAY socket option: $!";
    7. my $nodelay = unpack("I", $packed);
    8. print "Nagle's algorithm is turned ", $nodelay ? "off\n" : "on\n";

Recently read