0

I am trying to build a system in java, where one computer acts as the server and the other one as the client and connect them together (i.e send data b/w them) on the public network. Here is my pseudo code -

Server.java

ServerSocket server = new ServerSocket(55955);
Socket socket = server.accept();

Client.java

 Socket socket = new Socket("<ip address of server>", 55955);
//code to send and receive response from server

The code works fine if I have my server and client running on my computer.But if i run the server on a different machine (a friend's pc on the internet) and try to use that ip on the client running my machine, its not working. I did a lot of googling and did not find a appropriate solution other than opening up the firewall or ports on the machine.

  1. Is there any other option to connect b/w the computers on the internet without any forwarding or opening the ports ?
  2. There are a few peer to peer systems (eg: skype)etc that does the same thing I am trying to achieve (and in my best of my knowledge its without opening any ports on the host machine). Please correct me if I am wrong.

Thank you.

Mukesh Keshu
  • 467
  • 2
  • 13
  • 1
    1. Connection without forwarding/opening could be possible if this specific machine is exposed to the Internet without any protection by firewall or NAT - and this is almost improbable nowadays. 2. Skype uses TCP port 443 which is just open by default. – Nowhere Man Jun 28 '20 at 09:55
  • @AlexRudenko Thank you. Sorry I am newbie on networking. One question that arise to my mind is that if it listens on tcp port 443, how can it differentiate b/w other requests. For eg: If I have another application such as Zoom, which also listens on port 443. How can we differentiate ? – Mukesh Keshu Jun 28 '20 at 10:10
  • 1
    these links https://stackoverflow.com/questions/1694144/can-two-applications-listen-to-the-same-port https://serverfault.com/questions/653513/how-to-handle-multiple-apps-via-port-443 provide some details how this can be done. – Nowhere Man Jun 28 '20 at 10:26
  • Thank you. TCP or UDP hole punching will help me. – Mukesh Keshu Jun 28 '20 at 13:07

1 Answers1

2

Thank you. I am adding the solution which I was finally able to work with.

This can be achieved using NAT Hole punching. Found these awesome articles which explains the approach.

  1. Peer-to-Peer Communication Across Network Address Translators
  2. How Skype gets around firewalls
Mukesh Keshu
  • 467
  • 2
  • 13