Domanda

Sto cercando di creare un server TFTP ma quando riceve un file Sembra che non sia tutto salvato sul server (mancano alcuni byte).Il file è creato bene e la maggior parte dei dati è scritta, ma poiché il file non è completo è classificato come corrotto e non correttivo.Qualcuno sa come risolvere questo problema?

Classe principale

            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);
.

Classe WRQ

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;

}

}
.

È stato utile?

Soluzione

Questo codice sembra molto sospettoso:

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

L'array di uscita (dati) è di lunghezza length - 4, ma copia solo byte length - 8.Se i byte da ignorare in packetdata sono i primi 4 byte, dovrebbe essere

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

Perché l'ultima argomentazione non è una lunghezza, ma l'indice to (esclusivo).Vedi Javadoc per i dettagli.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top