Re: Flush recv buffer



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
.



Relevant Pages

  • Re: How hard is socket programming?
    ... malicious user in the most robust way. ... s = socket() - create a socket handle ... as many recv calls as necessary, ... It is a STREAM protocol, ...
    (microsoft.public.vc.mfc)
  • Re: How hard is socket programming?
    ... s = socket() - create a socket handle ... as many recv calls as necessary, which is somewhere between 1 and some unknown value n ... It is a STREAM protocol, ... chunks of data, based on how the sender and receiver stacks, and all intermediate routers, ...
    (microsoft.public.vc.mfc)
  • Re: Flush recv buffer
    ... Is there any way to simply flush the buffer, ... data from the socket so that a subsequent call to recv would not ... Obviously, you could just close the socket, or simply recv the ...
    (comp.unix.programmer)
  • Re: Synchronization with CAsyncSocket in CE 6.0
    ... number of bytes in the first packet, followed by packets of, say 1024 bytes. ... recv() operations, reassembling it into the format that you need. ... don't forget that recv can return zero, if the socket has ... So, CAsyncSocket while possibly a bad way to do things, does not appear to ...
    (microsoft.public.windowsce.embedded)
  • Re: Receive no data
    ... It is a best practice to shutdown ... a socket for sending once all data has been sent. ... I see multiple recv() statements in the code. ...
    (microsoft.public.win32.programmer.networks)