1

I am trying to access the FTP server using SSH.NET library without any luck. I am providing same credidentials as in FileZilla which are working fine. The SSH throws errow "Socket read operation has timed out". If I use the same code as bellow but without specifying the port :21 I got an error : "User cannot authenitcated" . Can someone privide insights?

string tempHost = @"ftp.mywebsite.com";
string tempUser = @"[email protected]";
string tempPassword = @"try123";

using (SftpClient sftpClient = 
       new SftpClient((ConnectionInfo)new PasswordConnectionInfo(tempHost,21, tempUser, tempPassword)))
        {
          sftpClient.Connect();
        }

2 Answers 2

3

If in case it's still unresolved for you, below code worked for me

using (var sftp = new SftpClient(host, userName, password))
            {
                sftp.Connect();                    
                //Do some operation
                sftp.Disconnect();
            }

Ananth

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

1 Comment

Hi thanks, the above code is working but I had to apply for SFTP on my hosting account... before trying it
1

SSH tools (like Renci SSH) are meant to be used on port 22 (secure). If you want to connect to port 21 you need another library. I have used FluentFTP which is not as easy to use as Renci, but gets the job done.

Here is a code sample you can use to upload a file to a server (on version 19.1.2). Regardless if you use port 21 or 22, I would highly recommend at least 500ms between write operations to give time to the server to breath.

using (FtpClient client = new FtpClient())
        {
            client.Host = ftpAddress;
            client.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
            client.Port = 21;
            client.Connect();
            using (Stream s = new FileStream(localPathWithFileName, FileMode.Open))
            {
                try
                {
                    //log.Info($"Uploading file...");
                    client.Upload(s, ftpFilePathWithFileName);
                    //log.Info($"File uploaded!");
                }
                catch(Exception e)
                {
                    //log.Info($"{e.StackTrace}");
                }
                finally
                {
                    client.Disconnect();
                }
            }


        }

Comments

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.