11

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…

Display Name
  • 8,022
  • 3
  • 31
  • 66
  • 1
    Your code works fine here. Seeing as you tagged this with [tag:ubuntu-14.04], did you allow port 12345 (tcp) through your firewall? – Joakim Erdfelt Feb 01 '16 at 19:23
  • Yes, as I said in the question, I allowed it. (by running `ufw allow 12345`), also tried rebooting after that — nothing changed. Well, I mean, I opened the port before even trying. But disabling `ufw` altogether did not change anything. – Display Name Feb 01 '16 at 19:25
  • 1
    I'm using Fedora 23, once the server is running, I was able to use http://www.websocket.org/echo.html from a different machine to connect back to the websocket EchoSocketAdapter via `ws://myremote:12345/hello/` and test the echo behavior just fine. – Joakim Erdfelt Feb 01 '16 at 19:30
  • The "Host is down" message points to a more fundamental networking concern between these two machines. Can you even get a ping response from the destination server? Can you access any other services on the server machine from the test machine? – Joakim Erdfelt Feb 01 '16 at 19:38
  • @JoakimErdfelt yes, I am pretty sure that the problem is not with the code, too. It's probably some misconfiguration in the OS. What's bad is that I don't have any idea where to look. And I have other software running on that machine, without such problems. – Display Name Feb 01 '16 at 19:39
  • @JoakimErdfelt yes, I can ping that machine, and it has several running&accessible services, including nginx, ssh, openvpn, and another Java application which seems to use Jetty under the hood, too (but for HTTP, not websocket). – Display Name Feb 01 '16 at 19:40
  • From what client are you seeing the "Host is down" message? – Joakim Erdfelt Feb 01 '16 at 19:41
  • I tried https://pypi.python.org/pypi/websocket-client/ and also my own code using Jetty websocket client. Both approaches gave "Host is down" error. (OTOH, if I attempt to connect to another, obviously closed port, the error changes to "Connection refused", so there is some difference). And both ways work OK on local machine. – Display Name Feb 01 '16 at 19:45
  • 'Host is down' is not a Java error message. Is it one of yours? If so, where do you issue it, and under what circumstances? – user207421 Feb 04 '16 at 09:07
  • @EJP I get this error at client side, when trying to connect to the server remotely (from a different machine). However, other services on that server machine do not have this issue. The text "host is down" is not mine, it's one of standard network errors – Display Name Feb 04 '16 at 13:54
  • No it isn't. It must be coming from a library. The 'standard network error' for this could be several things, from 'connection refused' to 'host unreachable' to 'no route to host' etc., and that's why Inasked the question. Without that information it's going to be difficult to help. – user207421 Feb 05 '16 at 09:22
  • @EJP ok, got it. I will try digging deeper when I stop being sick :/ – Display Name Feb 05 '16 at 11:23
  • @EJP The underlying error is `EHOSTDOWN` – Display Name Feb 08 '16 at 19:49

1 Answers1

4

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

Display Name
  • 8,022
  • 3
  • 31
  • 66