📄 ch17_07.htm
字号:
<HTML><HEAD><TITLE>Recipe 17.6. Using UNIX Domain Sockets (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:32Z"><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_06.htm"TITLE="17.5. Setting Up a UDP Server"><LINKREL="next"HREF="ch17_08.htm"TITLE="17.7. Identifying the Other End of a Socket"></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_06.htm"TITLE="17.5. Setting Up a UDP Server"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.5. Setting Up a UDP Server"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_08.htm"TITLE="17.7. Identifying the Other End of a Socket"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.7. Identifying the Other End of a Socket"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch17-12131">17.6. Using UNIX Domain Sockets</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-834">Problem<ACLASS="indexterm"NAME="ch17-idx-1000004757-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004757-1"></A></A></H3><PCLASS="para">You want to communicate with other processes on only the local machine.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-840">Solution</A></H3><PCLASS="para">Use domain sockets. You can use the code and techniques from the preceding Internet domain recipes, with the following changes:</P><ULCLASS="itemizedlist"><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch17-pgfId-844"></A>Because the naming system is different, use <CODECLASS="literal">sockaddr_un</CODE> instead of <CODECLASS="literal">sockaddr_in</CODE>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch17-pgfId-846"></A>Use IO::Socket::UNIX instead of IO::Socket::INET.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch17-pgfId-848"></A>Use PF_UNIX instead of PF_INET, and give PF_UNSPEC as the last argument to <CODECLASS="literal">socket</CODE>.</P></LI><LICLASS="listitem"><PCLASS="para"><ACLASS="listitem"NAME="ch17-pgfId-850"></A>SOCK_STREAM clients don't have to <CODECLASS="literal">bind</CODE> to a local address before they <CODECLASS="literal">connect</CODE>.</P></LI></UL></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-854">Discussion</A></H3><PCLASS="para">Unix domain sockets have names like files on the filesystem. In fact, most systems implement them as special files; that's what Perl's <CODECLASS="literal">-S</CODE> filetest operator looks for - whether the file is a Unix domain socket.</P><PCLASS="para">Supply the filename as the Peer argument to <CODECLASS="literal">IO::Socket::UNIX->new</CODE>, or encode it with <CODECLASS="literal">sockaddr_un</CODE> and pass it to <CODECLASS="literal">connect</CODE>. Here's how to make server and client Unix domain datagram sockets with IO::Socket::UNIX:</P><PRECLASS="programlisting">use IO::Socket;unlink "/tmp/mysock";$server = IO::Socket::UNIX->new(LocalAddr => "/tmp/mysock", Type => SOCK_DGRAM, Listen => 5 ) or die $@;$client = IO::Socket::UNIX->new(PeerAddr => "/tmp/mysock", Type => SOCK_DGRAM, Timeout => 10 ) or die $@;</PRE><PCLASS="para">Here's how to use the traditional functions to make stream sockets:</P><PRECLASS="programlisting">use Socket; socket(SERVER, PF_UNIX, SOCK_STREAM, 0);unlink "/tmp/mysock";bind(SERVER, sockaddr_un("/tmp/mysock")) or die "Can't create server: $!";socket(CLIENT, PF_UNIX, SOCK_STREAM, 0);connect(CLIENT, sockaddr_un("/tmp/mysock")) or die "Can't connect to /tmp/mysock: $!";</PRE><PCLASS="para">Unless you know what you're doing, set the protocol (the Proto argument to <CODECLASS="literal">IO::Socket::UNIX->new</CODE>, and the last argument to <CODECLASS="literal">socket</CODE>) to 0 for PF_UNIX sockets. You can use both SOCK_DGRAM and SOCK_STREAM types of communication in the Unix domain, with the same semantics as we saw for Internet sockets. Changing the domain doesn't change the characteristics of the type of socket.</P><PCLASS="para">Because many systems actually create a special file in the filesystem, you should delete the file before you try to bind the socket. Even though there is a race condition (somebody could create a file with the name of your socket between your calls to <CODECLASS="literal">unlink</CODE> and <CODECLASS="literal">bind</CODE>), this isn't a security problem, because <CODECLASS="literal">bind</CODE> won't overwrite an existing file.<ACLASS="indexterm"NAME="ch17-idx-1000004768-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004768-1"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-912">See Also</A></H3><PCLASS="para">Recipes <ACLASS="xref"HREF="ch17_02.htm"TITLE="Writing a TCP Client">Recipe 17.1</A> through <ACLASS="xref"HREF="ch17_06.htm"TITLE="Setting Up a UDP Server">Recipe 17.5</A></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_06.htm"TITLE="17.5. Setting Up a UDP Server"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.5. Setting Up a UDP Server"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_08.htm"TITLE="17.7. Identifying the Other End of a Socket"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.7. Identifying the Other End of a Socket"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">17.5. Setting Up a UDP Server</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.7. Identifying the Other End of a Socket</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 + -