Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty Websocket server is working locally, but remote connections fail with "Host is down" error, how to fix it?

I'm running a websocket server, using embedded Jetty.

It works as intended when I make connections from the same machine (localhost), but when I try to connect from a different machine, I get the error "Host is down" (also known as EHOSTDOWN).

Logs say that Jetty is listening on 0.0.0.0 address, so it should accept connections from everywhere, and the port (in this example, 12345) is allowed in ufw for all protocols. I also tried temporarily disabling ufw and that had no effect.

This is my code (this is a simple websocket echo-server, I've removed everything that's irrelevant):

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import java.io.IOException;

public class EchoServerLauncher {
    static final int PORT = 12345;

    public static void main(String[] args) {
        startServer(PORT);
    }
    private static void startServer(final int port) {
        new EchoServer(port).startAndJoin();
    }
}

class EchoServer extends WebsocketServerBase {
    static final String PATH = "/hello/";

    public EchoServer(final int port) {
        super(port);
    }
    void startAndJoin() {
        super.startAndJoinWithServlet(new EchoServlet(), PATH);
    }
}

class EchoServlet extends WebSocketServlet {
    @Override
    public void configure(final WebSocketServletFactory factory) {
        factory.setCreator((req, resp) -> new EchoSocketAdapter());
    }
}

class EchoSocketAdapter extends WebSocketAdapter {
    @Override
    public void onWebSocketText(final String message) {
        super.onWebSocketText(message);
        if (message == null) return;
        try {
            getSession().getRemote().sendString(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

class WebsocketServerBase {
    private final int port;

    public WebsocketServerBase(int port) {
        this.port = port;
    }
    void startAndJoinWithServlet(WebSocketServlet servlet, String path) {
        final Server server = new Server();
        final ServerConnector connector = new ServerConnector(server);
        connector.setPort(this.port);

        server.addConnector(connector);

        final ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        contextHandler.setContextPath("/");

        server.setHandler(contextHandler);

        final ServletHolder servletHolder = new ServletHolder(servlet.getClass().getSimpleName(), servlet);
        contextHandler.addServlet(servletHolder, path);

        try {
            server.start();
            server.dump(System.err);
            server.join();
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

So, what could cause such issue? I don't even know anything else to try…

like image 909
Display Name Avatar asked Feb 01 '16 19:02

Display Name


1 Answers

It was my stupidity, I forgot to allow that port on my client's router. (Too bad I cannot delete close the question).

like image 159
Display Name Avatar answered Oct 31 '22 19:10

Display Name