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

📄 ch17_10.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 17.9. Closing a Socket After Forking (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:34Z"><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_09.htm"TITLE="17.8. Finding Your Own Name and Address"><LINKREL="next"HREF="ch17_11.htm"TITLE="17.10. Writing Bidirectional Clients"></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_09.htm"TITLE="17.8. Finding Your Own Name and Address"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.8. Finding Your Own Name and Address"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_11.htm"TITLE="17.10. Writing Bidirectional Clients"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.10. Writing Bidirectional Clients"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch17-16980">17.9. Closing a Socket After Forking</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1054">Problem<ACLASS="indexterm"NAME="ch17-idx-1000004787-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004787-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004787-2"></A><ACLASS="indexterm"NAME="ch17-idx-1000004787-3"></A></A></H3><PCLASS="para">Your program has forked and you want to tell the other end that you're done sending data. You've tried <CODECLASS="literal">close</CODE> on the socket, but the remote end never gets an EOF or SIGPIPE.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1060">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">shutdown</CODE>:</P><PRECLASS="programlisting">shutdown(SOCKET, 0);                # I/we have stopped reading datashutdown(SOCKET, 1);                # I/we have stopped writing datashutdown(SOCKET, 2);                # I/we have stopped using this socket</PRE><PCLASS="para">On an IO::Socket object, you could also write:</P><PRECLASS="programlisting">$socket-&gt;shutdown(0);               # I/we have stopped reading data</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1076">Discussion</A></H3><PCLASS="para">When a process forks, the child has copies of all the parent's open filehandles, including sockets. When you <CODECLASS="literal">close</CODE> a file or socket, you close only the current process' copy. If another process (parent or child) still has the socket open, the operating system doesn't consider their file or socket closed.</P><PCLASS="para">Take the case of a socket that data is being sent to. If two processes have this socket open, one can close it but the socket isn't considered closed by the operating system because the other still has it open. Until the <EMCLASS="emphasis">other</EM> process closes the socket, the process reading from the socket won't get an end-of-file. This can lead to confusion and deadlock.</P><PCLASS="para">To avoid this, either <CODECLASS="literal">close</CODE> unused filehandles after a <CODECLASS="literal">fork</CODE>, or use <CODECLASS="literal">shutdown</CODE>. The <CODECLASS="literal">shutdown</CODE> function is a more insistent form of <CODECLASS="literal">close </CODE>&nbsp;- it tells the operating system that even though other processes have copies of this filehandle, it should be marked as closed and the other end should get an end-of-file if they read from it, or a SIGPIPE if they write to it.</P><PCLASS="para">The numeric argument to <CODECLASS="literal">shutdown</CODE> lets you specify which sides of the connection are closed. An argument of <CODECLASS="literal">0</CODE> says that we're done reading data, so the other end of the socket will get a <CODECLASS="literal">SIGPIPE</CODE> if they try writing. <CODECLASS="literal">1</CODE> says that we're done writing data, so the other end of the socket will get an end-of-file if they try reading. <CODECLASS="literal">2</CODE> says we're done reading and writing.</P><PCLASS="para">Imagine a server that wants to read its client's request until end of file, and send an answer. If the client calls <CODECLASS="literal">close</CODE>, that socket is now invalid for I/O, so no answer would ever come back. Instead, the client should use <CODECLASS="literal">shutdown</CODE> to half-close the connection.</P><PRECLASS="programlisting">print SERVER &quot;my request\n&quot;;        # send some datashutdown(SERVER, 1);                # send eof; no more writing$answer = &lt;SERVER&gt;;                 # but you can still read<ACLASS="indexterm"NAME="ch17-idx-1000004789-0"></A><ACLASS="indexterm"NAME="ch17-idx-1000004789-1"></A><ACLASS="indexterm"NAME="ch17-idx-1000004789-2"></A><ACLASS="indexterm"NAME="ch17-idx-1000004789-3"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch17-pgfId-1096">See Also</A></H3><PCLASS="para">The <ACLASS="olink"HREF="../prog/ch03_018.htm">&#13;<CODECLASS="literal">close</CODE></A> and <ACLASS="olink"HREF="../prog/ch03_148.htm">&#13;<CODECLASS="literal">shutdown</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); your system's <ICLASS="filename">shutdown </I>(2) manpage (if you have it)</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_09.htm"TITLE="17.8. Finding Your Own Name and Address"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 17.8. Finding Your Own Name and Address"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_11.htm"TITLE="17.10. Writing Bidirectional Clients"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 17.10. Writing Bidirectional Clients"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">17.8. Finding Your Own Name and Address</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.10. Writing Bidirectional Clients</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 + -