I am sending a file over datagram sockets, all of the packets arrive perfectly fine except the last one, since it does not fill the buffer of bytes when it is sent through the network those bytes in the array which didn't have data now have junk information that corrupt the file as I put it together, is there any work around this? Please help.

Instead of just sending fixed length buffers, send a byte count followed by the buffer. For most packets the count will == the buffer size, but for the last packet the count will tell you how many bytes are data, so you can ignore the rest.