0

I trying to create a TFTP server but when it receives a file it seems that not all of it is saved on to the server (some bytes are missing). The file is created fine and the majority of data is written but as the file is not complete it is classed as corrupt and unopenable. Does anyone know how to fix this issue?

main class

            WRQ WRQ = new WRQ();
            ACK ACK = new ACK();
            DatagramPacket outPacket;
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));
            byte[] bytes;
            byte[] fileOut;
            outPacket = WRQ.firstPacket(packet);
            socket.send(outPacket);

            socket.receive(packet);

            while (packet.getLength() == 516){

            bytes = WRQ.doWRQ(packet);
            bufferedOutput.write(bytes);

            outPacket = ACK.doACK(packet);
            socket.send(outPacket);

            socket.receive(packet); 

            }

            bytes = WRQ.doWRQ(packet);
            bufferedOutput.write(bytes);

            bufferedOutput.close();

            outPacket = ACK.doACK(packet);
            socket.send(outPacket);

WRQ class

public class WRQ {

public DatagramPacket firstPacket(DatagramPacket packet) throws IOException{

    ACK ACK = new ACK();
    DatagramPacket ACKpacket = ACK.doACK(packet);

    //takes ACK packet and sets block # as 0 to signal that this is the first packet in a WRQ
    byte[] ACKcontents = new byte[3];
    ACKcontents = ACKpacket.getData();
    ACKcontents[2] = 0;
    ACKcontents[3] = 0;
    ACKpacket.setData(ACKcontents);

    return ACKpacket;

}

public byte[] doWRQ(DatagramPacket packet){

    int length = packet.getLength();
    byte[] packetData = packet.getData();
    byte[] data = new byte[length - 4];
    data = Arrays.copyOfRange(packetData, 4, length - 4);

    return data;

}

}
DMo
  • 591
  • 2
  • 6
  • 13
  • You could just edit your previous question. – Kazekage Gaara May 20 '12 at 15:41
  • Thanks for your help. I'll bare this in mind next time. Would you be able to help as much with this question? – DMo May 20 '12 at 15:45
  • 1
    The problem is probably in code you haven't shown us. For example the WRQ.doWRQ() method. Try logging the bytes sent on the client, and written to the file on the server, and compare them. – JB Nizet May 20 '12 at 15:45
  • The problem is with the last lot of data being received. it isnt written to the file. I'll investigate further but any help in the meantime is greatly appreciated. – DMo May 20 '12 at 15:53
  • Duplicate of own post: [Using BufferedOutputStream to write bytes to file with while loop. Help needed](http://stackoverflow.com/questions/10674665/using-bufferedoutputstream-to-write-bytes-to-file-with-while-loop-help-needed). – user207421 May 21 '12 at 01:44

1 Answers1

1

This code looks very suspicious to me:

byte[] packetData = packet.getData();
byte[] data = new byte[length - 4];
data = Arrays.copyOfRange(packetData, 4, length - 4);

Your output array (data) is of length length - 4, but you only copy length - 8 bytes to it. If the bytes to ignore in packetData are the first 4 bytes, it should be

data = Arrays.copyOfRange(packetData, 4, length);

because the last argument is not a length, but the to index (exclusive). See the javadoc for details.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks, I've just figured this out myself but indeed this was the problem. I was thinking that it was a length arguement. – DMo May 20 '12 at 16:13