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
廣告
© . All rights reserved.