Search code examples
javaclienttftpserver-response

Apache commons tftp client, detecting not responding server


I wrote a simple TFTP client in Java, using org.apache.commons.net.tftp.TFTPClient. As server for testing i use tftpd64 and while it's running, everything works fine. But when there isn't any server running, functions sendFile() and receiveFile() are returning.

I'm trying to catch SocketTimeoutExeption, but functions don't throw anything. I've checked packets with Wireshark, and I can see a Write Request packet, but of course there is no response, since I don't have server running. How can i check, that there were no response?


Solution

  • TFTP is an UDP protocol. We have not connection and we have not error control. If server is running and it responds, it sends to your client an UDP datagram that you are not sure to receive. You can not have information "datagram is lost" with UDP. Timeout is managed by your implementation of TFTP.

    According to commons apache code and apache TFTPClient doc, you already have timeout management.

    • setDefaultTimeout(int timeout) : Set the default timeout in milliseconds to use when opening a socket.
    • setSoTimeout(int timeout) : Set the timeout in milliseconds of a currently open connection.
    • setMaxTimeouts(int numTimeouts) : Sets the maximum number of times a receive attempt is allowed to timeout during a receiveFile() or sendFile() operation before ending attempts to retry the receive and failing.

    • sendFile and receiveFile throw IOException. Maybe SocketTimeoutException is never thrown because timeout expiration is not managed by socket.