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

0

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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 stackoverflow.com/q/44606028/850848 + Setting ftpReq.ContentLength has no effect.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.