This project is read-only.

Port Forwarding and Java application launch

Jul 31, 2014 at 8:42 PM
Hello all, first of all thank you for this great community, I am learning a lot as I go. I am by no means a programmer by trade, I write code with a means to and end approach, and so any solution I came to is usually far from elegant. I also do not know much about the various ways to debug but I will try to pull together as much information for those willing to help as I can. Also, I apologize for any incorrect use of jargon.

The problem I am having is with, as the title suggests, Port Forwarding and launching a Java application. I am able to successfully create an ssh tunnel to my server with the following code.
                        pubfile = new PrivateKeyFile("C:\\System Apps\\" + _SSHkeyfile, _SSHpassword);
                        sshclient = new SshClient(_HostURI, _SSHPort, _SSHusername, pubfile);
                        sshclient.Connect();
                        forport1 = new ForwardedPortLocal("127.0.0.1", 65088, "127.0.0.1", 8088);
                        forport2 = new ForwardedPortLocal("localhost", 65088, "localhost", 8088);
                        sshclient.AddForwardedPort(forport1);
                        sshclient.AddForwardedPort(forport2);
                        System.Threading.Thread.Sleep(1000 * 1);
                        forport1.Exception +=new EventHandler<ExceptionEventArgs>(forport1_Exception);
                        forport1.Start();
                        forport2.Start();
I know it's working because the server hosts a webpage at 127.0.0.1:65088, and I am able to load firefox and navigate to said page and click through any number of links that reside on the page. However whenever I try to launch a Java application, the application is able to download to my local computer but when the application runs I receive the following error.

System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle)
at Renci.SshNet.Channels.Channel.Close(Boolean wait)
at Renci.SshNet.Channels.ChannelDirectTcpip.Close()
at Renci.SshNet.ForwardedPortLocal.<>c__DisplayClass3.<InternalStart>b__2()

from the ForwardedPortLocal ExceptionEventArgs and this error

System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at Renci.SshNet.Channels.ChannelDirectTcpip.InternalSocketSend(Byte[] data)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Renci.SshNet.Session.MessageListener()

from the SshClient ExceptionEventArgs.

What's interesting is that I was able to load the java application 1 time, and every time since then I get a "Connection Refused" as reported by the Java application.

I am able to use Putty to connect and load the Java application with no problems.

Any and all help is much appreciated, thanks!
Aug 1, 2014 at 7:03 PM
I've gotten this to work with the older SharpSSH library. I also used TCPView from Microsoft to watch what happens when the connection are created. It seems that when the java application is launched it begins by opening up many connections which are all routed through the 65088 port originating from various remote ports. These all seem to Open, Establish and Close_Wait very quickly. Sometimes a Close_Wait port will open up again and begin transmitting data but most times the ports remain closed, while only two will remain Established and one will be transmitting data.

I'm not sure if any of this makes sense, I could take a screenshot and show you whats happening in TCPView if anyone would like to investigate this. This could also be a problem with the java application that I open?

If I had to guess, it would appear that ssh.net has a problem handling the many opening and closing requests sent rapid fire over the one forwarded port.