Re: Flush recv buffer
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Sun, 06 May 2007 17:27:06 -0400
chsalvia@xxxxxxxxx wrote:
Suppose you have an open file descriptor (socket) with data waiting to
be read. Is there any way to simply flush the buffer, and remove all
data from the socket so that a subsequent call to recv would not
return any data?
Obviously, you could just close the socket, or simply recv the
remaining data. But is there any way to simply remove all data from
the recv buffer?
You could just read it and throw it away, using non-blocking
mode or select or poll or some such to detect when there's nothing
immediately available for reading. But there's no way to detect
data that's "in flight," on its way to your system but not there
yet. One nanosecond after you decide you've drained the stream,
Lo! there may be more data sitting there. In fact, there may be
quite a large amount of data. Imagine that the other side has
sent a megabyte, but that the first of the underlying packets got
garbled although the later packets arrived intact. You drain all
the available data -- none of the "after the gap" data is available
yet -- and just after you're done, the retransmission of the bad
packet comes through, and in the blink of an eye the whole megabyte
suddenly appears ...
If you want to flush "the current message" and then resume
receiving, your own application needs to define a way to mark
the message boundaries; TCP/IP delivers a uniform (almost) stream
of bytes and has no notion of subdivisions in that stream. Your
application can read and ignore data until it detects the end of
the message (by whatever means you've chosen), and then resume
reading and processing subsequent messages.
If you want to ignore everything else that might ever arrive
on the stream, why not just close it and have done? You can, if
you want, shut down the inbound path and leave the outbound path
operational (so you can send to the other end, but it can't send
to you).
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- References:
- Flush recv buffer
- From: chsalvia
- Flush recv buffer
- Prev by Date: Re: about signal handler.
- Next by Date: Re: pipe - understanding
- Previous by thread: Re: Flush recv buffer
- Next by thread: Re: Flush recv buffer
- Index(es):
Relevant Pages
|