0 votes
by (180 points)
edited by

I want to check sftp connectivity Issue with CB via Rebex in asp.net with C#.If the Rebex Connectivity fails not related to the credentials or any such , I need to get that exact connection issue and update accordingly. I am using rebex dlls 5.0.7161.0. Please update code level fix possible at the earliest
Dlls used
Rebex.Common
Rebex.SFTP
Rebex.Networking

Below is the code with which I am doing connectivity

    private void ConnectSftp(ref Sftp objSFTP)
    {
        try
        {
            SftpConnectionState IsActive = null;
            string[] sSFTPHost = ConfigurationSettings.AppSettings["SFTPHost"].Split('|');

            if (objSFTP == null)
            {
                objSFTP = new Sftp();
            }


            IsActive = objSFTP.GetConnectionState();

            if (IsActive.Connected == false)
            {
                objSFTP.Connect(sSFTPHost[0], int.Parse(sSFTPHost[1]));
                objSFTP.Login(sSFTPHost[2], sSFTPHost[3]);
            }

        }
        catch (Exception ex)
        {
            objSFTP.Disconnect();



        }
    } 

Error Details are as below:
Error Details :
12/04/2021 10:57:23 AM [Errormessage]:Error while connecting SFTP: 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.
at Rebex.Net.Sftp.Connect(String serverName, Int32 serverPort, SshParameters parameters)
at NeSTBLL.BLL_SFTPEngine.ConnectSftp(Sftp& objSFTP)

12/04/2021 10:57:28 AM [Errormessage]:An established connection was aborted by the software in your host machine.
at Rebex.Net.Sftp.CipxfYZ(AYPecc , String , Stream , Int64 , Int64 )
at Rebex.Net.Sftp.CipxfYZ(FileMode , AYPecc , String , String , Int64 , Int64 , Int64 )
at Rebex.Net.Sftp.GetFile(String remotePath, String localPath)
at NeSTBLL.BLLSFTPEngine.SFTPDownload(String FileType, String FileName)

Applies to: Rebex SFTP

1 Answer

0 votes
by (144k points)

The following try/catch block might address your needs:

try
{
    ...
}
catch (SftpException ex)
{
    // determine whether the exception is related to authentication
    Exception innerEx = ex.InnerException;
    if (ex.Status == SftpExceptionStatus.OperationFailure
        && innerEx is SshException
        && (string)innerEx.Data["ProtocolMessage"] == "AuthenticationCancelledByUser")
    {
        // error related to authentication (credentials or such)
    }
    else
    {
        // if not, determine whether it is a TCP error 
        while (innerEx != null)
        {
            if (innerEx is SocketException) break;
            innerEx = innerEx.InnerException;
        }

        var socketEx = innerEx as SocketException;
        if (socketEx != null)
        {
            // error related to TCP protocol or socket
        }
        else
        {
            // error related to something else
            switch (ex.Status)
            {
                case SftpExceptionStatus.ProtocolError:
                    ...
                    break;
                case SftpExceptionStatus.Timeout:
                    ...
                    break;
                case ...
                    break;
                default:
                    break;
            }
        }
    }
}
catch (Exception ex)
{
    // other unexpected issues
}
...