1

I wrote a server and client Java TCP Socket, It works on private network(192.168.2.x) but how I have no idea how make it work with public ip. I think I should open a port in the router and use my server ip and this port to init client socket! I tried it and did not work.

Init of sockets here:

Socket clientSocket = new Socket(IP, PORT);

ServerSocket server = new ServerSocket(PORT);

harunurhan
  • 1,516
  • 1
  • 17
  • 21

2 Answers2

0
  • "Opening ports" is usually done on the firewall - and it should be done, if you are using one.
  • In addition, you need your router to allow incoming traffic, this is usually done by port forwarding configuration / NAT section.
  • Last but not least, you need the client on the external network to specify the external facing IP of your router, which is an IP address that is not in the ranges reserved for private networks. 192.168.x.x is a range reserved for private networks and is not part of the public address space. it should be a different IP address.

You can find your router's public IP address by navigating http://www.whatismyip.com/

Lior Bar-On
  • 10,784
  • 5
  • 34
  • 46
  • - Sorry I translated it from my language, that's why I used "Opening ports", I think creating a socket with specified port, already open the port but I am not sure anyway. - I did port forwarding but I don't know how to test it ? Can you suggest me a way of testing! - I used my public ip and it did not work as I stated above. – harunurhan Aug 22 '14 at 21:37
-1

You should go to NAT section in your router and enable port forwarding, for example public ip:port will be 1.1.1.1:1 NATed to local 2.2.2.2:2, so you start your server on port 2 and connect with your client to 1.1.1.1 port 1.