UDP gurus? Trying to figure out total packet size.

BlitzMax Forums/BlitzMax Programming/UDP gurus? Trying to figure out total packet size.

kronholm(Posted 2007) [#1]
So let's assume I'm sending packets back and forth between a server and client, and they only contain 1 byte of data. Obviously the packet size is not 1byte, because there's header information etc. on the packet.

Is there somekind of way to calculate or see what the total packet size is, or a way to predict it?

I have been scratching my head at the packets I've captured with Ethereal. They are anything from 40 to 63 byte, but still only contain 1 byte of data, and the source/remote ip addresses in the header are the same. So it's not as straightforward as I had hoped, to predict what any given packet's size is.

Reason I want this precize packet size number is to have both server and client display the exact amount of data being sent and received by the game.

Anyone? :D


Yan(Posted 2007) [#2]
DISCLAIMER: It's been many many moons since CCNA and this kinda stuff is easily forgotten (by me at least ;o)), so it'd probably be best to google for 'udp encapsulation overhead' or something.

I seem to recall the overhead being 28 (20 + 8) bytes for UDP and 40 (20 + 20) bytes for TCP. Remember that transport mechanisms will add their own encapsulation.

Probably best to not rely on my flakey memory thought. Do a google! ;o)


FlameDuck(Posted 2007) [#3]
Is there somekind of way to calculate or see what the total packet size is, or a way to predict it?
Yes. Look at the OSI / TCP model. Each layer adds its own header and trailer. Also it's important to remember that the data-link layer, in the case of Ethernet, will extend the frame size to 512 bits, to ensure proper collision detection, regardless of previous size. So if your packet is being sent over Ethernet, it's always going to use at least 64 bytes.


bradford6(Posted 2007) [#4]
UDP headers are 8 bytes.
UDP headers contain 4 fields:
1) source port number = 2 bytes
2) destination port number = 2 bytes
3) length of data portion = 2 bytes (Length: The length of the entire UDP datagram, including both header and Data fields.)
4) UDP checksum. = 2 bytes

the packet size = the 8 byte header + the size of your DATA.

keep in mind that as your packet snakes it's way across the internets, it may change size due to the transport mechanism being used.

It could get encapsulated into another packet or frame, etc but when the UDP packet gets to the destination, it will be the same size as it was at the source.

hope this helps.


Karja(Posted 2007) [#5]
In short: no, there's no (reasonable) way to know the exact amount of data. But you can make educated guesses. Like FlameDuck mentions, have a look at the OSI model.

Example: UDP over IPv4 over Ethernet
Total packet size >= UDP data + UDP header + IPv4 header + MAC frame header

Why >=? Because the headers contain optional fields; for example, IP can have DiffServ fields as well, which will increase the size.

Also, to complicate things further, you can never just assume that a typical set of stacked protocols are being used; UDP with IPv4 and Ethernet is probably most common, but things change depending on the uplink. For example, a WLAN uplink would have additional headers. DSL would also introduce additional layer 2 protocols. (Although that's not visible for client computers connecting to a modem.)

If you want to know the exact amount of data you -can- add a traffic sniffer to analyze the traffic and match incoming/outgoing data (and its complete frame sizes) with your known received data. But that's not reasonable. Instead, you can make a rough estimate since you know that each packet must -at least- have an IPv4 header and a MAC frame header. You can safely add that static number, and get a sliightly better number.


kronholm(Posted 2007) [#6]
Thank you all so much! Very informative, sure as hell got my answer :)