Dear all,
I use PuTTy with the following settings for port forwarding which works fine:
putty.exe -ssh user@hostname -L 5555:localhost:5555 -L 5554:localhost:5554 -N -pw password
Now I want to do this from C# and therefore I'd like to use the SSH.NET library. So I use the following to forward the ports:
```
public void ForwardPortLocally(uint portNumber)
{
try
{
var p = new ForwardedPortLocal("localhost", portNumber, "localhost", portNumber);
client.AddForwardedPort(p);
p.Start();
}
catch (Exception ex)
{
throw new Exception(String.Format("ERROR: Port forwarding failed: {0}", ex.Message));
}
}
```
I know I need to clean up after my self an therefore I use the following code:
```
public void Dispose()
{
var toDispose = new List<ForwardedPortLocal>();
foreach (var forwardedPort in client.ForwardedPorts.Where(forwardedPort => forwardedPort.IsStarted))
{
forwardedPort.Stop();
toDispose.Add((ForwardedPortLocal)forwardedPort);
}
foreach (var port in toDispose)
{
client.RemoveForwardedPort(port);
port.Dispose();
}
if (client.IsConnected)
{
client.Disconnect();
}
client.Dispose();
}
```
This is where the problem begins. I am not able to close my ports properly. While setting up the forwarded ports is working fine, the attempt to close it always fails.
Once I have setup the forwarded ports and closed the application I am not able to open them anymore because they are still in use and I get the exception:
> "Only one usage of each socket address (protocol/network address/port) is normally permitted"
I have used sysinternals' tcpview to see whether the ports stay open and they do but the process is "non-existent". I have to logg off and on again from Windows to be able to re-open the port.
Am I doing something wrong? Have I found a bug in the library?
Regards
P.S.: I also experienced that the method i use to get rid of the forwarded ports takes seconds to run which is very long and seems a bit odd to me.
Comments: ** Comment from web user: bimbimone **
I use PuTTy with the following settings for port forwarding which works fine:
putty.exe -ssh user@hostname -L 5555:localhost:5555 -L 5554:localhost:5554 -N -pw password
Now I want to do this from C# and therefore I'd like to use the SSH.NET library. So I use the following to forward the ports:
```
public void ForwardPortLocally(uint portNumber)
{
try
{
var p = new ForwardedPortLocal("localhost", portNumber, "localhost", portNumber);
client.AddForwardedPort(p);
p.Start();
}
catch (Exception ex)
{
throw new Exception(String.Format("ERROR: Port forwarding failed: {0}", ex.Message));
}
}
```
I know I need to clean up after my self an therefore I use the following code:
```
public void Dispose()
{
var toDispose = new List<ForwardedPortLocal>();
foreach (var forwardedPort in client.ForwardedPorts.Where(forwardedPort => forwardedPort.IsStarted))
{
forwardedPort.Stop();
toDispose.Add((ForwardedPortLocal)forwardedPort);
}
foreach (var port in toDispose)
{
client.RemoveForwardedPort(port);
port.Dispose();
}
if (client.IsConnected)
{
client.Disconnect();
}
client.Dispose();
}
```
This is where the problem begins. I am not able to close my ports properly. While setting up the forwarded ports is working fine, the attempt to close it always fails.
Once I have setup the forwarded ports and closed the application I am not able to open them anymore because they are still in use and I get the exception:
> "Only one usage of each socket address (protocol/network address/port) is normally permitted"
I have used sysinternals' tcpview to see whether the ports stay open and they do but the process is "non-existent". I have to logg off and on again from Windows to be able to re-open the port.
Am I doing something wrong? Have I found a bug in the library?
Regards
P.S.: I also experienced that the method i use to get rid of the forwarded ports takes seconds to run which is very long and seems a bit odd to me.
Comments: ** Comment from web user: bimbimone **
Thanks to the example provided in : https://sshnet.codeplex.com/discussions/528720
I was able to 'pin-point' my problem and I have found my solution.
They were right, this is not a bug in the library it was my very own application that was causing this trouble.
The problem was that other processes for whom I had created the local port forwarding, kept one end of the port open. (In my case adb.exe, android debug bridge).
The solution is to __first__ close these processes before stopping the forwarded ports. Now stopping is completing fast and successfully.
Thanks for the great support and sorry for the inconvenience.