Bytech India
asked on
Renci SSH Socket Exception
Hi,
I am using Renci.SshNet to upload the files over SFTP. Below is my code.
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
Please suggest how can we solve this.
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;
}
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);
This scirpt is also not helpful.Please suggest how can we solve this.
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;
}
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
are you connecting to a host that exists?
can you use a SFTP client and connected to the host at your test machine?