當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。