Re: Network buffering question



Till Crueger wrote:
Hi,

I hope this is the best newsgroup for this topic. I looked around, but I
couldn't find any more specific newsgroups.

I am working on some network code right now. For help I am using "Beej's
Guide to Network Programming". Now I stumbled upon the chapter about data
encapsulation. I get the point that you should send across the Network
some information about how much data you have been sending, so you can
reconstruct the packets correctely. So far I think I got everything.

However then he says that you need to make your buffer twice as large as a
single packet, because you might actually read past the packet you are
trying to handle at the moment. Since you can tell recv() how much you
want to recieve couldn't you just make sure this wont ever happen? I would
have tried something like this:

read size information
check if everything was recieved
read the rest (let recv() know how much to read)

Or is there some problem to this, so you actually need to use bigger
buffers or circular buffers?

Thanks for your help,
Till


It can be done the way you outline. Efficiency will be improved if you do only one recv() call, and if you fetch lots of data in the one call. Then you can analyze the size info and determine if you have a complete message. There's nothing magic about having "twice" as large a buffer, but bigger is better if you care about performance.

A circular buffer is awkward because recv() needs a continuous array. So using a large buffer will also reduce the need to copy data down when you get near the end of the buffer. I often use a buffer 10X the largest packet to get this efficiency.

--
Scott McPhillips [VC++ MVP]

.



Relevant Pages

  • Re: Interesting TCP behaviour with large sends/small buffers
    ... My current workaround is simply setting the send buffer to a larger ... The server, upon connection, sends a configurable number of bytes to ... packet before sending the next packet. ... ACK, according to the delayed ACK algorithm - 50KB bytes means 34 MSS- ...
    (microsoft.public.win32.programmer.networks)
  • Re: Interesting TCP behaviour with large sends/small buffers
    ... The server, upon connection, sends a configurable number of bytes to ... I set the client's receive buffer size to 1MBps, ... packet before sending the next packet. ... ACK, according to the delayed ACK algorithm - 50KB bytes means 34 MSS- ...
    (microsoft.public.win32.programmer.networks)
  • Re: Fundamentals question, is this how it works?
    ... You maintain a buffer for the last incomplete packet. ... receiving that many bytes i then break and wait for the next set of data ... With a tcp stream socket what happens when it is reading say 4000bytes ...
    (microsoft.public.win32.programmer.networks)
  • Re: Design of a Router
    ... packet forward it and than close the port and move to the next and so ... would ideally like the router as small and as fast as possible. ... size of your buffer (just keep in mind that if you have 4 ports than ...
    (comp.lang.verilog)
  • Re: Winsock recv() and unfragmented packets
    ... I was wondering if a call to recv() will return all data for an unfragmented ... packet shouldn't be fragmented so it'll arrive on the server in one piece.. ... Does this mean that when calling recvwith a large enough buffer will ...
    (microsoft.public.win32.programmer.networks)