0

I am new to java networking. I tried to make a server localy (the servet and client on the same machine) and it works with simple chat server. And what iam trying to do is to make it online so i can make a connection between the server hosted on my computer and other computers.

I tried to search on google but whenever i type java server i got things about minecraft -_- java servers! Thanks if you can help.

I have the following setUpNetworking method in my Client class:

private void setUpNetworking(){
try{
InetSocketAddress serverAddress=new InetSocketAddress("127.0.0.1",5000);
SocketChannel socketchannel=SocketChannel.open(serverAddress);
/*
setting up reader and writer .... etc
*/
System.out.println("Networking established");
}catch (IOException ex){
ex.printStackTrace();
}
}

What i should i put instead of "127.0.0.1" i tried my computer ip address and it didn't prints 'Networking established' and never terminated, same thing happens when i host the server on other computer or on my phone. Can any one help me i want the connection to go through the internet and get back to my computer.

Issasafar
  • 13
  • 2
  • 3
    Oh ... you're opening a whole can of worms. The short answer is "your public IP address or a DNS name pointing to it", but thanks to wide use of NAT (both at your local network and even at the ISP level) you may or may not have one or it might only point to your router and not to your PC. Hosting publicly accessible ports from a local computer is non-trivial (and also somewhat risky). – Joachim Sauer Mar 16 '23 at 09:51
  • I've not found a good question with answers that explain it all in details but [this one](https://stackoverflow.com/questions/5108483/access-localhost-from-the-internet), [this one](https://stackoverflow.com/questions/45739486/open-8080-port-on-my-local-server-to-public) and [this one](https://stackoverflow.com/questions/52137672/how-do-i-make-my-ip-public-externally-can-access-my-localhost) might give you some pointers. – Joachim Sauer Mar 16 '23 at 09:55
  • 2
    @life888888: I don't think the code is actually OPs problem, it's more about the networking topology. – Joachim Sauer Mar 16 '23 at 10:17

2 Answers2

1

The machine you want to run the server on needs to actually be accessible from the outside, which means it has to have a public IP address and other machines need to be able to access the application running on your machine.

Being accessable from the outside also includes that other clients need to be able to establish a connection, not just responding to already existing connections that go out from your own machine. This becomes a problem in case your home PC sits behind a Carrier-Grade NAT or there is a firewall in between you and the internet you have no control over.

If there is no NAT or anything else blocking incoming connections you have no control over, what you want is certainly possible, and there are a few steps to achieving this:

  • Make sure the machine you want to run the server on has a public IP.
  • Make sure that the port you want to run your server on isn't blocked by a firewall or anything.
  • Make sure that no other service on your machine is already running on the same port you want to run your service on.

Now, even if it is possible opening ports on your home network is always at least a small security risk because you allow potential malicious actors to make connections back to your network. If you do this for testing purposes it is fine, but please remember to close the port again (in your router settings) when you are done.

th0bse
  • 97
  • 8
0

First steps:

Is the ip the one from this page?

Test if the port is accessible by doing a telnet to the ip and port 5000 from another computer off your network. If the telnet page opens up your server is reachable.

John Williams
  • 4,252
  • 2
  • 9
  • 18