连接到FTP端口21时,SSH.NET出现“套接字读取操作已超时”异常

时间:2019-09-09 18:34:55

标签: c# .net ftp sftp ssh.net

当客户端尝试连接到FTP服务器时,出现以下异常:

  

套接字读取操作在30000毫秒后超时。

     

来源=“ Renci.SshNet”

     在Renci.SshNet.Abstractions.SocketAbstraction.Read中的

(套接字套接字,Byte []缓冲区,Int32偏移量,Int32大小,TimeSpan超时)   在Renci.SshNet.Session.SocketReadLine(TimeSpan超时)   在Renci.SshNet.Session.Connect()   在Renci.SshNet.BaseClient.Connect()   在C:\ SftpClient \ SftpClient.cs:第42行的SftpClient.ReadAllBytesAsync()中

以下代码:

using (Renci.SshNet.SftpClient sftp = new Renci.SshNet.SftpClient(server,
                                                    21,
                                                    Username,
                                                    Password))                      
    sftp.Connect();  //exception here
    content = sftp.ReadAllBytes(FilePath);    
    sftp.Disconnect();                      
}

SSH.NET版本:2016.1.0

但是,它通过如下命令通过telnet连接:

telnet server_ip_address 21
220 (SFTPPlus_3.15.0) Welcome to the FTP/FTPS Service.

服务器端的工作人员向我发送公共证书,该证书已安装在Windows 10上。

有什么主意吗?


解决方案

使用这个:github.com/robinrodricks/FluentFTP

2 个答案:

答案 0 :(得分:1)

SSH.NET是SSH / SFTP客户端(端口22)。

您不能使用它连接到FTP服务器(端口21)。 FTP和SFTP是两个完全不同的协议。

对于FTP,您可以使用FtpWebRequest .NET framework class或某些第三方FTP实现。

答案 1 :(得分:-1)

FTP和SFTP是两个完全不同的协议。

对于 FTP服务器端口21 ),您可以使用以下代码:-

示例:使用FtpWebRequest上传文件

string filename = "ftp://" + ip + "//" + "HA11062020CJEIC.pdf";
FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename);
ftpReq.UsePassive = false;
ftpReq.UseBinary = true;
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.Credentials = new NetworkCredential(username, password);

string sourceFile = @"C:\Users\*****\*****\FTP Schedular\HA11062020CJEIC.pdf";
byte[] b = File.ReadAllBytes(sourceFile);  //Get local pc file

//var webClient = new WebClient();  //Get file from URL
//byte[] b = webClient.DownloadData(sourceFile); 

ftpReq.ContentLength = b.Length;
using (Stream s = ftpReq.GetRequestStream())
{
    s.Write(b, 0, b.Length);
}

FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

if (ftpResp != null)
{
    string MessageBox = (ftpResp.StatusDescription);
}