简体   繁体   中英

Java transfer files via UDP. Compressed files are damaged?

i'm trying to transfer Files with a DatagrammSocket in Java. I'm reading the files into 4096 Byte pieces. We are using ACK, so all pieces are in the right order, we tried pdf, exe, jpg and lot more stuff successfully, but iso, zip and 7z are not working. They have exactly the same size afterwards. Do you have any idea?

Reading the Parts:

byte[] b = new byte[FileTransferClient.PACKAGE_SIZE - 32];
FileInputStream read = new FileInputStream(file);
read.skip((part - 1) * (FileTransferClient.PACKAGE_SIZE - 32));
read.read(b);
content = b;

Writing the Parts:

stream = new FileOutputStream(new File(this.filePath));
stream.write(output);
...
stream.write(output);
stream.close();

(Sorry for great grammar, i'm German)

Your write() method calls are assuming that the entire buffer was filled by receive(). You must use the length provided with the DatagramPacket:

datagramSocket.receive(packet);
stream.write(packet.getData(), packet.getOffset(), packet.getLength());

If there is overhead in the packet, eg a sequence number, which there should be, you will need to adjust the offset and length accordingly.

NB TCP will ensure 'everything gets transferred and is not damaged'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM