📄 ch17_09.htm
字号:
<HTML><HEAD><TITLE>Recipe 17.8. Finding Your Own Name and Address (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:44:33Z"><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_08.htm"TITLE="17.7. Identifying the Other End of a Socket"><LINKREL="next"HREF="ch17_10.htm"TITLE="17.9. Closing a Socket After Forking"></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_08.htm"TITLE="17.7. Identifying the Other End of a Socket"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.7. Identifying the Other End of a Socket"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_10.htm"TITLE="17.9. Closing a Socket After Forking"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.9. Closing a Socket After Forking"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch17-24022">17.8. Finding Your Own Name and Address</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-996">Problem<ACLASS="indexterm"NAME="ch17-idx-1000004779-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004779-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004779-2"></A></A></H3><PCLASS="para">You want to find your (fully qualified) hostname.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1002">Solution</A></H3><PCLASS="para">First, get your (possibly qualified) hostname. Either try the standard <ACLASS="indexterm"NAME="ch17-idx-1000004780-0"></A>Sys::Hostname module:</P><PRECLASS="programlisting">use Sys::Hostname;$hostname = hostname();</PRE><PCLASS="para">or POSIX's <CODECLASS="literal">uname</CODE><ACLASS="indexterm"NAME="ch17-idx-1000004781-0"></A> function:</P><PRECLASS="programlisting">use POSIX qw(uname);($kernel, $hostname, $release, $version, $hardware) = uname();$hostname = (uname)[1]; # or just one</PRE><PCLASS="para">Then turn it into an IP address and convert to its canonical name:</P><PRECLASS="programlisting">use Socket; # for AF_INET$address = gethostbyname($hostname) or die "Couldn't resolve $hostname : $!";$hostname = gethostbyaddr($address, AF_INET) or die "Couldn't re-resolve $hostname : $!";</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1034">Discussion</A></H3><PCLASS="para">Sys::Hostname tries to be portable by using knowledge about your system to decide how best to find the hostname. It tries many different ways of getting the hostname, but several involve running other programs. This can lead to tainted data (see <ACLASS="xref"HREF="ch19_02.htm"TITLE="Writing a CGI Script">Recipe 19.1</A>).</P><PCLASS="para"><CODECLASS="literal">POSIX::uname</CODE>, on the other hand, only works on POSIX systems and isn't guaranteed to provide anything useful in the <CODECLASS="literal">nodename</CODE> field that we are examining. That said, the value <EMCLASS="emphasis">is</EM> useful on many machines and doesn't suffer from the tainted data problem that Sys::Hostname does.</P><PCLASS="para">Once you have the name, though, you must consider that it might be missing a domain name. For instance, Sys::Hostname may return you <CODECLASS="literal">guanaco</CODE> instead of <CODECLASS="literal">guanaco.camelids.org</CODE>. To fix this, convert the name back into an IP address with <CODECLASS="literal">gethostbyname</CODE> and then back into a name again with <CODECLASS="literal">gethostbyaddr</CODE>. By involving the domain name system, you are guaranteed of getting a full name.<ACLASS="indexterm"NAME="ch17-idx-1000004783-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004783-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004783-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1044">See Also</A></H3><PCLASS="para">The<ACLASS="olink"HREF="../prog/ch03_047.htm"> <CODECLASS="literal">gethostbyaddr</CODE></A>,<ACLASS="olink"HREF="../prog/ch03_048.htm"> <CODECLASS="literal">gethostbyname</CODE></A>functions 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 Net::hostent and Sys::Hostname modules</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_08.htm"TITLE="17.7. Identifying the Other End of a Socket"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.7. Identifying the Other End of a Socket"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_10.htm"TITLE="17.9. Closing a Socket After Forking"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.9. Closing a Socket After Forking"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">17.7. Identifying the Other End of a Socket</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.9. Closing a Socket After Forking</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 © 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 + -