Home Forums .NET libraries Xceed SFTP/FTP for .NET Closing connections problems.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21283 |

    Hello there. I have been using the FTP product (3.7.8312.9580) for some time now without issue. Recently the company that I pull files from have limited the number of connections allowed at one time to 4. This has caused my code to stop working well. It seems that the connection is not being closed and on the 5th file pull I get a “no more connections allowed from this IP” error from the FTP server.

    Here is the basic original code that was pulling the files (note that there is actually a loop around this code for all of the files that need to be pulled).

                    using (FtpConnection connection = new FtpConnection(serverUri, userId, password))
                    {
                        FtpFile source = new FtpFile(connection, remoteDirectory + filename);
                        source.CopyTo(destination, true);
                    }

     

    So in order to try to solve the problem I added a bunch of things to the code.

                    using (FtpConnection connection = new FtpConnection(serverUri, userId, password))
                    {
                        connection.SendTelnetInterruptSignal = true;
                        connection.TraceWriter = new StringWriter();
                        try
                        {
                            connection.TestConnection();

                            FtpFile source = new FtpFile(connection, remoteDirectory + filename);
                            source.CopyTo(destination, true);
                        }
                        catch (Exception ex)
                        {
                            StringWriter stringWriter = (StringWriter)connection.TraceWriter;
                            logger.LogEvent(LogLevel.Normal, stringWriter.ToString());

                            throw ex;
                        }
                        finally
                        {
                            StringWriter stringWriter = (StringWriter)connection.TraceWriter;
                            logger.LogEvent(LogLevel.Debug, stringWriter.ToString());

                            connection.CloseConnections();
                            connection.Dispose();
                        }
                    }
     

    I did everything I could think of to close the connection and it did not work. Upon reading the TraceWriter output I was interested that no quit command was sent at the end of the communication. 

    I was also interested that the connection.TestConnection() made things much worse as it seemed to crate it’s own connection using up another precious connection and not releasing it.

     What is it that I am missing to close out these connections? Or is it simply something screwy with the server?

     

    Thanks for your attention on this…

     

    Mark,

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Mark (had 1440 views)

    Diane [Xceed]
    Moderator
    Post count: 1353

    Hi Mark,

    Normally I always start by recommending to upgrade to the latest version (4.1 in this case), however in your scenario I don’t think it’s going to solve the problem. It’s still good to get the latest bug fixes though.

    You will not be able to do this directly, as FtpConnection manages all the connections under the hood and will take care of recycling the connections. It’s also possible that it keeps multiple connections open until it is done with all its transfers.

    The best solution in your case would be to use FtpClient instead and do everything manually. It kills the fact that right now the FileSystem does everything for you, however it gives you control over all your connections.

     

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Diane [Xceed] (had 237 views)

    User (Old forums)
    Member
    Post count: 23064

    Yaaaay, code rewrite. That’s always fun. 😉

    Anyhoo. As a feature in the next release, would it make sense to throw a ‘quit’ command in at the end of the connection to make sure that is closed?

    Just a thought…

     

    Thanks for the reply though.

     

    Mark,

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Mark (had 411 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Mark,

    Closing an ftp connection is not always as trivial as it should be. 

    Among other things, the origin of your problem could be coming from the ftp server.  As a matter of fact, there is usually a “recycle” that occurs on the ftp server when a connection occurs.  During this time (specified by the parameter TIME_WAIT on the server, but at the operating system level, normally), this can lead into troubles. 

    From this site http://www.ncftp.com/ncftpd/doc/misc/ftp_and_firewalls.html we learn that :

    The FTP specifies a mechanism for a default data connection, where the server can connect back to the client from port 20 to the same IP address and port number that the client is originating from on the control connection.  However, it really isn’t feasible because the preferred transfer mode is “stream mode” and would require that the default data connection be reopened with each data transfer (and TCP won’t let you do that until TIME_WAIT expires on the previous default data connection that has the same connection endpoints).

    What you could try is to transfer a lot of files using FileZilla (freeware, http://filezilla-project.org/download.php?type=client ) while setting the maximum number of the data connections to four and see how things go.  It is probably the best way to determine if your problem is coming from the ftp server or from somewhere else.

    Best regards,

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Ghislain (had 1505 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.