FTP only allows 2 threads at a time

I have a console app with a thread pool.  I am finding that I can only have 2 threads calling this method below.  On the third call, the request hangs and times out.  I know the default timeout is -1 so it should  never timeout.  If I set the request timeout explicitly to -1, the request does not timeout but still only allows 2 threads to process at a time.  I have verified that it is not a limitiation set by the ftp server.  It is definitely something with the code.  Please let me know if you have a suggestion.

public static void FtpDownload(string server, string username, string password, string pathIncludingFileName, string localName)

{

try

{

FtpWebRequest request = FtpWebRequest.Create("ftp://" + server + pathIncludingFileName) as FtpWebRequest;

request.Method = WebRequestMethods.Ftp.DownloadFile;

request.Credentials = new NetworkCredential(username, password);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))

using (BinaryWriter writer = new BinaryWriter(File.Open(localName, FileMode.Create)))

{

byte[] buffer = new byte[2048];

int count = reader.Read(buffer, 0, buffer.Length);

while (count != 0)

{

writer.Write(buffer, 0, count);

count = reader.Read(buffer, 0, buffer.Length);

}

}

response.Close();

 

}

finally

{

}

}



Answer this question

FTP only allows 2 threads at a time

  • Paventhan

    Mike,

    I followed your instruction and Durga and still have the same problem.  Am I doing something wrong here

    FtpWebRequest request = FtpWebRequest.Create("ftp://" + server + pathIncludingFileName) as FtpWebRequest;

    request.Method = WebRequestMethods.Ftp.DownloadFile;

    request.ServicePoint.ConnectionLimit = 10; 

    request.Credentials = new NetworkCredential(username, password);

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))

    using (BinaryWriter writer = new BinaryWriter(File.Open(localName, FileMode.Create)))

    {

    byte[] buffer = new byte[2048];

    int count = reader.Read(buffer, 0, buffer.Length);

    while (count != 0)

    {

    writer.Write(buffer, 0, count);

    count = reader.Read(buffer, 0, buffer.Length);

    }

    }

    response.Close();

     

    }

    finally

    {

    }

    }


  • dsMqplp

    Please set the connection limit to a higher value.
    ServicePointManager.DefaultConnectionLimit = 10 or

    FtpWebrequest.ServicePoint.ConnectionLimit



  • Modoko

    By default the connectionlimit is set to 2 as per the RFC recommendation. As Durga states, you can up this value. For a full discussion of connection limits and associated connection groups, please see: http://blogs.msdn.com/mflasko/archive/2005/09/11/463865.aspx

  • FTP only allows 2 threads at a time