微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

FluentFTP 错误 421 无法创建套接字...尝试并行上传时

如何解决FluentFTP 错误 421 无法创建套接字...尝试并行上传时

我正在使用最新的 FluentFTP,并且正在尝试实现并行上传功能

问题是某些连接抛出异常并显示“无法创建套接字”消息,并且我在失败的文件上看不到任何特定模式。

private List<string> ParallelUpload(string dir,string ftpPath,int maxConnections,FtpRemoteExists existsMode,FtpVerify verifyOptions) {

    Dictionary<string,byte[]> imgList = ReadImagesFromdisk(dir);
    var fileList = imgList.Keys.ToList();
    var FailedUploads = new List<string>();
    var syncFailed = new object();
        
    fileList.ParallelForEarch(maxConnections,(fileName) => {
        var tmpFailed = DoUploadImage(ftpPath,fileName,existsMode,verifyOptions);
        lock (syncFailed) {
            FailedUploads.AddRange(tmpFailed);
        }
    });
        
    return FailedUploads;
}
        
private List<string> DoUploadImage(string ftpPath,string file,FtpVerify verifyOptions) {
        
    var FailedUploads = new List<string>();
        
    using (var ftp = new FtpClient()) {
        ftp.Host = _ftpServer;
        ftp.Port = _ftpPort;
        
        if (!string.IsNullOrEmpty(_ftpUser) && !string.IsNullOrEmpty(_ftpPassword))
            ftp.Credentials = new NetworkCredential(_ftpUser,_ftpPassword);
        
        ftp.EncryptionMode = FtpEncryptionMode.Auto;
        ftp.ValidateAnyCertificate = true;
        
        ftp.OnLogEvent = OnFTPLogEvent;
        
        ftp.AutoConnect();
        
        var remotePath = $"{ftpPath}{Path.GetFileName(file)}";
        
        try {
            ftpStatus = ftp.UploadFile(file,remotePath,createRemoteDir,verifyOptions);
        } catch (Exception ex) {
            //catch exception
        }
        
        if (ftpStatus.IsFailure())
            FailedUploads.Add(file);
    }
        
    return FailedUploads;
}

public void ParallelForEarch<TSource>(this IEnumerable<TSource> source,int parallelSlots,Action<TSource> action) {
    Parallel.ForEach(source,new ParallelOptions() { MaxDegreeOfParallelism = parallelSlots },action);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。