This project is read-only.

Client Disconnected

Nov 9, 2011 at 2:24 PM
Edited Nov 9, 2011 at 2:25 PM

When using the SSH.NET library, I am finding that the connection is dropped, and the client appears to become "locked" and needs to be restarted. The SFTP server log has this entry:

> The client was disconnected by intermediate network components or chose to  disconnect without sending a disconnect message. 

> Session server 21696: Closing session server.

> Terminating gracefully

I suspect that the connection is being dropped after calling the DownloadFile() method but because no exception is being explicitly thrown, it is difficult finding out what is causing this to occur? Any help would be very welcome!

Nov 9, 2011 at 4:35 PM

Having taken the time to read through some of the older entries, I see that my problem is exactly the same as the problem described in an earlier discussion from August:

"Disconnect Blocks Forever"

http://sshnet.codeplex.com/discussions/267304

Presumably this has been thrown up as a "trust issue".

Has this issue been solved in the SSH library?

Thanks!

 

Nov 9, 2011 at 4:59 PM

Hi

Try the latest changeset and let me know if it works out or not.

Nov 10, 2011 at 9:37 AM

Hi kenneth_aa

We are using the latest build of the library - 2011.9.28

I'm not yet sure what trust level the application is running in on the live environment.

Do you think it will solve the issue if the application is run in Full Trust?

Thanks

Nov 10, 2011 at 10:41 AM

Hi

I don't know what scenario you use this library in, so I can't say. I would give it a go for Full Trust, let us know if it works and what you did.

Thank you.

Nov 10, 2011 at 10:43 AM

We are running the SSH library in a Windows Service. But I will report back and let you know.

Thanks

Nov 10, 2011 at 10:50 AM

Thanks for a quick reply.

If it isn't too much to ask, could you write some skeleton code for a windows service using sshnet? That way I can get it into the tests and hopefully detect some bugs!

So something like:

  1. Test starts
  2. Setup/Install service
  3. Setup test environment(?)
  4. Start service
  5. Get notified what happens 
  6. Stop service
  7. Uninstall service
  8. Test results
  9. Test ends.

+ configuration files if needed.

I haven't done any windows service projects before, so I would really like to use your knowledge of how to do it.

Coordinator
Nov 22, 2011 at 2:50 AM

Hi,

 

Sorry for delay in reply to this thread, was busy with other project.

Do you still experience the problem? Did you try the latest code? Not the released one but one available in "Source Code" tab?

If problem still exists, can you post or send us privately the code you using?

Also, what server you trying to connect to? May be your server expects some additional information to be sent to indicate succesfull disconnect.

 

Thanks,

Oleg

Nov 28, 2011 at 1:19 PM
Edited Nov 28, 2011 at 3:40 PM

Hi Oleg

Thanks for replying.

I have not managed to remove the errors that sometimes happens when the Disconnect() method is invoked. 

I have tried using your latest code build of of the assembly.

So in order to get around the problem, I used the following methods:

1) Took the code out of the Windows Service and wrote it as a straightforward Windows Console. This does not have any issues of Trust levels. And I used the standard Windows Scheduler tool to manage the start/stop (rather than rely on a Timer in the Windows Service - very inefficient!)

2) For additional safe execution of my application, I wrapped all the method calls made by the SFTP library in my client code in a Timeout so that the application can be restarted in the case of an error thrown by one of its methods. So I have all the method calls made by the client in code like this:

 

TimeoutCall.Invoke(sftpClient.DownloadFile, remoteFile.FullName, localFs, _timeOutMilliseconds);

 

Where TimeoutCall.Invoke is a static method:

 

        public static void Invoke<T1>(Action<T1> action, T1 arg1, int timeOutMs)
        {
            Thread workingThread = null;

            Action<T1> wrappedAction = (p1) =>
            {
                workingThread = Thread.CurrentThread;
                action(arg1);
            };

            try
            {
                IAsyncResult result = wrappedAction.BeginInvoke(arg1, null, null);
                if (result.AsyncWaitHandle.WaitOne(timeOutMs))
                {
                    wrappedAction.EndInvoke(result);
                }
                else
                {
                    workingThread.Abort();
                    throw new TimeoutException();
                }
            }
            catch (TimeoutException ex)
            {
                LogClass.LogError(String.Format("Call to {0}(): {1}", action.Method.Name, ex.Message));
            }
        }

 

The above code is written for a method which takes a single argument as input but does not return anything, such as the Download() method which takes a single string argument (the path to the file to download). I also have additional overloads of this for other methods which take no arguments (in the case of Connect() and Disconnect()) or two (in the case of DeleteFile()) - all of which are void methods which do not return anything, And the use of a Func() delegate for the ListDirectory() (2 input args and an IEnumerable of type SftpFile).

Thanks