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

📄 ch17_15.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 17.14. Writing a Multi-Homed Server (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:44:43Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch17_01.htm"TITLE="17. Sockets"><LINKREL="prev"HREF="ch17_14.htm"TITLE="17.13. Non-Forking Servers"><LINKREL="next"HREF="ch17_16.htm"TITLE="17.15. Making a Daemon Server"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_14.htm"TITLE="17.13. Non-Forking Servers"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.13. Non-Forking Servers"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch17_01.htm"TITLE="17. Sockets"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch17_16.htm"TITLE="17.15. Making a Daemon Server"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.15. Making a Daemon Server"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch17-17381">17.14. Writing a Multi-Homed Server</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1838">Problem<ACLASS="indexterm"NAME="ch17-idx-1000004864-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004864-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004864-2"></A><ACLASS="indexterm"NAME="ch17-idx-1000004864-3"></A><ACLASS="indexterm"NAME="ch17-idx-1000004864-4"></A></A></H3><PCLASS="para">You want to write a server that knows that the machine it runs on has multiple IP addresses, and that it should possibly do different things for each address.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1844">Solution</A></H3><PCLASS="para">Don't bind your server to a particular address. Instead, bind to <CODECLASS="literal">INADDR_ANY</CODE>. Then, once you've <CODECLASS="literal">accept</CODE>ed a connection, use <CODECLASS="literal">getsockname</CODE> on the client socket to find out which address they connected to:</P><PRECLASS="programlisting">use Socket;socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1);bind(SERVER, sockaddr_in($server_port, INADDR_ANY))    or die &quot;Binding: $!\n&quot;;# accept loopwhile (accept(CLIENT, SERVER)) {    $my_socket_address = getsockname(CLIENT);    ($port, $myaddr)   = sockaddr_in($my_socket_address);}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1874">Discussion</A></H3><PCLASS="para">Whereas <CODECLASS="literal">getpeername</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004870-0"></A> (as discussed in <ACLASS="xref"HREF="ch17_08.htm"TITLE="Identifying the Other End of a Socket">Recipe 17.7</A>) returns the address of the remote end of the socket, <CODECLASS="literal">getsockname</CODE> returns the address of the local end. When we've bound to <CODECLASS="literal">INADDR_ANY</CODE>, thus accepting connections on any address the machine has, we need to use <CODECLASS="literal">getsockname</CODE> to identify which address the client connected to.</P><PCLASS="para">If you're using IO::Socket::INET, your code will look like this:</P><PRECLASS="programlisting">$server = IO::Socket::INET-&gt;new(LocalPort =&gt; $server_port,                                Type      =&gt; SOCK_STREAM,                                Proto     =&gt; 'tcp',                                Listen    =&gt; 10)    or die &quot;Can't create server socket: $@\n&quot;;while ($client = $server-&gt;accept()) {    $my_socket_address = $client-&gt;sockname();    ($port, $myaddr)   = sockaddr_in($my_socket_address);    # ...}</PRE><PCLASS="para">If you don't specify a local port to <CODECLASS="literal">IO::Socket::INET-&gt;new</CODE>, your socket will be bound to <CODECLASS="literal">INADDR_ANY</CODE>.</P><PCLASS="para">If you want your server to listen only for a <EMCLASS="emphasis">particular</EM> virtual host, don't use <CODECLASS="literal">INADDR_ANY</CODE>. Instead, bind to a specific host address:</P><PRECLASS="programlisting">use Socket;$port = 4269;                       # port to bind to$host = &quot;specific.host.com&quot;;        # virtual host to listen onsocket(Server, PF_INET, SOCK_STREAM, getprotobyname(&quot;tcp&quot;))    or die &quot;socket: $!&quot;;bind(Server, sockaddr_in($port, inet_aton($host)))    or die &quot;bind: $!&quot;;while ($client_address = accept(Client, Server)) {    # ...}<ACLASS="indexterm"NAME="ch17-idx-1000004866-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004866-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004866-2"></A><ACLASS="indexterm"NAME="ch17-idx-1000004866-3"></A><ACLASS="indexterm"NAME="ch17-idx-1000004866-4"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1932">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch03_067.htm">&#13;<CODECLASS="literal">getsockname</CODE></A> function in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlfunc </I>(1); the documentation for the standard Socket and IO::Socket modules; the section on <ACLASS="olink"HREF="../prog/ch06_02.htm#PERL2-CH-6-SECT-2.4"> "Sockets"</A> in <ACLASS="olink"HREF="../prog/ch06_01.htm">Chapter 6</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> or <ICLASS="filename">perlipc </I>(1)</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="ch17_14.htm"TITLE="17.13. Non-Forking Servers"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.13. Non-Forking Servers"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_16.htm"TITLE="17.15. Making a Daemon Server"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.15. Making a Daemon Server"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">17.13. Non-Forking Servers</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.15. Making a Daemon Server</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 + -