Synchronous File Transfer

0 votes
asked Apr 3, 2014 by vsat (230 points)
edited Apr 4, 2014

Hi, i am using the SFTP for .NET 2.0 libraries. I need to upload file to sftp server in synchronous mode. May i know the list of commands to do? Also if synchronous transfer fails, need to have some mechanism to find the failure and re-transmit it.

3 Answers

+1 vote
answered Apr 4, 2014 by Tomas Knopp (58,890 points)
selected May 13, 2016 by Rebex KB
 
Best answer

See list of file transfer synchronous methods available in Rebex SFTP:

for multi file operations there are these synchronous methods:

If the upload fails you will get an exception. You can retry the file transfer with the code snippet below:

        int cnt = 0;
        int maxRetry = 3;

        Sftp sftp = new Sftp();
        sftp.Connect(server);
        sftp.Login(user, password);

        while (cnt <= maxRetry)
        {
            try
            {
                sftp.PutFile(@"localPathToFile", "remotePathToFile");
                sftp.Disconnect();
                break;
            }
            catch (NetworkSessionException ex)
            {
                if (cnt == maxRetry)
                {
                        sftp.Disconnect();
                    throw;
                    }

                if (ex.Status != NetworkSessionExceptionStatus.ProtocolError)
                {
                    // reconnect needed if the status is other than ProtocolError
                    sftp.Disconnect();
                    sftp = new Sftp();
                    sftp.Connect(server);
                    sftp.Login(user, password);
                }

                cnt++;
            }
        }
    }
0 votes
answered Apr 3, 2014 by Tomas Knopp (58,890 points)
edited Apr 3, 2014

Hello,

you could try the below code. It connects and authenticates to the SFTP server and then uploads one file and disconnects.

To handle errors use try catch block as in the below example:

        Sftp sftp = new Sftp();
        sftp.Connect("server");
        sftp.Login("user", "pass");

        try
        {
            sftp.PutFile("localFilePath", "remoteFilePath");
        }
        catch (NetworkSessionException ex)
        { 
            // handle exception
        }

        sftp.Disconnect();
0 votes
answered Apr 4, 2014 by vsat (230 points)
edited Apr 4, 2014

I am using Rebex SFTP.NEt 2.0. I have not found Rebex.Networking.dll in binaries. Do i need to download from Rebex website? Please advice.

commented Apr 4, 2014 by Tomas Knopp (58,890 points)
edited Apr 4, 2014

Please login into your account and then download the latest Rebex SFTP version (2014R1). There are three dlls you will need Rebex.Common.dll, Rebex.Networking.dll and Rebex.Sftp.dll.

commented Apr 4, 2014 by Tomas Knopp (58,890 points)
edited Apr 4, 2014

To me it looks like you are using some older version of Rebex SFTP, so please remove all old Rebex referenced libraries, and add all of the new ones as in my above answer. This should solve the problem.

...