1

I am trying to make a SSH Tunnel application. I successfully open a tunnel with dynamic port and it works until the first connection by browser is made. After one page is loaded, the tunnel is not working anymore and I unable to open next page until I restart a tunnel. Sometimes there is an exception

"Object reference not set to an instance of the object." "System.NullReferenceException" in Renci.SshNet.dll.

My code is following:

SshClient client;
ForwardedPortDynamic port;

public void Start(string server, string user, string password, int serverport=22)
{
        client = new SshClient(server, user, password);
        client.KeepAliveInterval = new TimeSpan(0, 0, 5);
        client.ConnectionInfo.Timeout = new TimeSpan(0, 0, 20);
        client.Connect();

        if (client.IsConnected)
        {
            try
            {
                port = new ForwardedPortDynamic("127.0.0.1", 1080);
                client.AddForwardedPort(port);
                port.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

}

public void Stop()
{
    port.Stop();
    port.Dispose();
    client.Disconnect();
    client.Dispose();
}

What's wrong with it? Could you please help me make my app work! Many thanks in advance!

  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hamid Pourjam Jun 28 '15 at 22:41
  • On which line the error happens? – Hamid Pourjam Jun 28 '15 at 22:43
  • I would suggest that you debug your application. Set a breakpoint, and then check what goes wrong. – Afzaal Ahmad Zeeshan Jun 28 '15 at 22:48
  • 2
    Likely not a duplicate if the exception happens inside the third-party library, and if the OP has not changed the library code. – John Saunders Jun 28 '15 at 22:49
  • Exception sometimes happens inside the third-party library after I am trying to reload page when it stops to load. I guess it's the reason of the exception, but I can't understand what's wrong with the library and my code. –  Jun 29 '15 at 09:43

1 Answers1

2

Solution: don't use SSH.net from NuGet, use the latest version from their website, there are different versions. The last one is working good.

  • This. The version on NuGet is not even the recommended release right now. – iamkrillin Oct 19 '15 at 20:04
  • Please note: https://github.com/sshnet/SSH.NET/releases "For Visual Studio 2015 and higher, the SSH.NET NuGet package remains our primary distribution mechanism." – BCdotWEB Mar 26 '20 at 13:06