Sftp unstable



Sftp unstable


I have set up, in c#, code to upload one single xml file to a remote sftp server. However it seems to be random if it's successful or not. Often trying to run the upload it reports that connection has been refused because it seems to be ftp not sftp. However trying again 5 minutes later, it transfers the file fine.

First I had the idea that the session never disconnects from the server and this was locked for login again until timeout. I added session.close() to close the connection without any luck. However I am unsure if this actually disconnects the session. This is a c# cli script which runs several times during an hour.

Transferring with Filezilla never causes this issue. I can also connect with several instances to the server without issues.

Is the api itself somehow denying me to use the code again unless some one has gone by?


So, any idea what's going on?

Reply with quote


Site Admin
martin avatar
Prague, Czechia

Re: Sftp unstable

Sorry, but this is too vague.

Though to give you some information:

- There's nothing that would prevent you from using the assembly frequently.
- If you get "connection refused" from the server, it's the server that refuses the connection, that has nothing to do with WinSCP.
- Of course, the problem can be that you do not close the previous connections (what makes the server refuse you to open another one).
- FileZilla may retry unsuccessful connection, without you noticing it. Double check its log.

Reply with quote



Hi. Thanks for your reply. Checking the logs on filezilla I can't see it failing to connect any time. It always seem to work.

the following code has been implemented:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
Protocol = Protocol.Sftp,
HostName = server_address,
UserName = username,
Password = password,
PortNumber = port,

SshHostKeyFingerprint = "<fingerprint>"

Console.WriteLine("Uploading ADI => " + file_to_upload + ":" + Environment.NewLine +
"Host = " + server_address + Environment.NewLine +
"Username = " + username + Environment.NewLine +
"Port = " + port + Environment.NewLine +
"Destination folder = " + destination_folder);

using (Session session = new Session())
// Connect

// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
transferOptions.FilePermissions = null;
transferOptions.PreserveTimestamp = false;

TransferOperationResult transferResult;
transferResult = session.PutFiles(file_to_upload, destination_folder, false, transferOptions);

// Throw on any error

// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);


catch (Exception e)
Console.WriteLine("Error: {0}", e);

The error received at seemingly random is:

Error: WinSCP.SessionRemoteException: Server unexpectedly closed network connection.
Authentication log (see session log for details):
Using username "<username>".

Authentication failed.
The server rejected SFTP connection, but it listens for FTP connections.

Did you want to use FTP protocol instead of SFTP? Prefer using encryption.
at WinSCP.SessionLogReader.Read(LogReadFlags flags)
at WinSCP.SessionElementLogReader.Read(LogReadFlags flags)
at WinSCP.CustomLogReader.WaitForNonEmptyElementAndCreateLogReader(String localName, LogReadFlags flags)
at WinSCP.Session.Open(SessionOptions sessionOptions)
at Script.Program.sftp_upload(String server_address, String username, String password, Int32 port, String file_to_upload, String destination_folder)

Any ideas?

Reply with quote


You can post new topics in this forum