Re: How to terminate a socket in CLOSE_WAIT state

From: Slava M. Usov (stripit.slough_at_gmx.net)
Date: 05/20/04


Date: Thu, 20 May 2004 19:19:32 +0200


"Francois PIETTE" <francois.piette@overbyte.be> wrote in message
news:40acd762$0$9867$a0ced6e1@news.skynet.be...

[...]

> > CLOSE_WAIT is required by the TCP RFCs, mostly so the machine can
> > properly handle duplicate packets arriving after the connection is
> > closed.
>
> I'm perfectly aware of that.

CLOSE_WAIT is the state in which a socket may remain indefinitely -- more
exactly until the user of the socket decides to close it [in the TCP sense,
by sending a FIN to the peer]. A socket enters this state when it receives a
FIN from its peer. In short, the time spent in CLOSE_WAIT has nothing to do
with the behavior -- or misbehavior -- of the peer, and everything to do
with your software. The only exception is when some data have been sent but
not ACKed by the peer at the time of close, in which case TCP may be trying
to resend the data before sending the FIN.

It may also be that MS labels some other state as CLOSE_WAIT, but I find it
very unlikely.

[...]

> ? I mean closing after it is already gracefully closed but in CLOSE_WAIT
> state, waiting for the remote end to reply.

If you're sure that you actually call shutdown() or closesocket() on the
very socket that remains in CLOSE_WAIT, then there is no way to close it
gracefully. You can only close it ungracefully, by specifying an abortive
linger option, with zero or non-zero timeout.

Alternatively, you may avoid the situation in the first place, by disabling
the Nagle algorithm on the socket and waiting for a confirmation after each
send with a timeout. This is going to be a catastrophe in terms of
performance, though.

S



Relevant Pages

  • Re: close and O_NONBLOCK on TCP/IP socket in Linux
    ... >> reading what i am interested in from the peer. ... If they don't read, but only write, then they won't see FIN, and ... >> to report the FD as writable after nonblocking shutdown of write?. ... OP the socket is closed after he got what he wanted. ...
    (comp.unix.programmer)
  • Re: What situations cause WSAECONNABORTED to be returned by recv?
    ... Software caused connection abort. ... > 2) calling shutdownon the socket, and the peer application never ... > FIN) ...
    (microsoft.public.win32.programmer.networks)
  • Re: Sockets debugging tools
    ... > to peer for a long time. ... I'll just stick to looking out for ETIMEDOUT from the socket I/O ... > during write it'll get SIGPIPE because the pipe is closed by OS already. ... EPIPE is send's analogue to recv returning 0, or rather, I found out ...
    (comp.unix.programmer)
  • Re: Another socket programming question
    ... When you close one side peer, the second side can fill that only if it do ... closing of peer socket, but accepted one only if it will do mentioned op. ... One for listening that can be started and stopped and one for sending data ... BeginReceiveCallback loop listening for data, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Recv and fwding in multi-threaded sockets programming
    ... I am new to socket programming in Java and I have a newbie question. ... > would forward it to the other 4 connections. ... listen for incoming message from peer and forward it to other thread. ... A sender that recieves messages from the queue and sends them ...
    (comp.lang.java.programmer)