How to handle lots of packets send in quick succession

Please post support and help requests here.
Posts: 7
Joined: 25 Jul 2012, 12:58

How to handle lots of packets send in quick succession

Postby marlondean » 27 Jul 2012, 14:24

Hi everyone,

I am new to network programming and POCO and I ran into a bit of a problem.

I got the networking working for single packets, but I am having difficulty dealing with a large number of packets in quick succession.

I send 100 packets from my client to the server as a test. On the server side, only every second packet or so gets handled properly. Closer investigation revealed that all packets are in the pipe, but I do not know how to get 1 packet out of the pipe to process it. I can see there are x waiting bytes, but how do I get y bytes from it? The major problem is that I don't know the size of the packets as these can be variable. I first need to read the bytes from the pipe to see the the size of the packets so I can see how much to read, therefore I have a chicken and egg situation.

Is there a way I can peak at the contents of socket.available without using socket.receiveBytes?

Posts: 19
Joined: 23 Feb 2012, 18:13

Re: How to handle lots of packets send in quick succession

Postby Royce » 27 Jul 2012, 17:05

Assuming you're doing TCP, don't get too hung up on accessing the individual TCP packets. Its not designed to work like that and you'll find yourself fighting the code every step of the way. This is not POCO specific, either. The underlying OS networking looks very similar to the POCO calls.

Besides, across long distances on the internet, the packet structure and timing you send out may not be the packet structure and timing you receive. The only thing that TCP is beholden to preserve is the sequence of payload bytes you send out.

So your code has to expect to receive bytes in random sized globs. Your code needs to work whether you get 1 packet from a call to receive bytes, or just half a packet. Or 1 and a half packets. This means you need some way, completely internal to your stream of bytes and not dependent on TCP packets, to isolate packets from one another. They can have a special delimiter byte, a null terminator, be length delimited or whatever. But the basic pattern is to append all the bytes you do get from a call on to a buffer and then analyze that buffer to see if there are any valid packets as you have defined them. If there are, extract them from the buffer and send them on to the rest of your program for processing.

Now, there is a Poco class that does all of that for you: Poco::Net::WebSocket. As long as you are OK with its overhead and both sides of your connection can support it, it works great for doing the grunt work of re-assembling discrete transmissions. Just because its a fancy new web-browser protocol, doesn't mean you can't re-purpose it for your own uses.

Return to “Support”

Who is online

Users browsing this forum: No registered users and 3 guests