Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
6.8k views
in Java FTP by (420 points)
Hi,

I noticed that if server drops connection
and you use client.connected() it returns true.

How can I be sure that client is connected to server?


Br,

Juha

12 Answers

0 votes
by (51.2k points)
Hi Juha

Yes, this is a known weakness in the library. We have recently fixed it in the .NET version of the library, but the Java version still has the problem. Perhaps you can help us by testing a fix for this problem. Could you please rebuild edtFTPj from source replacing FTPClient.connected() with the following code:
    public boolean connected() {
       if (control==null)
          return false;
       else if (!control.controlSock.isConnected()) {
          control = null;
          return false;
       } else
          return true;
    }


- Hans (EnterpriseDT)
0 votes
by (420 points)
Hi,

Thanks for reply :)

I will test the fix.

-Juha-
0 votes
by (420 points)
Hi,

I have compiled library from sources and included the fix you provided.
We do some testing to our product what we are developing.
I will send info if the fix helped.


-Juha-
0 votes
by (51.2k points)
Thanks.

- Hans (EnterpriseDT)
0 votes
by (420 points)
Hi,

We have tested that fix and I think it doesn't work like expected.

We have implemented JCA Resource adapter which uses your
FTP library.

When pooled connections get disconnected by ftp server
and it is fetched from pool, there is check is connection open.

The new version of connected method seems to give incorrect
information since it says connection is open but when we try to use
it, we get:

IOException : There is no process to read data written to a pipe.


-Juha-
0 votes
by (420 points)
I think

else if (!control.controlSock.isConnected()) should also check if control.controSock is not null?
Because if it is NPE will be thrown....

---> else if (control.controlSock == null || !control.controlSock.isConnected())


-Juha-
0 votes
by (161k points)
I've made one or two other changes, please email us to request the latest version. support at enterprisedt dot com.

We'll be releasing it very soon.
0 votes
by (51.2k points)
Version 1.5.5 has now been released including a fix for this problem. See here.

- Hans (EnterpriseDT)
0 votes
by (420 points)
Hi,

I have made simple test case to test this fix.

I'm sorry to say that the fix doesn't work.

connected() method still returns true although server is killed while connection is open.

Sample code:
-----------------------------------------------------------
import com.enterprisedt.net.ftp.FTPException;

public class Test {
   Test() {      
      try {         
         FTP ftp = new FTP();
         ftp.connect();         
         try {
            System.out.println("Sleeping one minute.... Kill  ftp server process now!!");
            Thread.sleep(60000);

         } catch (InterruptedException e) {}
         
         System.out.println("------>" + ftp.connected());         
         ftp.disconnected();               
      } catch (FTPException e) {         
         e.printStackTrace();
      }            
   }
      
   public static void main(String[] args) {   
      new Test();
   }
}

-----------------------------------------------------------------------------
import java.io.IOException;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPException;

public class FTP {

   FTPClient ftpClient = null;
   FTP() throws FTPException {
      ftpClient = new FTPClient();
      try {
         ftpClient.setRemoteHost("10.10.10.10");
         ftpClient.setRemotePort(21);
         ftpClient.setTimeout(20000);

      } catch (IOException e) {
         throw new FTPException(e.getMessage());
      }
   }

   public void connect() throws FTPException {

      try {
         ftpClient.connect();
         ftpClient.login("yyyyyyyyyyy", "xxxxxx");
         String[] dirList = ftpClient.dir(".");
         for (int i = 0; i < dirList.length; i++) {
            System.out.println(dirList[i]);
         }
      } catch (IOException e) {
         throw new FTPException(e.getMessage());
      }
   }

   public boolean connected() {
      return ftpClient.connected();
   }

   public void disconnected() throws FTPException {

      try {
         ftpClient.quit();
      } catch (IOException e) {
         throw new FTPException(e.getMessage());
      }
   }
}

----------------------------------------------------------------------------------------[/code]
0 votes
by (161k points)
It isn't guaranteed to work - all it does is return the result of Socket.isConnected(). If the socket still thinks it is connected, it will return true. Just because the server is killed doesn't mean clients know about it ...

Categories

...