⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<p>The <tt class="literal">sockaddr_in</tt> function takes a port number forits first argument and a 32-bit IP address for the second argument.The 32-bit address is formed from the <tt class="literal">inet_aton</tt>function found in the Socket package. This function takes either ahostname (e.g., www.oreilly.com)or a dotted-decimal string (e.g., 207.54.2.25) and returns thecorresponding 32-bit structure.</p><p>Continuing with the previous example, a call to<tt class="literal">connect</tt> could look like this:</p><blockquote><pre class="code">my $dest = sockaddr_in (80, inet_aton('www.oreilly.com'));connect (SH, $dest) || die $!;</pre></blockquote><p>This call attempts to establish a network connection to the specifiedserver and port. If successful, it returns true. Otherwise, itreturns false and <tt class="literal">die</tt> s with the error in<tt class="literal">$!</tt>.</p><p>Assuming that the <tt class="literal">connect</tt> call has completedsuccessfully and a connection has been established, there are anumber of functions we can use to write to and read from thefilehandle. For example, the <tt class="literal">send</tt> function sendsdata to a socket:</p><blockquote><pre class="code">$data = "Hello";send (FH, $data);</pre></blockquote><p><a name="INDEX-1923" /></a>The <tt class="literal">print</tt> functionallows a wider variety of expressions for sending data to afilehandle:</p><blockquote><pre class="code">select (FH);print "$data";</pre></blockquote><p>To read incoming data from a socket, use either the<tt class="literal">recv</tt> function or the diamond input operatorregularly used on filehandles. For example:</p><blockquote><pre class="code">recv (FH, $buffer);$input = &lt;FH&gt;;</pre></blockquote><p>After the conversation with the server is finished, use<tt class="literal">close</tt> or <tt class="literal">shutdown</tt> to close theconnection and destroy the socket.</p></div><a name="perlnut2-CHP-15-SECT-1.3" /></a><div class="sect2"><h3 class="sect2">15.1.3. Server Connections</h3><p><a name="INDEX-1924" /></a><a name="INDEX-1925" /></a>After creating a socket with the<tt class="literal">socket</tt> function as you did previously, a serverapplication must go through the following steps to receive networkconnections:</p><ol><li><p>Bind a port number and machine address to thesocket<a name="INDEX-1926" /></a></p></li><li><p>Listen for incoming connections from clients on the port</p></li><li><p>Accept a client request and assign the connection to a specificfilehandle</p></li></ol><p>We start out by creating a socket for the server: </p><blockquote><pre class="code">my $proto = getprotobyname('tcp');socket(FH, PF_INET, SOCK_STREAM, $proto) || die $!;</pre></blockquote><p>The filehandle <tt class="literal">$FH</tt> is the generic filehandle forthe socket. This filehandle only receives requests from clients; eachspecific connection is passed to a different filehandle by<tt class="literal">accept</tt>, where the rest of the communicationoccurs.</p><p>A server-side socket must be bound to a port on the local machine bypassing a port and an address data structure to the<tt class="literal">bind</tt> <a name="INDEX-1927" /></a>function via<tt class="literal">sockaddr_in</tt>. The Socket module providesidentifiers for common local addresses, such as localhost and thebroadcast address. Here we use <tt class="literal">INADDR_ANY</tt>, whichallows the system to pick the appropriate address for the machine:</p><blockquote><pre class="code">my $sin = sockaddr_in (80, INADDR_ANY);bind (FH, $sin) || die $!;</pre></blockquote><p><a name="INDEX-1928" /></a>The <tt class="literal">listen</tt> functiontells the operating system that the server is ready to acceptincoming network connections on the port. The first argument is thesocket filehandle. The second argument gives a queue length, in casemultiple clients are connecting to the port at the same time. Thisnumber indicates how many clients can wait for an<tt class="literal">accept</tt> at one time.</p><blockquote><pre class="code">listen (FH, $length);</pre></blockquote><p><a name="INDEX-1929" /></a>The <tt class="literal">accept</tt>function completes a connection after a client requests and assigns anew filehandle specific to that connection. The new filehandle isgiven as the first argument to <tt class="literal">accept</tt>, and thegeneric socket filehandle is given as the second:</p><blockquote><pre class="code">accept (NEW, FH) || die $!;</pre></blockquote><p>Now the server can read and write to the filehandle<tt class="literal">NEW</tt> for its communication with the client.</p></div><a name="perlnut2-CHP-15-SECT-1.4" /></a><div class="sect2"><h3 class="sect2">15.1.4. Socket Module Functions</h3><p><a name="INDEX-1930" /></a>The following functions are importedfrom the Socket module for use in socket applications.</p><a name="INDEX-1931" /></a><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>inet_aton</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" true align="left" color="black" /><pre>inet_aton (<em class="replaceable">hostname</em>)</pre><p><a name="INDEX-1931" /></a>Translates a hostname such as<em class="emphasis">www.oreilly.com</em> or 18.181.0.24into a data structure (a four-byte string) used for socket addresses.If the hostname cannot be resolved, the function returns theundefined value.</p></div><a name="INDEX-1932" /></a><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>inet_ntoa</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" true align="left" color="black" /><pre>inet_ntoa (<em class="replaceable">addr_string</em>)</pre><p><a name="INDEX-1932" /></a>Translates a four-byte address string(as returned by <tt class="literal">inet_aton</tt>) into a string with thedotted-quad form of IP address.</p></div><a name="INDEX-1933" /></a><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>sockaddr_in</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" true align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>sockaddr_in (<em class="replaceable">port</em>, <em class="replaceable">addr_string</em>)pack_sockaddr_in (<em class="replaceable">port</em>, <em class="replaceable">addr_string</em>)</pre></td><td align="right" /></tr></table><p><p><a name="INDEX-1933" /></a>Takesa port number and a four-byte <em class="replaceable"><tt>addr_string</tt></em>(as returned by <tt class="literal">inet_aton</tt>) and returns the socketaddress structure including those arguments packed with the AF_INETargument. This structure is normally what you need for the argumentsin <tt class="literal">bind</tt>, <tt class="literal">connect</tt>, and<tt class="literal">send</tt>, and is also returned by<tt class="literal">getpeername</tt>, <tt class="literal">getsockname</tt>, and<tt class="literal">recv</tt>.</p></div><a name="INDEX-1934" /></a><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>sockaddr_un</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" true align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>sockaddr_un (<em class="replaceable">pathname</em>)pack_sockaddr_un (<em class="replaceable">pathname</em>)</pre></td><td align="right" /></tr></table><p><p><a name="INDEX-1934" /></a>Takesone argument, a pathname, and returns the Unix domain socket addressstructure (the path packed in with AF_UNIX filled in). For Unixdomain sockets, this structure is normally what you need for thearguments in <tt class="literal">bind</tt>, <tt class="literal">connect</tt>, and<tt class="literal">send</tt>, and is also returned by<tt class="literal">getpeername</tt>, <tt class="literal">getsockname</tt>, and<tt class="literal">recv</tt>.</p></div><a name="INDEX-1935" /></a><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>unpack_sockaddr_in</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" true align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>unpack_sockaddr_in (<em class="replaceable">sockaddr</em>)sockaddr_in (<em class="replaceable">sockaddr</em>) </pre></td><td align="right" /></tr></table><p><p><a name="INDEX-1935" /></a>Takes a socket address structure andreturns an array of two elements (in list context): the port numberand the four-byte IP address.</p></div><a name="INDEX-1936" /></a><a name="INDEX-1937" /></a><a name="INDEX-1938" /></a><a name="INDEX-1939" /></a><a name="INDEX-1940" /></a><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>unpack_sockaddr_un</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" true align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>unpack_sockaddr_un (<em class="replaceable">sockaddr_un</em>)sockaddr_un (<em class="replaceable">sockaddr_un</em>) </pre></td><td align="right" /></tr></table><p><p><a name="INDEX-1936" /></a>Takes a Unix domain socket addressstructure (as returned by <tt class="literal">sockaddr_un</tt>) and returnsthe pathname.</p><p>The following constants are defined in the Socket module:</p><dl><dt><b><tt class="literal">INADDR_ANY</tt></b></dt><dd><a name="INDEX-1937" /></a>Thefour-byte packed string for the wildcard IP address that specifiesany of the host's addresses (if the host hasmultiple addresses). This is equivalent to<tt class="literal">inet_aton('0.0.0.0')</tt>. <p></p></dd><dt><b><tt class="literal">INADDR_BROADCAST</tt></b></dt><dd><a name="INDEX-1938" /></a>The four-byte packed string for thebroadcast address. This is equivalent to<tt class="literal">inet_aton('255.255.255.255')</tt>.<p></p></dd><dt><b><tt class="literal">INADDR_LOOPBACK</tt></b></dt><dd><a name="INDEX-1939" /></a>The four-byte packed string for theloopback address. This is equivalent to<tt class="literal">inet_aton('localhost')</tt>.<p></p></dd><dt><b><tt class="literal">INADDR_NONE</tt></b></dt><dd><a name="INDEX-1940" /></a>Thefour-byte packed string for the"invalid" IP address (bitmask).Equivalent to <tt class="literal">inet_aton('255.255.255.255')</tt>.<p></p></dd></dl></div></div></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="part7.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="ch15_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">VII. Network Programming</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">15.2. The IO::Socket Module</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 &copy; 2002</a> O'Reilly &amp; 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 + -