• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java Networking Question

clamum

Lifer
I'm transmitting a file via UDP from one host to another. The amount of data from the file that is being packaged in a UDP datagram is controllable by user input; right now I'm testing it with a 100 byte payload. The other host receives the data and appends it to a file on it's machine.

The problem is that I have to create a new byte array to store the data received BEFORE I can check how much data is carried by the datagram (i.e., you have to create a DatagramPacket before you can call the DatagramPacket's getData() method). Now, normally the data will be 100 bytes, but if the filesize of the file isn't divisible by 100, then the last chunk of data will be less than 100 bytes, but stored in an array of size 100. Thus, then I go to append the last chunk of data to the file, it will be larger than it should be.

Does anyone have any suggestions on how I could go about solving this problem? Thanks!

EDIT: I fixed it, there's a method in the DatagramPacket (getLength) that actually checks the amount of data transferred, not just the array size. That solved it.
 
Well I created an instance variable to keep track of the current chunk size being sent so I can refer to that when creating the array.

Now I have a synchronization issue where the receiver goes and creates a 0-byte array (since the chunk size is initially set to 0) before the send method is called and the correct chunk size is set. Grrrr.
 
Back
Top