3

Possible Duplicate:
How can I programmatically remove the 2 connection limit in WebClient

My FTP script was working great until I started to try and run multiple threads at once. After excessive debugging it turns out the FtpWebRequest simply times out if two other threads are already doing something. (Uploading, checking if a file/directory exists or creating a directory.)

I already tried implementing a lock in the ftp class so that only one thread can ever create an FtpWebRequest at a time (and then close the lock when getting the response ofc) but that didn't help.

Each request uses it's own FtpWebRequest object so I'm not quite sure why this is happening. I can upload 10 files simultaneously to the same FTP server when using the FileZilla client so I can't imagine it's a problem on the server's end.

Is there some static behind-the-scenes thing in .NET that is causing this issue?

Example function that times out >2 threads:

public class ftp
{
    private string host = null;
    private string user = null;
    private string pass = null;
    private int bufferSize = 2048;

    /* Construct Object */
    public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }

    private object fileFolderCheckLock = new object(); //Only check if a file/dir exists one thread at a time
    private object requestLock = new object(); //Don't create multiple ftprequests simultaneously. Exit this lock when the response is being received.

    /* Create a New Directory on the FTP Server */
    public bool CreateDirectory(string newDirectory)
    {
        FtpWebRequest ftpRequest = null;
        FtpWebResponse ftpResponse = null;
        try
        {
            lock(requestLock)
            {
                if(!newDirectory.EndsWith("/")) newDirectory += "/";
                //Console.WriteLine("chk: "+host + "/" + newDirectory);
                Uri theuri = new Uri(host + "/" + newDirectory);
                //Console.WriteLine("theuri: "+theuri.ToString());
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)WebRequest.Create(theuri);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* When in doubt, use these options */
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Timeout = 10000;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
                /* Establish Return Communication with the FTP Server */
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            }
        }
        catch (Exception ex){ Console.WriteLine("CreateDirectory Exception"+ex.ToString()); }
        finally
        {
            /* Resource Cleanup */
            try{ftpResponse.Close();}catch(Exception){}//??
            ftpRequest = null;
        }
        return true;
    }
}

Can anyone tell me why this happens?

Community
  • 1
  • 1
natli
  • 3,782
  • 11
  • 51
  • 82

1 Answers1

7

You are probably hitting the default maximum connections. Take a look at this: How can I programmatically remove the 2 connection limit in WebClient

Community
  • 1
  • 1
Jason Whitted
  • 4,059
  • 1
  • 16
  • 16
  • That seems to have fixed it.. all those hours wasted for something this simple, argh! I have decided to blame Microsoft ^^ Thx for your answer! – natli Dec 17 '12 at 20:56
  • 2
    Bottom line is I called this in Main(): `System.Net.ServicePointManager.DefaultConnectionLimit = 10;` – natli Dec 17 '12 at 20:57