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

📄 ch17_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
> connects a local socket to a (possibly remote) one, <CODECLASS="literal">listen</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004663-0"></A> readies a socket for connections from other sockets, and <CODECLASS="literal">accept</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004665-0"></A> receives the connections one by one. You can communicate over a stream socket with <CODECLASS="literal">print</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004666-0"></A> and <ACLASS="indexterm"NAME="ch17-idx-1000004669-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004669-1"></A>&lt; &gt; as well as with <CODECLASS="literal">syswrite</CODE> and <CODECLASS="literal">sysread</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004667-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004667-1"></A>, or over a datagram socket with <CODECLASS="literal">send</CODE> and <CODECLASS="literal">recv</CODE>. (Perl does not currently support <EMCLASS="emphasis">sendmsg</EM>(2).)</P><PCLASS="para">A typical server calls <CODECLASS="literal">socket</CODE>, <CODECLASS="literal">bind</CODE>, and <CODECLASS="literal">listen</CODE>, then loops in a blocking <CODECLASS="literal">accept</CODE> call that waits for incoming connections (see <ACLASS="xref"HREF="ch17_03.htm"TITLE="Writing a TCP Server">Recipe 17.2</A> and <ACLASS="xref"HREF="ch17_06.htm"TITLE="Setting Up a UDP Server">Recipe 17.5</A>). A typical client calls <CODECLASS="literal">socket</CODE> and <CODECLASS="literal">connect</CODE> (see <ACLASS="xref"HREF="ch17_02.htm"TITLE="Writing a TCP Client">Recipe 17.1</A> and <ACLASS="xref"HREF="ch17_05.htm"TITLE="Setting Up a UDP Client">Recipe 17.4</A>). Datagram clients are special. They don't have to <CODECLASS="literal">connect</CODE> to send data because they can specify the destination as an argument to <CODECLASS="literal">send</CODE>.</P><PCLASS="para">When you <CODECLASS="literal">bind</CODE> , <CODECLASS="literal">connect</CODE>, or <CODECLASS="literal">send</CODE> to a specific destination, you must supply a socket name. An Internet domain socket name is a host (an IP address packed with <CODECLASS="literal">inet_aton</CODE>) and a port (a number), packed into a C-style structure with <CODECLASS="literal">sockaddr_in</CODE>:</P><PRECLASS="programlisting">use Socket;$packed_ip   = inet_aton(&quot;208.146.240.1&quot;);$socket_name = sockaddr_in($port, $packed_ip);</PRE><PCLASS="para">A Unix domain socket name is a filename packed into a C structure with <CODECLASS="literal">sockaddr_un</CODE>:</P><PRECLASS="programlisting">use Socket;$socket_name = sockaddr_un(&quot;/tmp/mysock&quot;);</PRE><PCLASS="para">To take a packed socket name and turn it back into a filename or host and port, call <CODECLASS="literal">sockaddr_un</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004677-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004677-1"></A> or <CODECLASS="literal">sockaddr_in</CODE> in list context:</P><PRECLASS="programlisting">($port, $packed_ip) = sockaddr_in($socket_name);    # for PF_INET sockets($filename)         = sockaddr_un($socket_name);    # for PF_UNIX sockets</PRE><PCLASS="para">Use <CODECLASS="literal">inet_ntoa</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004678-0"></A> to turn a packed <ACLASS="indexterm"NAME="ch17-idx-1000004906-0"></A>IP address back into an ASCII string. It stands for "numbers to ASCII" and <CODECLASS="literal">inet_aton</CODE> stands for "ASCII to numbers."</P><PRECLASS="programlisting">$ip_address = inet_ntoa($packed_ip);$packed_ip  = inet_aton(&quot;204.148.40.9&quot;);$packed_ip  = inet_aton(&quot;www.oreilly.com&quot;);</PRE><PCLASS="para">Most recipes use Internet domain sockets in their examples, but nearly everything that applies to the Internet domain also applies to the Unix domain. <ACLASS="xref"HREF="ch17_07.htm"TITLE="Using UNIX Domain Sockets">Recipe 17.6</A> explains the differences and pitfalls.</P><PCLASS="para">Sockets are the basis of network services. We provide three ways to write servers: one where a child process is created for each incoming connection (<ACLASS="xref"HREF="ch17_12.htm"TITLE="Forking Servers">Recipe 17.11</A>), one where the server forks in advance (<ACLASS="xref"HREF="ch17_13.htm"TITLE="Pre-Forking Servers">Recipe 17.12</A>), and one where the server process doesn't fork at all (<ACLASS="xref"HREF="ch17_14.htm"TITLE="Non-Forking Servers">Recipe 17.13</A>).</P><PCLASS="para">Some servers need to listen to many IP addresses at once, which we demonstrate in <ACLASS="xref"HREF="ch17_15.htm"TITLE="Writing a Multi-Homed Server">Recipe 17.14</A>. Well-behaved servers clean up and restart when they get a HUP signal; <ACLASS="xref"HREF="ch17_17.htm"TITLE="Restarting a Server on Demand">Recipe 17.16</A> shows how to implement that behavior in Perl. We also show how to put a name to both ends of a connection; see <ACLASS="xref"HREF="ch17_08.htm"TITLE="Identifying the Other End of a Socket">Recipe 17.7</A> and <ACLASS="xref"HREF="ch17_09.htm"TITLE="Finding Your Own Name and Address">Recipe 17.8</A>.</P><PCLASS="para"><EMCLASS="emphasis">Unix Network Programming</EM> and the three-volume <EMCLASS="emphasis">TCP/IP Illustrated</EM> by W. Richard Stevens are indispensable for the serious socket programmer. If you want to learn the basics about sockets, it's hard to beat the original and classic reference, <CITECLASS="citetitle">An Advanced 4.4BSD Interprocess Communication Tutorial.</CITE> It's written for C, but almost everything is directly applicable to Perl. It's available in <EMCLASS="emphasis">/usr/share/doc</EM> on most BSD-derived Unix systems. We also recommend you look at <CITECLASS="citetitle">The Unix Programming Frequently Asked Questions List</CITE> (Gierth and Horgan), and <CITECLASS="citetitle">Programming UNIX Sockets in C&nbsp;- Frequently Asked Questions </CITE>(Metcalf and Gierth), both of which are posted periodically to the <ACLASS="systemitem.newsgroup"HREF="news:comp.unix.answers">comp.unix.answers</A> newsgroup.</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch16_23.htm"TITLE="16.22. Program: sigrand"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 16.22. Program: sigrand"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_02.htm"TITLE="17.1. Writing a TCP Client"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.1. Writing a TCP Client"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">16.22. Program: sigrand</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">17.1. Writing a TCP Client</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + -