3

可能重复:
如何以编程方式删除 WebClient 中的 2 个连接限制

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

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

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

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

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

4

1 回答 1

7

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

于 2012-12-17T20:48:21.383 回答