Re: close and O_NONBLOCK on TCP/IP socket in Linux
From: Andrei Voropaev (avorop_at_mail.ru)
Date: 02/27/04
- Next message: Villy Kruse: "Re: gethostbyaddr returning null for CNAME records"
- Previous message: Fletcher Glenn: "Re: waiting on multple process"
- In reply to: David Schwartz: "Re: close and O_NONBLOCK on TCP/IP socket in Linux"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Villy Kruse: "Re: gethostbyaddr returning null for CNAME records"
- Previous message: Fletcher Glenn: "Re: waiting on multple process"
- In reply to: David Schwartz: "Re: close and O_NONBLOCK on TCP/IP socket in Linux"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|