3

I got the exception below when the client tries to connect to FTP server:

Socket read operation has timed out after 30000 milliseconds.

Source = "Renci.SshNet"

at Renci.SshNet.Abstractions.SocketAbstraction.Read(Socket socket, Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) at Renci.SshNet.Session.SocketReadLine(TimeSpan timeout) at Renci.SshNet.Session.Connect() at Renci.SshNet.BaseClient.Connect() at SftpClient.ReadAllBytesAsync() in C:\SftpClient\SftpClient.cs:line 42

Code below:

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 version: 2016.1.0

However, it connects via telnet like below via command prompt:

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

A staff on server side sends me public certificates, which I installed on my Windows 10.

Any idea?


Solution:

Use this one: github.com/robinrodricks/FluentFTP

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Pingpong
  • 7,681
  • 21
  • 83
  • 209

2 Answers2

5

SSH.NET is SSH/SFTP client (port 22).

You cannot use it to connect to an FTP server (port 21). FTP and SFTP are two completely different protocols.

For FTP, you can use:

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
-1

FTP and SFTP are two completely different protocols.

For FTP server (Port 21), You can use the below code are:-

Example : Upload files using 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);
}
Gyan
  • 14
  • 1
  • The `ftpReq.UsePassive = false` is usually a bad choice. For most purposes, stick with the default `true`. + Also `ReadAllBytes` is quite inefficient solution (and the question is about download rather than upload anyway). For better approaches to both upload and download, see https://stackoverflow.com/q/44606028/850848 + Setting `ftpReq.ContentLength` has no effect. – Martin Prikryl Jul 30 '20 at 11:14
  • I known **ftpReq.UsePassive = false** is usually a bad choice. In this case i used **ftpReq.UsePassive = false** because my FTP Connection Modes is in Active mode. – Gyan Aug 03 '20 at 22:25