I originally posted this question on stackoveflow but was advised that it would be more suited to this site.
I am trying to access a Jetty Server deployed on one machine from another machine outside LAN but it's not working. I've read this thread and followed the advise offered by @Joakim Erdfelt however it did not work. My code is below:
Server server = new Server();
// HTTP connector
ServerConnector serverConnector = new ServerConnector(server, 1, 1);
serverConnector.setHost("0.0.0.0"); <-
serverConnector.setPort(8080);
serverConnector.setIdleTimeout(30000);
// Set the connector
server.addConnector(serverConnector);
I tried accessing the server from another remote server with the following code:
String postUrl = "http://" + myIP + ":8080"; // myIP is set to the public IP address of Jetty Server
System.out.println("Post URL: " + postUrl);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(120 * 1000).build();
try (CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();) {
HttpPost httpPostRequest = new HttpPost(postUrl);
...
However, this throws the following exception:
HttpPostConnectException: Connect to 123.4.56.78:8080.. failed: Connection refused: connect
What is causing this error? I allowed Firewall access when prompted so I don't think it is a Firewall issue. The port 8080
is also not being used for any other process. I tried multiple ports to no avail.
Thanks
Best Answer
I'll move some of my recommendations to an answer. You are missing basic networking knowledge, please try to read up a little bit on it (here for example). Some basics:
Now it should be clear, why you have several IPs: Google is only able to see your public IP. In order for your jetty server to be reachable from the internet, you need port forwarding. Basically, you will be creating a rule inside your home router "forward every TCP packet you receive on port 8080 to the internal device XY". You'll need to check your router manual on how to do this, most routers have a web interface for configuration. I hope this clears up your confusion...