For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > November 2005 > Portable way to close() and prevent RST with non-empty receive queue?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Portable way to close() and prevent RST with non-empty receive queue?
Thomas Kho

2005-11-17, 7:01 pm

Hello,

I'm wondering if the following snippet is a portable way to prevent a
TCP RST when there's still data in the receive queue.

shutdown(sock, SHUT_WR);
close(sock);

Background:
I'm implementing a HTTP monitoring server that responds to all requests
with the same reply, and in the process doesn't bother to read the
request. On Linux 2.6.14 without the shutdown(), the client gets a RST
instead of the connection properly closing.

Regards,

Thomas Kho

P.S. I stumbled across this solution to prevent the RST here:
http://cs.ecs.baylor.edu/~donahoo/p...kets/TCPRST.pdf

Maxim Yegorushkin

2005-11-18, 7:56 am


Thomas Kho wrote:
> Hello,
>
> I'm wondering if the following snippet is a portable way to prevent a
> TCP RST when there's still data in the receive queue.
>
> shutdown(sock, SHUT_WR);
> close(sock);
>
> Background:
> I'm implementing a HTTP monitoring server that responds to all requests
> with the same reply, and in the process doesn't bother to read the
> request. On Linux 2.6.14 without the shutdown(), the client gets a RST
> instead of the connection properly closing.


That's strange. close() in Linux does not send RST, rather FIN, unless
you tweaked SO_LINGER option.

Barry Margolin

2005-11-18, 7:01 pm

In article <1132310720.280963.277590@z14g2000cwz.googlegroups.com>,
"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> wrote:

> Thomas Kho wrote:
>
> That's strange. close() in Linux does not send RST, rather FIN, unless
> you tweaked SO_LINGER option.


I think the OP may be thinking of Windows.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Andrei Voropaev

2005-11-21, 7:57 am

On 2005-11-17, Thomas Kho <thomaskho@gmail.com> wrote:
[...]
> Background:
> I'm implementing a HTTP monitoring server that responds to all requests
> with the same reply, and in the process doesn't bother to read the
> request. On Linux 2.6.14 without the shutdown(), the client gets a RST
> instead of the connection properly closing.
>


Nothing will help. If you close connection without reading the input
stream, and the client doesn't check for EOF(FIN) before trying to write
(or has put already lots of data in the out-buffer) then the client will
receive RST. Shutdown is really of no help here. That's just a way to
signal to remote side that you are done with writing and it may start
sending, or to signal kernel that you don't want to read from the socket
anymore. RST is sent by the kernel when it receives data for the socket
that doesn't do reading anymore or is closed. So, if you don't read, RST
will be sent when client attempts to send something.

--
Minds, like parachutes, function best when open
Daniel C. Bastos

2005-11-21, 7:01 pm

In article <dls8lk$gm3$1@chessie.cirr.com>,
Andrei Voropaev wrote:

> On 2005-11-17, Thomas Kho <thomaskho@gmail.com> wrote:
> [...]
>
> Nothing will help. If you close connection without reading the input
> stream, and the client doesn't check for EOF(FIN) before trying to write
> (or has put already lots of data in the out-buffer) then the client will
> receive RST. Shutdown is really of no help here. That's just a way to
> signal to remote side that you are done with writing and it may start
> sending, or to signal kernel that you don't want to read from the socket
> anymore. RST is sent by the kernel when it receives data for the socket
> that doesn't do reading anymore or is closed. So, if you don't read, RST
> will be sent when client attempts to send something.


As far as I am able to verify, only by closing the socket an RST will
be sent if more data arrives. So, the ``doesn't do reading anymore''
case doesn't seem to be true. If it is, how do you do it? My tests
were made only on FreeBSD.
Andrei Voropaev

2005-11-22, 4:00 am

On 2005-11-21, Daniel C. Bastos <dbast0s@yahoo.com.br> wrote:
> In article <dls8lk$gm3$1@chessie.cirr.com>,
> Andrei Voropaev wrote:

[...]
>
> As far as I am able to verify, only by closing the socket an RST will
> be sent if more data arrives. So, the ``doesn't do reading anymore''
> case doesn't seem to be true. If it is, how do you do it? My tests
> were made only on FreeBSD.


Well. You are right. This stuff is specific to the system. On linux RST
is sent both when connection is closed and when shutdown(s, SHUT_RD) is
done. Looks like on BSD in the last case the packets are silently
dropped and no RST returned.


--
Minds, like parachutes, function best when open
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com