I am using a filechannel with a byte buffer to send packets over the network. My problem is that when the filechannel reads the last few bytes it appends the last bit of data from previous bytes read even though I am clearing the byte buffer after I write.
For example,
Byte Buffer size = 512 For the last iteration, the remaining bytes to send is 372. It reads the last 372 but it also appends another 140 bytes (512-372) to the end of it, and appears that last 140 bytes is from the previous 512 bytes sent.
Heres my code:
ByteBuffer bBuffer = ByteBuffer.allocate(512);
while (fChannel.read(bBuffer) > 0) {
bBuffer.flip();
datagramChannel.write(bBuffer);
bBuffer.clear();
//omitted code
}
Using a
DatagramChannel
this way is really never going to work. You're just sending chunks of the file which may or may not arrive, or arrive twice, or more, in any order. Use TCP.Even if it does magically work, and I have my suspicions that there are further bugs in the 'omitted code', or the receiving code:
The correct version of the copy loop between channels in Java is as follows:
Note that you must continue writing while there is still anything in the buffer (
buffer.position() > 0
), and that you mustcompact()
rather thanclear()
so as not to assume that thewrite()
emptied the buffer.If it wasn't a
DatagramChannel
you should use a buffer much larger than 512, for example 8192.