Re: close and O_NONBLOCK on TCP/IP socket in Linux

From: Andrei Voropaev (avorop_at_mail.ru)
Date: 02/27/04


Date: 27 Feb 2004 08:54:51 GMT

On 2004-02-26, David Schwartz <davids@webmaster.com> wrote:
>
> "Torsten" <torsten-iversen@sol.dk> wrote in message
> news:600802cf.0402260927.262f2af9@posting.google.com...
>>> I guess you'll have to call 'shutdown' and then wait for the shutdown
>>> to
>>> complete in a non-blocking way. This is what you should be doing anyway,
>>> otherwise what happens if other side sends some more data just before you
>>> call 'close'?
>
>
>> I guess they would get broken pipe, which is what they should get if i
>> disconnect. Obviously i do not close the stream before i have finished
>> reading what i am interested in from the peer.
>
>
> Actually, they'll get 'connection reset by peer', which will lead them
> to believe that an error took place.

Just to be clear in that. After local side is closed, remote shall get
FIN. If they don't read, but only write, then they won't see FIN, and
then they won't see Reset By peer and as final measure they will get
Broken pipe. If they read then they shall see FIN and stop sending.

>
>
>> I have searched the web but failed to find out how to "wait for the
>> shutdown to
>> complete in a non-blocking way.". Maybe i should just wait for select
>> to report the FD as writable after nonblocking shutdown of write?. But
>> then again, my info page (redhat 7.2 info pages:
>> Libc->Sockets->Open/Close Sockets->Closing a Socket) says about
>> shutdown:
>
>
> No, keep selecting for 'read' and wait until 'read' returns zero. That's
> how a closed connection is indicated to a non-blocking program.
>
>
[...]
>>
>> My program does what i want it to if i call close after finishing
>> write (no shutdown),
>
> If and only if the other side does not try to send any data before you
> call 'close'.

Strange statement. This is true only if the application of OP is
interested in the data that remote sends. But according to the words of
OP the socket is closed after he got what he wanted. The rest of stuff is
not interesting for him. I assume that application of OP has done all
necessary things to warn remote side that connection will be closed
(this is the matter of politness though, not technical requirement).

>
>> but i would prefer not to rely on behaviour that
>> is not explicitly stated in the manual. I am trying to find some
>> documentation that confirms that what i am doing is correct. In my
>> info pages i could find nothing about handling of pending transmit
>> data during close of a nonblocking socket.
>
>
> Don't close the socket, shut it down and then wait for it to complete
> the shutdown. Then and only then release the socket descriptor.

This is not absolutely necessary. It highly depends on the protocol.
Take for example HTTP 1.0. Client sends request and then to mark end of
request it does shutdown for write and keeps reading the socket. Server
after sending the response simply closes connection because it shall not
read anything else from client.

In SMPP protocol the side that wants to close the socket sends special
PDU to the peer, after getting response to that PDU, again it simply
closes the socket, without using shutdown, because there shall be
nothing else coming from the peer, and if someting comes then it is
problems of the peer.

The main point here is this. After application calls 'close' on the
stream socket, kernel makes sure that all the data in the buffers, and
the FIN packet are delivered to the peer. All incoming data for that
connection shall be thrown away and RST shall be sent (after the FIN was
sent). This is the behaviour of Linux 2.4.x. I can't speak for other
operating systems. The best for the OP would be to check man pages for
his system and not for unknown system in the internet.

Andrei



Relevant Pages

  • Re: How to terminate a socket in CLOSE_WAIT state
    ... CLOSE_WAIT is the state in which a socket may remain indefinitely -- more ... FIN from its peer. ... with zero or non-zero timeout. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Graceful socket cleanup problem
    ... Put recvbetween shutdown() and closesocket() ... calling shutdown causes the pending completion requests for the peer ... closesocket), surprisingly, the host socket (the one that initially shut ...
    (microsoft.public.win32.programmer.networks)
  • 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: closing ASyncSocket
    ... I reread the help for the ShutDown() call. ... not close the socket, and resources attached to the socket will not be freed ... What if I want to Closethe connection to return resources. ... receiving side after I received the last data. ...
    (microsoft.public.vc.mfc)
  • 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)