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: e5490626 **
Hi,
I have the same problem with Forwadingports. The first opening port forwadin run properly. When disconnect and try to forwading with a new SSH server, the new one doesn't run, because the old connection is still "open".
I do the following:
1.- Open
```
Imports Renci.SshNet
Dim client As SshClient
Dim port As ForwardedPortLocal
If (contador.Use_server_ssh And RadioButton1Remoto.Checked) Then
ToolStripStatusLabel1.Text = "SQL Server string conection..."
CadenaDEconexion = "data source =127.0.0.1,14444; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ToolStripStatusLabel1.Text = "Connecting SSH..."
SSHConect(client, port, contador.ssh_server_ip, contador.ssh_server_port, contador.ssh_DDBB_Ipforwading)
ToolStripStatusLabel1.Text = "Conexion SSH OK..."
End If
```
Where:
contador.ssh_server_ip= ip remota del servidor SSH (por ejemplo 94.168.1.23)
contador.ssh_server_port = puerto del servidor SSH 1443
contador.ssh_DDBB_Ipforwading = ip local del equipo remoto 192.168.1.3
and
```
Public Function SSHConect(ByRef _client As SshClient, ByRef _port As ForwardedPortLocal, _SSHIPserver As String, _SSHPortServer As Integer, _SSHForwardIP As String, _SSHForwardPort As Integer) As Boolean
If (Not IsNothing(_client)) Then
If (_client.IsConnected) Then
'_port.Stop()
'_client.RemoveForwardedPort(_port)
_client.Disconnect()
End If
End If
SSHConect = False
_client = New SshClient(_SSHIPserver, _SSHPortServer, "userSSH", "passSSH")
_client.KeepAliveInterval = New TimeSpan(0, 0, 30)
_client.Connect()
_client.SendKeepAlive()
If _client.IsConnected Then
Try
_port = New ForwardedPortLocal("127.0.0.1", _SSHForwardPort, _SSHForwardIP, _SSHForwardPort)
_client.AddForwardedPort(_port)
_port.Start()
If (_port.IsStarted) Then
SSHConect = True
Else
SSHConect = False
End If
Catch ex As Exception
MessageBox.Show(ex.ToString())
SSHConect = False
End Try
End If
End Function
```
2 .- Just here, all run OK, I can connect to remote database and read data.
3 .- Now, I try to disconnect and delete the forwading rule:
```
If (Not IsNothing(client)) Then SSHDisconnect(client, port)
Donde SSHDisconnect :
Public Sub SSHDisconnect(ByRef _client As SshClient, ByRef _port As ForwardedPortLocal)
_port.Stop()
_client.Disconnect()
_client.RemoveForwardedPort(_port)
_port.Dispose()
_client.Dispose()
End Sub
```
Now, if I try to forwading to a new SSH server, seems like the socket is in an unknow state. The connection with the new rule return successful, but really open the forwading with the previous address, really I think the old rule is all time running, because if the old server SSH and the new rule are over the same address run properly.
Some idea?