0

I am playing with the code https://cs.lmu.edu/~ray/notes/javanetexamples/#chat at the moment. In the line

var client = new ChatClient(args[0]);

of the clients' main method you pass a host name to the constructor.

I am still getting a "ConnectException: connection refused" trying to connect to my server as a client over the internet (with my public IP), whereas I can connect to it locally (with "localhost"). I have checked the firewall, all ports and the server itself, but still getting it. The ping to my IP works, but the connection is being refused.

Is it possible for me to connect as a client over the internet from my laptop to the server running at my laptop?

The trace:

Exception in thread "main" java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:503)
    at java.base/sun.nio.ch.Net.connect(Net.java:492)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
    at java.base/java.net.Socket.connect(Socket.java:648)
    at java.base/java.net.Socket.connect(Socket.java:597)
    at java.base/java.net.Socket.<init>(Socket.java:520)
    at java.base/java.net.Socket.<init>(Socket.java:294)
    at ChatClient.run(ChatClient.java:71)
    at ChatClient.main(ChatClient.java:97)

So, calling client.run() the exception occurs when trying to open a socket at the passed IP address at

var socket = new Socket(serverAddress, 50000);

and I use the port 50000 for both.

jupiter_jazz
  • 333
  • 2
  • 8
  • 1
    You are not giving to much information. The error is telling you that no one is listening at the target IP/Port. Can you provide a trace so we can compare with the code you pointed to? Also see this answer https://stackoverflow.com/a/6876306/6708879 – MissingSemiColon Jul 17 '20 at 09:43
  • I am not sure about the workflow: maybe the server starts automatically at "127.0.0.1" so that trying to reach it by the public IP has no success. But that is the reason of the question - I try to figure out a problem :) And yes, I have looked through the post you linked. It did not help, so I've posted this question on a concrete example. – jupiter_jazz Jul 17 '20 at 10:08
  • When you don't give it an ip it will listen to all interfaces, so that should not be a problem here (you can give it the ip 0.0.0.0 to listen to all interfaces). Can you connect using the local ip? (127.0.0.1 or your network one). Have you added your client and server to the firewall? This could be blocking the request. Normally if you can ping a machine but then other software is not working, check the firewall always. For a quick test just turn it off, if it starts working add the programs to it. – MissingSemiColon Jul 17 '20 at 10:27

1 Answers1

0

It is extremely unlikely that you would be able to connect directly to your computer from the internet without extra work.

Minimally you would need to do port forwarding in your router to redirect all connections to port 59001 to your machine.

In suggest using something like ngrok for this instead:

ngrok http 59001

and now you should be able to connect to your service

var socket = new Socket("http://CUSTOM_URL_PROVIDED_BY_NGROK.ngrok.io", 59001);
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93