old-www/LDP/LG/issue76/tag/9.html

338 lines
12 KiB
HTML

<!--startcut ======================================================= -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<META NAME="generator" CONTENT="lgazmail v1.4F.n">
<TITLE>The Answer Gang 76: How Do You Detect if a Server Closed a TCP Connection</TITLE>
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"
LINK="#3366FF" VLINK="#A000A0">
<!--endcut ========================================================= -->
<P> <hr>
<!--startcut ======================================================= -->
<CENTER>
<!-- *** BEGIN navbar *** -->
<!-- *** END navbar *** -->
</CENTER>
</p>
<!--endcut ========================================================= -->
<!--startcut ======================================================= -->
<P> <hr>
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
<p align="center">
<table width="100%" border="0"><tr>
<td align="right" valign="center"
><IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0"
><A HREF="..//"
><IMG SRC="../../gx/navbar/toc.jpg" align="middle"
ALT="[ Table Of Contents ]" border="0"></A
><A HREF="../lg_answer.html"
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
ALT="[ Answer Guy Current Index ]" border="0"></A></td>
<td align="center" valign="center"><A HREF="../lg_answer.html#greeting"><img align="middle"
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A> &nbsp;
<A HREF="../tag/bios.html">Meet&nbsp;the&nbsp;Gang</A> &nbsp;
<A HREF="1.html">1</A> &nbsp;
<A HREF="2.html">2</A> &nbsp;
<A HREF="3.html">3</A> &nbsp;
<A HREF="4.html">4</A> &nbsp;
<A HREF="5.html">5</A> &nbsp;
<A HREF="6.html">6</A> &nbsp;
<A HREF="7.html">7</A> &nbsp;
<A HREF="8.html">8</A> &nbsp;
<A HREF="9.html">9</A> &nbsp;
<A HREF="10.html">10</A> &nbsp;
<A HREF="11.html">11</A> &nbsp;
<A HREF="12.html">12</A>
</td>
<td align="left" valign="center"><A HREF="../../tag/kb.html"
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
ALT="[ Index of Past Answers ]" border="0"></A
><IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
WIDTH="14" HEIGHT="45" BORDER="0"></td></tr></table>
</p>
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
<!--endcut ========================================================= -->
<P> <hr> <P>
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<center>
<H1><A NAME="answer">
<img src="../../gx/dennis/qbubble.gif" alt="(?)"
border="0" align="middle">
<font color="#B03060">The Answer Gang</font>
<img src="../../gx/dennis/bbubble.gif" alt="(!)"
border="0" align="middle">
</A></H1>
<BR>
<H4>By Jim Dennis, Ben Okopnik, Dan Wilder, Breen, Chris, and...
(<a href="bios.html">meet the Gang</a>) ...
the Editors of Linux Gazette...
and You!
<br>Send questions (or interesting answers) to
The Answer Gang
for possible publication
(but read the <a href="ask-the-gang.html">guidelines</a> first)
</H4>
</center>
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<p><hr><p>
<!-- begin 9 -->
<H3 align="left"><img src="../../gx/dennis/qbubble.gif"
height="50" width="60" alt="(?) " border="0"
>How Do You Detect if a Server Closed a TCP Connection</H3>
<p><strong>From Sree
</strong></p>
<p align="right"><strong>Answered By Chris Gianakopoulos, Thomas Adam, Ben Okopnik
</strong></p>
<P><STRONG>
hi group,
</STRONG></P>
<P><STRONG>
I have a client here that is communicating
with a server socket.
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Thomas]
Hi,
</blockQuote>
<blockQuote>
[** I'll give this my best shot, but my knowledge on
this is limited **]
</blockQuote>
<blockQuote>
(Hey...perhaps I ought to invest in some Dark
Sunglasses like our "shady" KGB counterpart)
</blockQuote>
<blockquote><font color="#000066"><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Heather] The Editor Gal hands the Weekend Mechanic his toolbox and welding goggles...
</font></blockquote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
The client is transferring data to the
server.
If the server closes the connection after
sometime ,how can the client know that the
server has closed the connection.
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Thomas]
You could try periodically pinging the server, and if
you get "timed out", then you know that the connection
has terminated.
</blockQuote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
The write fails, but how can the client know that
it is because the server closed the connection
or is due to some other error.
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Chris]
If the client were doing a read on the socket, it would get an end of file
indication when the server closes the connection.
</blockQuote>
<blockQuote>
When the server closes its end of the socket, a TCP FIN segment is sent by
the server side. The client's TCP will send an ACK segment, and the client
will be given an end of file on its next read of the socket. Under this TCP
half-close situation, the client can continue sending data to the server.
When the client is finished writing its data, it then closes its end of the
connection. The client TCP then sends a TCP FIN segment, and the server TCP
sends an ACK segment.
</blockQuote>
<blockQuote>
I'm surprised that an error occurs during a write to the socket when the
server closes its end of the connection. I'm not sure what happens under
the scenario that you are describing, but, the variable errno should give
some indication of the type of error that had occurred. I cannot remember
off hand what the error code would be.
</blockQuote>
<blockQuote>
But the <TT> shutdown()</TT> command (associated with sockets) must
be used for this half-close situation to occur. Also, the literature states
that very few applications use the half-close feature of TCP.
</blockQuote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
Is it correct to assume that
the server closed connection
if the write fails?
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Thomas]
No....it is not correct to assume this. It could be
<EM>either</EM> machine that closes the connection. Perhaps
the client closed the connection because of over
loading the data buffer????
</blockQuote>
<P><STRONG>
<IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
In my program
I have to sense that the server has closed
connection and should try to reestablish
the connection.
</STRONG></P>
<P><STRONG>
How can this be done..?
</STRONG></P>
<P><STRONG>
please help...
</STRONG></P>
<P><STRONG>
sree
</STRONG></P>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Thomas]
</blockQuote>
<blockQuote>
In which language are you writing this program??? If
it is in Perl, Bash, Ada, then I can try and help.
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Chris]
I was just reading my "Unix Network Programming" textbook, and it mentions
that if the connection is terminated, a signal, with a value of SIGPIPE,
is sent to the process that tried to perform a read on socket.
</blockQuote>
<blockQuote><DL><DT>
Sooo....., I snooped around on Google, and found a URL,
<DD><A HREF="http://www.linuxsocket.org/book/programs/part2/chap10/sigpipe-client.c"
>http://www.linuxsocket.org/book/programs/part2/chap10/sigpipe-client.c</A>
</DL></blockQuote>
<blockQuote>
It shows some "C" code that, I think, confirms what I believe. A SIGPIPE
signal will be generated and sent to the process that initiated the write
on the socket with the now closed server connection. The client would just
to catch the signal, and the event would be detected, thus, the client
could perform its desired response to the event (the server terminating the
connection).
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
I've read up on this stuff since, and I believe that you're right - as long
as it's understood that it's not the <EM>server</EM> itself that sends the
SIGPIPE; it's part of the IPC, which is handled by the kernel. As an
example, here is The Proper Way to raise a kid who reads:
</blockQuote>
<blockquote><pre> # Open a buffer to be dumped to a process; defined by the leading "|".
open SPOO, "|txt2gif 2&gt;/dev/null" or die "Can't fork: $!\n";
# Declare a local trap; $SIG{PIPE}'s current value is saved and will be
# restored on exit. This is the part that most folks forget to do.
local $SIG{ PIPE } = sub { die "ENOTOBACCO: Broken pipe error!\n" };
# Send some STUFF to the buffer; unless autoflush is enabled, the data
# will just sit there until...
print SPOO "Bar foo fraggle garp\n";
# ...we actually flush it into the pipe, here.
close SPOO or die "Bad spool: Papa spank! $? $!\n";
</pre></blockquote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Chris]
If I messed anything up in this, feel free to jump and give me a
good wack up the side of my head with a dead weasel (as Ben O. would say).
</blockQuote>
<blockQuote>
<IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [Ben]
Hey, hey! &lt;virtuously&gt; Don't blame your weasel addiction on *me.* You must
take full responsibility for your own actions, or you'll never get that
mustelid off your back.
</blockQuote>
<!-- end 9 -->
<P> <hr> </p>
<!-- *** BEGIN copyright *** -->
<H5 align="center">This page edited and maintained by the Editors
of <I>Linux Gazette</I>
<a href="http://www.linuxgazette.com/copying.html"
>Copyright &copy;</a> 2002
<BR>Published in issue 76 of <I>Linux Gazette</I> March 2002</H5>
<H6 ALIGN="center">HTML script maintained by
<A HREF="mailto:star@starshine.org">Heather Stern</a> of
Starshine Technical Services,
<A HREF="http://www.starshine.org/">http://www.starshine.org/</A>
</H6>
<!-- *** END copyright *** -->
<!--startcut ======================================================= -->
<P> <hr>
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
<p align="center">
<table width="100%" border="0"><tr>
<td align="right" valign="center"
><IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0"
><A HREF="..//"
><IMG SRC="../../gx/navbar/toc.jpg" align="middle"
ALT="[ Table Of Contents ]" border="0"></A
><A HREF="../lg_answer.html"
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
ALT="[ Answer Guy Current Index ]" border="0"></A></td>
<td align="center" valign="center"><A HREF="../lg_answer.html#greeting"><img align="middle"
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A> &nbsp;
<A HREF="../tag/bios.html">Meet&nbsp;the&nbsp;Gang</A> &nbsp;
<A HREF="1.html">1</A> &nbsp;
<A HREF="2.html">2</A> &nbsp;
<A HREF="3.html">3</A> &nbsp;
<A HREF="4.html">4</A> &nbsp;
<A HREF="5.html">5</A> &nbsp;
<A HREF="6.html">6</A> &nbsp;
<A HREF="7.html">7</A> &nbsp;
<A HREF="8.html">8</A> &nbsp;
<A HREF="9.html">9</A> &nbsp;
<A HREF="10.html">10</A> &nbsp;
<A HREF="11.html">11</A> &nbsp;
<A HREF="12.html">12</A>
</td>
<td align="left" valign="center"><A HREF="../../tag/kb.html"
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
ALT="[ Index of Past Answers ]" border="0"></A
><IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
WIDTH="14" HEIGHT="45" BORDER="0"></td></tr></table>
</p>
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
<!--endcut ========================================================= -->
<P> <hr>
<!--startcut ======================================================= -->
<CENTER>
<!-- *** BEGIN navbar *** -->
<!-- *** END navbar *** -->
</CENTER>
</p>
<!--endcut ========================================================= -->
<!--startcut ======================================================= -->
</BODY></HTML>
<!--endcut ========================================================= -->