Link to home
Start Free TrialLog in
Avatar of Bytech India
Bytech India

asked on

Renci SSH Socket Exception

Hi,
I am using Renci.SshNet to upload the files over SFTP. Below is my code.

var connectionInfo = new ConnectionInfo(host,
                                    objFI.UserId,
                                    new PasswordAuthenticationMethod(objFI.UserId, objFI.Password),
                                    new PrivateKeyAuthenticationMethod("rsa.key"));
 try
                    {
                        using (SftpClient sftp = new SftpClient(connectionInfo))
                        {
                            try
                            { 
                                    sftp.Connect();
                                    if (sftp.IsConnected)
                                    {
                                        using (var file = File.OpenRead(FilewithUploadingext))
                                        {
                                            if (file != null)
                                            {
                                                sftp.BufferSize = 4 * 1024; // bypass Payload error large files                                                     
                                                sftp.UploadFile(file, Path.GetFileName(FilewithUploadingext), (uploadCallback) =>
                                                {
                                                    ServerFileSize = uploadCallback;
                                                });
                                            }
                                        }                                         
                                        sftp.Disconnect();
                                        sftp.Dispose();
                                    }      }                             
                            catch (Exception ex)
                            { 
                                return false; }    }  }
                    catch (Exception ex)
                    { 
                        return false;
                    }

Open in new window


In my systems, this is working smoothly but in other, this code is getting below exception.
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"

I have also tried
sftp.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);

Open in new window

This scirpt is also not helpful.

Please suggest how can we solve this.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

the error already provided the possible reasons.

are you connecting to a host that exists?

can you use a SFTP client and connected to the host at your test machine?
And clean up your code. This is sufficient:

var connectionInfo = new ConnectionInfo(host, objFI.UserId, new PasswordAuthenticationMethod(objFI.UserId, objFI.Password), new PrivateKeyAuthenticationMethod("rsa.key"));
using (SftpClient sftp = new SftpClient(connectionInfo))
{
    sftp.Connect();
    if (sftp.IsConnected)
    {
        using (var file = File.OpenRead(FilewithUploadingext))
        {
            if (file != null)
            {
                sftp.BufferSize = 4 * 1024;
                sftp.UploadFile(file, Path.GetFileName(FilewithUploadingext), (uploadCallback) => { ServerFileSize = uploadCallback; });
            }
        }

        sftp.Disconnect();
    }

    return true;
}

Open in new window

Avatar of Bytech India
Bytech India

ASKER

Hi Ryan,

Yes, host exists and the same is working with ChilKat FTP library with the same configuration. But with Renci.SSH this is not working.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial