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, e.g. 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'.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top