I used this little console-application and everything is working fine:
What does netstat -ant say after you stop your forwarding?
using System;
using System.Diagnostics;
using Renci.SshNet;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var client = new SshClient(IP, USER, PASSWORD);
try
{
client.Connect();
Console.WriteLine("Started first client");
Console.WriteLine(RunCmd("netstat -ant | find \"22222\""));
var fw = new ForwardedPortLocal("127.0.0.1", 22222, "127.0.0.1", 22);
client.AddForwardedPort(fw);
fw.Start();
Console.WriteLine("Started Forwarding");
Console.WriteLine(RunCmd("netstat -ant | find \"22222\""));
Console.WriteLine("FW started: " + fw.IsStarted);
var fwclient = new SshClient("127.0.0.1", 22222, USER, PASSWORD);
fwclient.Connect();
Console.WriteLine("Started second client");
Console.WriteLine(RunCmd("netstat -ant | find \"22222\""));
Console.WriteLine("Press Enter to stop");
Console.ReadLine();
fwclient.Disconnect();
Console.WriteLine("Stopped second Client");
fw.Stop();
client.RemoveForwardedPort(fw);
Console.WriteLine("Stopped Forwarding");
Console.WriteLine(RunCmd("netstat -ant | find \"22222\""));
client.Disconnect();
Console.WriteLine("Stopped first client");
Console.WriteLine(RunCmd("netstat -ant | find \"22222\""));
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
Console.ReadLine();
}
private static string RunCmd(string cmd)
{
var proc = new Process
{
StartInfo =
{
FileName = "cmd.exe",
Arguments = "/c " + cmd,
UseShellExecute = false,
RedirectStandardOutput = true
}
};
proc.Start();
proc.WaitForExit();
return proc.StandardOutput.ReadToEnd();
}
}
}
Example- Output:Started first client
Started Forwarding
TCP 127.0.0.1:22222 0.0.0.0:0 ABHÖREN InHost
FW started: True
Started second client
TCP 127.0.0.1:22222 0.0.0.0:0 ABHÖREN InHost
TCP 127.0.0.1:22222 127.0.0.1:51105 HERGESTELLT InHost
TCP 127.0.0.1:51105 127.0.0.1:22222 HERGESTELLT InHost
Press Enter to stop
Stopped second Client
Stopped Forwarding
TCP 127.0.0.1:22222 127.0.0.1:51105 HERGESTELLT InHost
TCP 127.0.0.1:51105 127.0.0.1:22222 HERGESTELLT InHost
Stopped first client
TCP 127.0.0.1:22222 127.0.0.1:51105 HERGESTELLT InHost
TCP 127.0.0.1:51105 127.0.0.1:22222 HERGESTELLT InHost
The ListenSocket of the forwarding gets cleaned up as expected.What does netstat -ant say after you stop your forwarding?
But just to check. When one closes a forwarded port, is it normal that this takes seconds to complete?No.