Pingpong
Pingpong

Reputation: 8009

"Socket read operation has timed out" exception with SSH.NET when connecting to FTP port 21

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

Upvotes: 3

Views: 18610

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202088

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:

Upvotes: 5

Gyan
Gyan

Reputation: 14

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

Upvotes: -1

Related Questions