[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: write to socket problem
From: |
David Chisnall |
Subject: |
Re: write to socket problem |
Date: |
Thu, 8 Apr 2010 17:46:23 +0100 |
On 8 Apr 2010, at 17:34, Andreas Höschler wrote:
> call blocks and never returns and thus renders the service inoperable. I have
> already spent a couple of hours with the select, write, send,...
> documentation but haven't found a solution for this problem yet. Any idea how
> I can find out whether the reading end has closed? I expected select to do
> this, but it does not. select returns and FD_ISSET(_sock, &fdset)) gives me
> TRUE. But then the send blocks!
Unfortunately, there is no good way of doing this. The connection is being
closed due to a timeout (and may be closed by a NAT in the way for the same
reason). The solution that we use for XMPP is to send a space character every
50 seconds of inactivity. This works because the protocol is XML and cdata in
the outer scope is ignored. You might consider something similar for your
protocol.
The problem you are encountering is that this is not a graceful shutdown of the
connection. When the connection times out, there is no tear-down of the TCP
session, the other end just gives up. When you write to the socket, your
server does not know that the other end has closed, so it will send the data
and wait for the acknowledgement. It then waits for its own timeout period for
this to be received.
On most socket implementations (not sure if this includes Solaris), the a write
on a blocking socket will block once the TCP window is full. The window will
start to fill up if acknowledgements are not received for packets.
David
-- Sent from my IBM 1620