【问题标题】:FtpWebRequest times out when running more than 2 threads [duplicate]运行超过 2 个线程时,FtpWebRequest 超时 [重复]
【发布时间】:2012-12-17 20:27:57
【问题描述】:

可能重复:
How can I programmatically remove the 2 connection limit in WebClient

在我开始尝试一次运行多个线程之前,我的 FTP 脚本运行良好。经过过度调试后,如果其他两个线程已经在做某事,FtpWebRequest 就会超时。 (上传,检查文件/目录是否存在或创建目录。)

我已经尝试在 ftp 类中实现一个锁,这样一次只有一个线程可以创建一个FtpWebRequest(然后在获得 c 的响应时关闭锁),但这并没有帮助。

每个请求都使用它自己的FtpWebRequest 对象,所以我不太清楚为什么会这样。使用FileZilla客户端时,我可以同时上传10个文件到同一个FTP服务器,所以我无法想象这是服务器端的问题。

.NET 中是否存在导致此问题的静态幕后因素?

超时 >2 个线程的示例函数:

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;
    }
}

谁能告诉我为什么会这样?

【问题讨论】:

    标签: c# multithreading ftp


    【解决方案1】:

    您可能正在达到默认的最大连接数。看看这个:How can I programmatically remove the 2 connection limit in WebClient

    【讨论】:

    • 这似乎已经解决了......所有这些时间都浪费在这么简单的事情上,啊!我决定责怪微软^^谢谢你的回答!
    • 底线是我在 Main() 中调用它:System.Net.ServicePointManager.DefaultConnectionLimit = 10;
    猜你喜欢
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多