我正在尝试创建SSH隧道应用程序。我成功打开了一个带有动态端口的隧道,它一直工作,直到通过浏览器建立第一个连接。加载一个页面后,隧道不再工作,我无法打开下一页,直到我重新启动隧道。有时会出现异常
“对象引用未设置为对象的实例。” Renci.SshNet.dll中的“System.NullReferenceException”。
我的代码如下:
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();
}
它出了什么问题?你能帮我把我的应用程序搞定!非常感谢提前!
答案 0 :(得分:2)
解决方案:不要使用来自NuGet的SSH.net,使用他们网站上的最新版本,有不同的版本。最后一个工作正常。