7

I open a connection to an FTP server at the start of my program.

Before I perform operations on the server I want to check if the connection was successfully established. The easiest fast manner so if the connection is gone, I will try to connect again.

I used this code to do this:

private boolean checkConnection()
{
    try 
    {
        boolean success = ftpClient.login(user_name, password);
        if(success)
            return true;
        else 
            return false;
    }
}

But this method throws a NullPointer exception when the connection was closed.

I can check the connection with the ftpClient.connect(server, port); but that is like attampting to connect all over again.

What is the bes way to check the connection?

Reporter
  • 3,897
  • 5
  • 33
  • 47
Michael A
  • 5,770
  • 16
  • 75
  • 127

2 Answers2

14

Trying to send a simple sendNoOp() and checking the reply might be a good way to lightly check the connecion:

private boolean checkConnectionWithOneRetry()
{
    try 
    {
        // Sends a NOOP command to the FTP server. 
        boolean answer = ftpClient.sendNoOp();
        if(answer)
            return true;
        else
        {
            System.out.println("Server connection failed!");
            boolean success = reconnect();
            if(success)
            {
                System.out.println("Reconnect attampt have succeeded!");
                return true;
            }
            else
            {
                System.out.println("Reconnect attampt failed!");
                return false;
            }
        }
    }
    catch (FTPConnectionClosedException e) 
    {
        System.out.println("Server connection is closed!");
        boolean recon = reconnect();
        if(recon)
        {
            System.out.println("Reconnect attampt have succeeded!");
            return true;
        }
        else
        {
            System.out.println("Reconnect attampt have failed!");
            return false;
        }

    }
    catch (IOException e) 
    {
        System.out.println("Server connection failed!");
        boolean recon = reconnect();
        if(recon)
        {
            System.out.println("Reconnect attampt have succeeded!");
            return true;
        }
        else
        {
            System.out.println("Reconnect attampt have failed!");
            return false;
        }   
    }
    catch (NullPointerException e) 
    {
        System.out.println("Server connection is closed!");
        boolean recon = reconnect();
        if(recon)
        {
            System.out.println("Reconnect attampt have succeeded!");
            return true;
        }
        else
        {
            System.out.println("Reconnect attampt have failed!");
            return false;
        }   
    }
}
Michael A
  • 5,770
  • 16
  • 75
  • 127
  • Looks like you are using org.apache.commons.net.ftp In this case you can use isAvailable() or isConnected() methods they are running mach faster than ftpClient.sendNoOp() Disadvantage is that both methods still don't guaranties that connection works. https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/SocketClient.html#isAvailable() – David Abragimov Jul 31 '19 at 20:32
3
private FTPClient ftp = null;

private void connect()
{
            ftp = new FTPClient();

            try {

                ftp.connect("Server",port);
                boolean login = ftp.login("username", "password");
                System.out.println(" login "+ login );

            } catch (FTPConnectionClosedException e) {          
                System.err.println("ERROR :: FTP Server Unreachable");
                sleep();
                connect();          
            } catch (SocketException e) {
                System.err.println("ERROR :: FTP Server Unreachable");
                sleep();
                connect();  
            } catch (IOException e) {
                System.err.println("ERROR :: FTP Server Unreachable");
                sleep();
                connect();  
            }
}

public void sleep(){
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
}
Reporter
  • 3,897
  • 5
  • 33
  • 47
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37