Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
2.6k views
in Java FTP by (120 points)
Hi, we are experiencing a problem were we are being "disconnected" from a ftp server after what appears to be about 30 seconds. Here is a quick summary of our problem
in code:

SSHFTPClient sftpClient = new SSHFTPClient();
sftpClient.setRemoteHost(this._hostName);
sftpClient.setRemotePort(this._portNumber);

writeLog("Setting user-name and password");
sftpClient.setAuthentication(this._userName, this._userPassword);
sftpClient.getValidator().loadKnownHosts("C:/Documents and Settings/All Users/.ssh/known_hosts");
sftpClient.connect();
sftpClient.setType(FTPTransferType.BINARY);
sftpClient.chdir(this._ftpDirectory);

FTPFile files[] = sftpClient.dirDetails(this._ftpDirectory);
for(int i=0; i<files.length;i++)
{
   String outputFileName = this._localDirectory
                            + currFile.getName();
    File f = new File(outputFileName);
   String tInProgressFile = files[i].getName() + ".inprogress";
   sftpClient.rename(files[i].getName(), tInProgressFile);
   
   sftpClient.get(new FileOutputStream(f), tInProgressFile);
   
   // Code omitted here.  We basically call external process to consume this file.
   // This usually takes about 5 to 10 seconds.  On very large files, it takes 25 to 45 seconds
   
   //The code below throws an exception 
   if(sftpClient.connected())
      sftpClient.rename(renamedFile, currFile.getName()
                                + ".successful");
   else
      throw new Exception("Connection to FTP server has been lost!");
                           
   // The exception above gets thrown whenever we process very large files longer than 25 to 45 seconds
    //  The problem is that using netstat -i still shows these connections as active on our machine.   
}


It is easy enough to throw in a reconnect if we see we have disconnected, but the problem is that our netstat -a shows that port goes to a "CLOSE_WAIT" state. Our partner, who runs the FTP site, has many active connections showing after we process several large files with this reconnect logic, so it seems like we are still connected (somehow) when we call connected() and return false.

What can we try to fix this problem?

I can post logs if needed.

Thank you!

1 Answer

0 votes
by (162k points)
You are probably best connecting, downloading a file and disconnecting before processing it locally. The server is throwing you off after 30 sec by the looks.

The connected() method is not 100% reliable, so it is best not to use it. A socket in CLOSE_WAIT state is not connected.

Categories

...