I am making a TFTP application. As per protocols RFC all data must be send in chunks of 512 bytes in max size. So each packet can be <= 512 bytes.
I read each file in to a byte[] outgoingData = new byte[512]; array and i send it to Client however it seems that something goes wrong when this gets a file that is smaller than 512 bytes in total size like a ascii file or a .ini, .css, .html, etc..
Oddly for UDP protocol each transfer up to 3mb has came past without a big loss. The only loss that seems to happen is when the last chunk of a file is read that is less than 512 bytes.
private void sendData() throws Exception
{
DatagramPacket data = new DatagramPacket(outgoingData, outgoingData.length, clientAddress, clientPort);
InputStream fis = new FileInputStream(responseData);
int a;
while((a = fis.read(outgoingData,0,512)) != -1)
{
serverSocket.send(data);
Thread.sleep(5);
}
}
Since this is a problem regarding reading the file how can i fix the loss at the end of the file and the problem where it does not read a file smaller than 512
Client:
private void receiveData() throws Exception
{
DatagramPacket receiveData = new DatagramPacket(incomingData, incomingData.length);
OutputStream fos = new FileOutputStream(new File("1"+data));
while(true)
{
clientSocket.receive(receiveData);
if(receiveData.getLength() == 512)
{
fos.write(incomingData);xx
} else {
fos.write(incomingData);
fos.close();
break;
}
}
clientSocket.close();
}