Thursday, January 15, 2009

Apache Commons Net FTP isConnected

I had some problems at work writing an in-house Java FTP program. (Why Java? Because the other developers know Java. The company takes the expedient path of using Java for everything because than any developer can read any other developer's work.)

The Commons FTP 'isConnected()' API call would return "true" sometimes when the connection was dead.

After some hunting in the project mailing list archives, I discovered 'isConnected()' returns true as long as 'disconnect()' has not been called since the last time 'connect()' has been called. 'isConnected()' does not actually verify that the network socket is still open.

So I went in my code and replaced isConnected() with a call to noop(), which just sends a simple message to the server to verify the socket is still open. This fixed the problem.

Hopefully someone else won't have to deal with the same problem.