The java docs state that if a packet exceeds the maximum packet width it will be truncated. I want to check for possible truncation. The UDP header has a 16-bit length field, but I don’t see any way to access the UDP header in java. There is a length() method in the datagrampacket class, but the docs are not clear whether that returns the original packet length, or the truncated packet length. i.e. it’s not clear whether length() is simply computing the length of the byte array that java gets from the OS or if length() is actually extracting the length from the packet header.
Worst-case scenario I can append 2 bytes to the front of my packets that would include the length of the packet which I could check against when the packet arrives at it’s destination. I would like to avoid this if possible though as the length is already in the packet header and sending it again adds a fair bit of overhead for small packets.
I tried sending increasingly larger packets from my dev server to my local machine and calling length() on the datagrampacket on my local machine to see if the values diverge. The server will send increasingly larger packets starting at 100 bytes until they exceed an unsigned 16-bit length in which case the server throws an exception. Oddly, my client stops receiving packets once they hit 3,000 bytes in length. They leave the server, but they never make it to my client application. I’m not sure if windows is discarding them, or if Java is doing it, or if they get lost while enroute. Thus I wasn’t able to test the packet.length() against the packet that the server sends as they stop showing up after 3,000 in size.
~don