1

I'm tring to execute this TFTP DOS send command by TFTP Client in Java:

tftp -i 192.168.1.13 put file1.romz file1.romz

It means to send the file file1.romz to host: 192.168.1.13, in binary way and rename the file when arrives to file1.romz. About TFTP DOS command.

This is the code:

import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.apache.commons.net.tftp.TFTP;
import org.apache.commons.net.tftp.TFTPClient;

public class TFTPClientToSend {

public final static void main(String[] args) {
    boolean closed;
    int transferMode = TFTP.BINARY_MODE;
    String hostname, localFilename, remoteFilename;
    TFTPClient tftp;

    // Get host and file arguments
    hostname = "192.168.1.13";
    localFilename = "file1.romz";
    remoteFilename = "file1.romz";

    // Create our TFTP instance to handle the file transfer.
    tftp = new TFTPClient();

    // We want to timeout if a response takes longer than 60 seconds
    tftp.setDefaultTimeout(60000);

    // Open local socket
    try {
        tftp.open();
    } catch (SocketException e) {
        System.err.println("Error: could not open local UDP socket.");
        System.err.println(e.getMessage());
        System.exit(1);
    }

    // We haven't closed the local file yet.
    closed = false;

    // We're sending a file
    FileInputStream input = null;

    // Try to open local file for reading
    try {
        input = new FileInputStream(localFilename);
    } catch (IOException e) {
        tftp.close();
        System.err.println("Error: could not open local file for reading.");
        System.err.println(e.getMessage());
        System.exit(1);
    }

    // Try to send local file via TFTP
    try {
        tftp.sendFile(remoteFilename, transferMode, input, hostname, 69);
    } catch (UnknownHostException e) {
        System.err.println("Error: could not resolve hostname.");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println(
                "Error: I/O exception occurred while sending file.");
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        // Close local socket and input file
        tftp.close();
        try {
            input.close();
            closed = true;
        } catch (IOException e) {
            closed = false;
            System.err.println("Error: error closing file.");
            System.err.println(e.getMessage());
        }
    }

    if (!closed) {
        System.exit(1);
    }
}
}

The fields are correct, but I get the following error message:

Error: I/O exception occurred while sending file.
Incorrect source port (69) in request reply.

The TFTP UDP port is officially 69. Windows Firewall is off and TFTP Lantronix Server is enable to update new firware by TFTP. Java uses a port randomly to send data in the first frame of the string. I think this is why I have this error message. How could you change that source port? Or is it another problem that causes this error?

Thanks in advance.

1 Answers1

-1

Like a workaround. I've used this to execute a Windows DOS command in Java.

try {
    Process proceso = Runtime.getRuntime().exec("tftp -i 192.168.1.13 put 
file1.romz file1.romz");
} catch (IOException e) {
    e.printStackTrace();
}