当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Perl getsockopt用法及代码示例



描述

此函数获取在套接字实现级别 LEVEL 上 SOCKET 上为选项 OPTNAME 设置的套接字选项。下表给出了套接字级别 OPTNAME 的一些示例值 -

OPTNAME 	Result
SO_DEBUG 	Get status of recording of debugging information
SO_REUSEADDR 	Get status of local address reuse
SO_KEEPALIVE 	Get status of keep connections alive
SO_DONTROUTE 	Get status of routing bypass for outgoing messages
SO_LINGER 	Get status of linger on close if data is present
SO_BROADCAST 	Get status of permission to transmit broadcast messages
SO_OOBINLINE 	Get status of out-of-band data in band
SO_SNDBUF 	Get buffer size for output
SO_RCVBUF 	Get buffer size for input
SO_TYPE 	Get the type of the socket
SO_ERROR 	Get and clear error on the socket
TCP_NODELAY     To disable the Nagle buffering algorithm.

压缩字符串中的确切内容取决于 LEVEL 和 OPTNAME,有关详细信息,请参阅您的系统文档。

用法

以下是此函数的简单语法 -

getsockopt SOCKET, LEVEL, OPTNAME

返回值

此函数在错误时返回 undef,否则在标量上下文中返回选项值。

示例

以下是显示其基本用法的示例代码,这将检查是否在套接字上打开了 Nagle 算法。但是,在此示例中,您必须打开一个套接字以提供 socked ID -

#!/usr/bin/perl

use Socket qw(:all);

defined(my $tcp = getprotobyname("tcp"))
   or die "Could not determine the protocol number for tcp";
# my $tcp = IPPROTO_TCP; # Alternative

my $packed = getsockopt($socket, $tcp, TCP_NODELAY)
   or die "Could not query TCP_NODELAY socket option:$!";
my $nodelay = unpack("I", $packed);

print "Nagle's algorithm is turned ", $nodelay ? "off\n":"on\n";

相关用法


注:本文由纯净天空筛选整理自 Perl getsockopt Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。