Networking – Socket Connection to External IP Through Server

networkingnetworkssocketswebsockets

I am working on an application using sockets. Essentially I want to stream data between two clients. Since I can't connect the two clients together directly (they aren't on the same network), I will need to connect the two through a server.

enter image description here

My client-side is written in Java but the server-side could be written in any language. I have already explored using socket.io etc. but the issue I ran in to is that Java doesn't have a nice websocket solution. What other options are there out there to achieve the same effect?

The solution I have in mind right now is basically "forwarding" the stream from one client to the other through the server, but there could be a better way out there…

Best Answer

There are a few ways you could solve this problem.I understand from your diagram and question your one of your client say client 1 is the server and your server is actually a proxy to redirect connections.The following solutions are in order of their complexity.

First is a networking solution: If connectivity between client 1 and client 2 is all you need to achieve. All you need to do in your clients is to add a route to the other client,add a route to client 1 to reach client 2 and vice versa.you do not have to change design of your program.

Second is a web proxy server:If you are running a web server in your server then you can configure web server to act as a reverse proxy.(Apache proxy instructions).This is useful if you do not have a lot of control over networking but have administrative privilege to your servers

Third is a programming solution:I guess you have the right solution in mind to write a program to "forward" the packets to the client.This is called proxy server so what proxy will do is receive requests from the client forward it to the server and receives the response from the server and forwards it to the client.But this does not guarantee performance you might have some performance issue.I would choose this way only when I have privileges that will allow me to change only the application I wrote and have restricted access to networking or servers and I will absolutely avoid going this road If at all possible for production systems.

Your statement about Java not having nice web socket is not true, Java's socket API is probably one of the most useful sockets out there.

Related Topic