Gilgamesh
Gilgamesh

Reputation: 337

Websocket server connection refused

I want to make a little Android game in which one person is the host (the websocket server) and the same person and others are the clients (websocket client).

I'm using TooTallNate's Java-WebSocket library for the client and server.

I have two mobile phones, one with the websocket server, created with the next uri: "ws://localhost:port", and the other one with the websocket client connecting to "ws://my_public_ip:port"

I've tryied several ports I found on the internet and multiple combinations with WIFI and 3G, and localhost or my public IP, and all ended up in connection refused or timeouts.

I know the client side is working, because I was able to connect to an echo websocket server.

This should work at least through WIFI and it would be nice through 3G too.

What IP and ports should I use in the server and client side?

Do I need to open any ports?

The code below is the server and client implementation of the library, the only difference with my project is that instead of the java's main method i'm calling the same lines in an Android's activity onCreate method in a thread different from the main thread.

Library's server side implementation:

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

public class SimpleServer extends WebSocketServer {

    public SimpleServer(InetSocketAddress address) {
        super(address);
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        conn.send("Welcome to the server!"); //This method sends a message to the new client
        broadcast( "new connection: " + handshake.getResourceDescriptor() ); //This method sends a message to all clients connected
        System.out.println("new connection to " + conn.getRemoteSocketAddress());
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        System.out.println("closed " + conn.getRemoteSocketAddress() + " with exit code " + code + " additional info: " + reason);
    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        System.out.println("received message from " + conn.getRemoteSocketAddress() + ": " + message);
    }

    @Override
    public void onMessage( WebSocket conn, ByteBuffer message ) {
        System.out.println("received ByteBuffer from "  + conn.getRemoteSocketAddress());
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        System.err.println("an error occured on connection " + conn.getRemoteSocketAddress()  + ":" + ex);
    }

    @Override
    public void onStart() {
        System.out.println("server started successfully");
    }


    public static void main(String[] args) {
        String host = "localhost";
        int port = 8887;

        WebSocketServer server = new SimpleServer(new InetSocketAddress(host, port));
        server.run();
    }
}

Library's client side implementation:

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

public class EmptyClient extends WebSocketClient {

    public EmptyClient(URI serverUri, Draft draft) {
        super(serverUri, draft);
    }

    public EmptyClient(URI serverURI) {
        super(serverURI);
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        send("Hello, it is me. Mario :)");
        System.out.println("new connection opened");
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        System.out.println("closed with exit code " + code + " additional info: " + reason);
    }

    @Override
    public void onMessage(String message) {
        System.out.println("received message: " + message);
    }

    @Override
    public void onMessage(ByteBuffer message) {
        System.out.println("received ByteBuffer");
    }

    @Override
    public void onError(Exception ex) {
        System.err.println("an error occurred:" + ex);
    }

    public static void main(String[] args) throws URISyntaxException {
        WebSocketClient client = new EmptyClient(new URI("ws://localhost:8887"));
        client.connect();
    }
}

Edit 25/08/2019

I installed Port Scanner on the android device and could check if ports were open, these are the steps I followed, all connected through WIFI:

  1. Install Port Scanner
  2. Check 127.0.0.1 (localhost) and port 8080, result: 0 ports open
  3. Run the application with the websocket server and start it.
  4. Check 127.0.0.1 (localhost) and port 8080, result: Port 8080 OPEN: Alternate HTTP (I didn't know that you had to start the server for the port to be open, I thought that the router's port forwarding configuration should be enough, I was wrong)
  5. Check 192.168.1.X (my private static local IP for the same device, the one with the server) and port 8080. Result: 0 ports open.

I don't understand why the port is open for localhost and not through the private IP. I need this to be open for the private and public IPs, What am I missing?

Upvotes: 0

Views: 11453

Answers (1)

Gilgamesh
Gilgamesh

Reputation: 337

The ISP was the one blocking the ports. Once I tried a random port it worked.

Steps that may be usefull in this kind of situation:

  1. Use WIFI on de server's device.
  2. Use a static IP in your server device instead of the one provided by DHCP.
  3. Go to the router's port forwarding section to open the port where your server will be running. You need to put your server device private IP.
  4. Run the server (important missable step).
  5. Use any tool/web or app to check if your port is open using your public IP.
  6. In case none of that works try disabling your O.S.'s and Router's firewall, once you have the answer enable them again, and if it was one of the firewalls that blocked your ports, create a rule for that port in the firewall.
  7. If none of that worked, your ISP may be blocking them, search in google info about your ISP blocking ports.

Upvotes: 1

Related Questions