[Fixed] Need Help with Server/Client Connections (Connecting over Internet)

Hello! I’ve recently been looking into clients and the like, and was trying to test it out with one of my friends over the internet (Because it tested fine over localhost). The weird thing is that crashes whenever he goes to enter the IP to connect to (Mine). It listens on port 7777, and here’s the code. I have the server and client separate, so they have different github URLs. Here they are:

Server: https://github.com/RyanMB97/ServerStuff/
Client: https://github.com/RyanMB97/ClientStuff

According to the error message my friend sent me, it was somehow related to line 21 in Client, where I create the Socket. Any and all help is appreciated, and thanked in advance!

-Ryan Mitrun

P.S. Close-Up of problem line(s):

System.out.print("Enter the IP of the server you are connecting to: "); // Ask for the IP
String socketInput = input.nextLine(); // Set the socket IP to whatever they set

System.out.println("Connecting..."); // Connecting to server
socket = new Socket(socketInput, 7777); // Create a socket, connected to the server

P.P.S. The issue was port-forwarding. I just adjusted my router accordingly. Thanks for the help and suggestions anyways!


do you have a stack trace of the error or something similar ?

If your code works in local but not over internet, there are good chances that the guilty is your network configuration. Are you launching your server behind a router ? If yes have you forwarded the ports you’re listening to ?

I haven’t tried forwarding the port, but I’ll definitely try that next. Already tested via normal internet connections, and Hamachi (Internet-based LAN), so that might be the issue. Here’s the stack-trace:

Again, the problem line is 21. I have no idea what the rest means other that that it has something to do with the socket (Obviously). I’ll try forwarding the port and see if that works. In the mean-time, any other suggestions are appreciated!

-Ryan Mitrun

Modification 1: Not sure what you mean about hosting behind a router, but I’m just running it as an Executable Jar via a Batch file.

Based on this I’d be willing to bet it’s a firewall problem, so definitely try forwarding the port. I’ve had similar problems when writing networking code. Works great over LAN but as soon as I send it to someone to test, one of our firewalls or routers blocks the port and it won’t work.

ok from your answer I’m 99% sure it’s a routing issue :slight_smile:

Basically if you have an internet box and you are able to connect several network devices on it without issues, then your internet box certainly acts as a router. You should check wikipedia or google to learn a bit about it.

In a few words, in the case of a home internet connection, the router is there to dispatch packets to the appropriate computer/device connected to it. From the outside of your home you have only one visible IP address: the one of your internet box. If from your computer you connect to a website, when the website returns packets to you, it’s the router’s job to forward these packets back to your computer and not to the other devices ( mobile phone, tablet … ) that is also connected to internet.
When the request is initiated from you the router is able to identify the packets coming back and forward them to you … but when the request is initiated from outside your home, the router has no way to know if the packet should go to your computer, your mobile or your tablet.
That’s why you need to forward ports: you configure the router for it to know that if it gets a connection on port 7777, then it should forward the packet to your computer ( identified by your IP address ). It’s that simple.

Also to do this you have to be sure that your computer has always the same IP address in your local home network. If you get an IP address automatically ( via DHCP ), there’s a good chance that your internet box acts also as a DHCP server. In this case you should configure it as well to always return the same IP address to you ( for your MAC address ).

Hope this helps :wink:

I fixed it. Turned out that it was indeed a routing issue. I had my same friend I was trying to connect to fix it. Also, as you suggested deathpat, I also got a static IP while I was at it. Thanks for the help and everything! Much appreciated.

Very Much Thankful,
-Ryan Mitrun

P.S. I’ll certainly look into it, death. Thanks for the info!