tags:

views:

685

answers:

2

I have had no trouble getting the sockets working with localips, but once I made the code change to use public ips, I've consistently gotten java.net.ConnectException.

I am using the port 8084, which as far as I know, is not used elsewhere In the command prompt, netstat -a | grep 8084 shows: File STDIN: TCP user-9114eb19a8:8084 user-9114eb19a8:0 LISTENING

I have gone into my router and ensured that it is open I get my public ip using a request to http://www.whatismyip.org

Server:

serverSocket = new ServerSocket (8084);

Client:

socket = new Socket (hostaddr, 8084); //hostaddr is a string containing my public-IP
//it works when the program is run on a localnetwork and I am using my local-ips
+7  A: 

What type of router? Don't forget that even though a port may be open on your router, you need to have your router Forward port 8084 to the destination computer's internal IP, otherwise because of Network Address Translation (NAT) the router/firewall doesn't know what to do with the traffic once it gets there.

For example say your Client PC is in California with the IP 10.1.1.100, with a router using public IP of 70.62.50.42.

Your Server PC is in New York with an IP 192.168.1.121, that is behind a router with internal IP 192.168.1.1 and public IP 40.20.26.63. You will need to make sure that you go into the router and forward port 8084 (TCP) to internal address 192.168.1.121 (the internal client PC).

Depending on the level of your router this terminology will be Port Forwarding, Virtual IPs, or Static NAT Translation (there are a few others but you get the idea).

You won't have to adjust any settings on the Client router, because that router already knows how to route your request out to the internet.

routeNpingme
A: 

I asked about the netstat just to see if perhaps you were listening on 127.0.0.1, but I'm not familiar with what your OS is showing.

Assuming that you're not accidentally still listening on localhost: With routers, you generally have to forward to a specific address. At least, with the routers I've had, that's what I had to do. assuming you're not listening localhost, then the router needs to forward to an IP that ifconfig shows.

Don Branson