1

I have built a console application to upload a file to a FTP server. As this FTP server only accepts SFTP protocol I am using SSH.NET open source library since there are no built in method in .NET (At less for .NET 3.5, which is the one I am using under Visual Studio 2008 Professional).

I have seen a lot of examples. I have implemented a quick test application to connect to a public SFTP server and upload a file using SFTP protocol.

I am using below public free SFTP server obtained from here:

server   : demo.wftpserver.com
user     : demo-user
password : demo-user
port     : 2222

And here is the code:

    const string host             = "demo.wftpserver.com";
    const string username         = "demo-user";
    const string password         = "demo-user";
    const string workingdirectory = "/upload";
    const string uploadfile       = @"C:\test.txt";
    const int    port             = 2222;

    static void Main(string[] args)
    {
        try
        {                
            using (SftpClient client = new SftpClient(host, port, username, password))
            {
                {
                    client.Connect();
                    Console.WriteLine("Connected to {0}", host);

                    client.ChangeDirectory(workingdirectory);
                    Console.WriteLine("Changed directory to {0}", workingdirectory);

                    var listDirectory = client.ListDirectory(workingdirectory, null);
                    Console.WriteLine("Listing directory:");
                    foreach (var fi in listDirectory)
                    {
                        Console.WriteLine(" - " + fi.Name);
                    }

                    using (var fileStream = new FileStream(uploadfile, FileMode.Open))
                    {
                        Console.WriteLine("Uploading {0} ({1:N0} bytes)", uploadfile, fileStream.Length);
                        client.BufferSize = 4 * 1024; // bypass Payload error large files
                        client.UploadFile(fileStream, Path.GetFileName(uploadfile), null);
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.Out.WriteLine(e.Message);
        }

The problem is the following:

When trying to connect with the server using sentence below:

client.Connect();

I get an exception. The exception says the following:

"Value cannot be null. \r\nParameter name: All lists are either null or empty."

StackTrace  "   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)\r\n   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle)\r\n   at Renci.SshNet.Session.Connect()\r\n   at Renci.SshNet.BaseClient.Connect()\r\n   at ConsoleApplication2.Program.Main(String[] args) at C:\\Users\\Win7\\Downloads\\ConsoleApplication2\\ConsoleApplication2\\Program.cs:line 61"    string

Also, I have tried to change the timeout as explained here but without success.

However, If I try to connect to the same SFTP server using the same credentials from FileZilla client, then I can connect without problems and even upload a file.

So I do not understand what is happening. Could someone help me?

UPDATE: I have found this fix. The patch was applied in develop brank on 22 January 2017 but the last binary for download is 14th December 2016. So how can I get the binaries that includes this fix?

2 Answers 2

2

I have solved.

I downloaded the source code from developer branch in which this bug is corrected. Then I open .NET Framework 3.5 project in Visual Studio 2015 and I build it. So I obtained the binaries, DLL for release and debug. Then I come back to my Visual Studio 2008 project and I add this reference. And I have checked that with the same code now I can connect to FTP server with same credentials.

Last binary provided in SSH.NET web does not include this bug fixed.

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

Comments

-4

I used Tamir.SharpSSH for sending file in our SFTP. Try to use this one, below code is the simplest way to send file to your sftp.

Sftp sftpBase = new Tamir.SharpSsh.Sftp(_sftpHost, _sftpUid, Up_decryptPass);
sftpBase.Connect(int.Parse(_sftpPort));
sftpBase.Put(Dir + filename, sftpDir);

4 Comments

SharpSSH is a dead project. Do not use it. It's website is even down, atm.
It's not dead. We're currently using it on our projects.
The last version is from 2011 and the web page has not been updated since 2007. That's what I call dead for a security related library. It cannot even support any modern ciphers.
Suggesting use of an obsolete and insecure library over a well-maintained and up-to-date library deserves the -1 => You should delete your answer to remove the -1.

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.