Perl getsockopt 函数



说明

该函数获取套接字实现级别 LEVEL 的套接字上设置的套接字选项,具体使用选项 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

返回值

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

举例

以下示例代码显示了它基本用法,这将检查某个套接字上的 Nagle 算法是否已启用。但是,在这里你必须打开一个套接字才能在此示例中提供套接字 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_function_references.htm
广告