views:

102

answers:

3

I'm building a simple server application, and I can connect to it in the Local Network. But I can't connect over the internet.

This is my Server code:

ServerSocket server;
try {
    server = new ServerSocket(4000);
} catch(IOException ex) {
    System.out.printf("Could not bind socket 4000\n");
    System.exit(1);
}

try {
    Socket socket = server.accept();
    ClientThread client = new ClientThread(socket);
    client.start();
} catch(IOException ex) {
}

And this is the client:

try {
    System.out.printf("connecting...\n");
    Socket socket = new Socket("mydomain.org", 4000);
    System.out.printf("connected!\n");
} catch(UnknownHostException ex) {
    ex.printStackTrace();
} catch(IOException ex) {
    ex.printStackTrace();
}

I've forwarded port 4000 on my router, which should work. I've forwarded other ports before like 80 and 22.

When I run the client, I get the "connecting..." string, and it hangs there. I don't get "connected!", or a stack trace. But like I said before, it does work on the local network. It works when connecting to 127.0.0.1 and when using 192.168.1.90.

I used CanYouSeeMe.org to check if the port was open. It was successful on port 80, but it times out on 4000.

A: 

You've done the experiments to prove that it is not your java and must therefore be a firewall or firewalls issue. Even if you are properly forwarding the ports to your server, the server firewall may not be allowing inbound connections, so check that also.

GregS
I checked my Firewall and it's allowing in all protocols except NetBios. I don't know what protocol Java Sockets use, but "All other protocols" is checked.
jonescb
+1  A: 

Check which ip address port 4000 is bound to. It may only be bound to the loopback address (127.0.0.1) instead of any interface.

Not sure which os you're using, but to check: linux: netstat -ant windows and mac: netstat -anp tcp

look for the LISTEN line on port 4000 and see whether it's bound to all interfaces (*:4000 or 0.0.0.0:4000), or to a specific interface (127.0.0.1:4000).

If it's not listening on all interfaces, that's your issue - use the constructor that lets you specify the BindAddress.

Matt
I forgot to mention that I am using Linux; Fedora 12. Which just now reminded me that I'm probably getting owned by iptables. I'll disable that for now and check back in.
jonescb
Try one thing at a time... try connecting from another machine in your network before going through the firewall. That way you can eliminate iptables and bindaddress issues, then deal with your router nat and firewall rules.
Matt
Yep, it was iptables, I just disabled it for a while. It's really easy to forget that Fedora has iptables there and blocking _everything_.I also had to use another constructor to bind to the local IP address (192.168.1.90). So I'll mark you as the solution for that and inadvertanly reminding me about iptables.
jonescb
A: 

You have a firewall rule that's throwing away the incoming packets. That's why your client hangs. Check the IP rules on the server computer and on any gateway system to the outside world.

Steve Emmerson