undertow webserver not binding to remote address
Asked Answered
L

1

6

I'm testing out the undertow 2.0.0.Alpha1 webserver. When I run it locally it works and returns Hello World when I go to localhost:80. I then deploy the webserver on a remote server and go to remote_ip:80 but I get no response back. If I run curl -i -X GET http://localhost:80 on the remote server then I get Hello World back as well. So the server is definitely running but for some reason it's just not accessible via the remote ip address. If I try to set a hostname as the remote IP in the code (i.e. .addHttpListener(80, "remote.ip")) then I get a BindException.

import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloWorldServer {

    public static void main(final String[] args) {
        try {
            Runtime.getRuntime().exec("sudo fuser -k 80/tcp");
        } catch (IOException ex) {
            Logger.getLogger(HelloWorldServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        Undertow server = Undertow.builder()
                .addHttpListener(80, null)
                .setHandler(new HttpHandler() {
                    @Override
                    public void handleRequest(final HttpServerExchange exchange) throws Exception {
                        exchange.getResponseSender().send("Hello World");
                    }
                }).build();
        server.start();
    }

}

Any clues?

Lustreware answered 26/6, 2016 at 1:33 Comment(4)
Clue #1: use "netstat -a" (or equivalent) to check what IP and port the server is listening on.Earmuff
tcp6 0 0 127.0.0.1:80 :::* LISTEN 2939/java Lustreware
So are you using curl on IPv6 using that IP address? (It is "localhost" ...)Earmuff
No I think localhost is just linked to 127.0.0.1 by default on UbuntuLustreware
F
7

The second argument on addHttpListener(80, null) is the host. You need to put a host name or IP in there to have it listen on the public IP. Using null it will only bind to localhost.

Try binding to the public IP or binding to 0.0.0.0 if you want to bind to all addresses.

Undertow server = Undertow.builder()
        .addHttpListener(80, "0.0.0.0")
        .setHandler(new HttpHandler() {
            @Override
            public void handleRequest(final HttpServerExchange exchange) throws Exception {
                exchange.getResponseSender().send("Hello World");
            }
        }).build();
server.start();
Flair answered 27/6, 2016 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.