Use local pc as a server

1.9k Views Asked by At

I just made a little chat programm with a server and a client in java. The client needs to connect to the server with the ip of the server. All my testing I did with the localhost (127.0.0.1) and everything went fine, but when I tested it later on with a friend of mine, I had to notice that if I put my ip address (I run the server, he runs the client) that it doesn't work. Is there a way to set up a private little server on my pc to run my server on, or maybe another way to get it working?

EDIT:

Just found a tool called "Apache" to set up your own server, could that might be helpful?

EDIT: When I say it doesnt work I mean I get an IOException, because this fails:

public void connectToServer() throws IOException{

    showMessage("Attemption to connect...");
    connection = new Socket(InetAddress.getByName(serverIP), 6789);
    showMessage("\nConnected to: " + connection.getInetAddress().getHostName());
}
2

There are 2 best solutions below

1
On

There is a whole host of things that you need to look at before your application will work.

  • Firewalls on both ends (and anti-virus applications) need to allow the programs to communicate
  • Your ISP needs to allow messages to be sent via your designated ports
  • Your router (and the clients) need to not-filter these messages.

As a start, see if you can ping each others IP addresses and take it from there.

2
On

I recommend you first try and disable your firewall. If you are using windows, here is the instruction for turning off windows firewall: Turn Windows Firewall on or off

If you are testing with someone outside of your local network, you may need to setup NAT on the router of the person hosting the server. You can access the router by typing in it's local IP address in the web browser. This is usually something like 192.168.1.1 or 192.168.1.254 but it will depend on the model and network setup.

Once you have connected you should find an option (usually under advanced) for "NAT" or "Port Forwarding". I suggest you do a google search with the router model and how to setup port forwarding.

You also need to be aware that some ISPs will block certain ports. I suggest testing on a common port such as port 80 (HTTP) since it's unlikely an ISP will block this (be aware that you will need to disable Skype or any local web servers to test this)