📄 ch08_159.htm
字号:
<html><head><title>Net::Ping (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_158.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_160.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.159. Net::Ping</h2><p><a name="INDEX-1515" />Provides methods to create ping objectsand test the reachability of remote hosts on the network. After theobject has been created, a variable number of hosts can be pingedmultiple times before closing the connection. The methods are thefollowing.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">p</em> = Net::Ping->new([<em class="replaceable">proto</em>[, <em class="replaceable">def_timeout</em>[, <em class="replaceable">bytes</em>]]])</pre><p>Creates a new ping object. All arguments are optional. Takes thefollowing arguments:</p><dl><dt><i><em class="replaceable"><tt>proto</tt></em></i></dt><dd>The protocol to use when doing a ping. Default is<tt class="literal">udp</tt>. The possible protocols are:</p><dl><dt><b><tt class="literal">icmp</tt></b></dt><dd>Sends an <tt class="literal">icmp</tt> <tt class="literal">echo</tt> message tothe remote host. If the echoed message is received back correctlyfrom the remote host, that host is considered reachable. Requires theprogram to be run as root or setuid to root.</p></dd><dt><b><tt class="literal">tcp</tt></b></dt><dd>Attempts to establish connection to remote host's<tt class="literal">echo</tt> port. If successful, remote host isconsidered reachable. No data is actually echoed. No specialprivileges are required, but overhead is higher than for the others.</p></dd><dt><b><tt class="literal">udp</tt></b></dt><dd>Sends a udp packet to remote host's<tt class="literal">echo</tt> port. If the echoed packet is received backfrom the remote host and contains the same data that was sent, theremote host is considered reachable. Requires no special privileges.</p></dd></dl></dd><dt><i><em class="replaceable"><tt>def_timeout</tt></em></i></dt><dd>Default timeout in seconds to be used if timeout not passed to the<tt class="literal">ping</tt> method. Must be greater than zero; defaultsto five seconds.</p></dd><dt><i><em class="replaceable"><tt>bytes</tt></em></i></dt><dd>Number of bytes included in the ping packet sent to the remote host.Ignored if protocol is tcp. Default is <tt class="literal">1</tt> ifprotocol is udp, otherwise <tt class="literal">0</tt>. These are also theminimum number of bytes; the maximum is 1,024.</p></dd></dl></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>close</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">p</em>->close( )</pre><p>Closes network connection for this ping object. The connection canalso be closed by <tt class="literal">undef $p</tt> and is automaticallyclosed if the ping object goes out of scope.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ping</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">p</em>->ping(<em class="replaceable">host</em>[, <em class="replaceable">timeout</em>])</pre><p>Pings remote host and waits for a response. Takes the followingarguments:</p><dl><dt><i><em class="replaceable"><tt>host</tt></em></i></dt><dd>Specified as either the hostname or the IP number of the remote host.</p></dd><dt><i><em class="replaceable"><tt>timeout</tt></em></i></dt><dd>Optional, must be greater than <tt class="literal">0</tt> seconds ifspecified. Defaults to whatever was specified when the ping objectwas created.</p></dd></dl><p>If <em class="replaceable"><tt>host</tt></em> cannot be found, or if there is aproblem with the IP number, returns <tt class="literal">undef</tt>.Otherwise, returns <tt class="literal">1</tt> if the host is reachable and<tt class="literal">0</tt> if it is not.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>pingecho</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>pingecho (<em class="replaceable">host</em>[, <em class="replaceable">timeout</em>])</pre><p>Provides backward compatibility with the previous version ofNet::Ping. Uses the tcp protocol, with return values and parametersthe same as described for <tt class="literal">ping</tt>.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_158.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_160.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.158. Net::NNTP</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.160. Net::POP3</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -