java - Websocket 服务器连接被拒绝

标签 java android websocket server connection-refused

我想制作一款Android小游戏,其中一个人是主机(websocket服务器),同一个人和其他人是客户端(websocket客户端)。

我正在使用TooTallNate's Java-WebSocket library对于客户端和服务器。

我有两部手机,一部带有 websocket 服务器,使用下一个 uri:“ws://localhost:port”创建,另一部带有连接到“ws://my_public_ip:port”的 websocket 客户端

我尝试了在互联网上找到的多个端口以及 WIFI 和 3G、本地主机或我的公共(public) IP 的多种组合,但最终都以连接被拒绝或超时告终。

我知道客户端正在工作,因为我能够连接到 echo websocket 服务器。

这至少应该可以通过 WIFI 工作,通过 3G 也可以。

我应该在服务器端和客户端使用什么IP和端口?

我需要打开任何端口吗?

下面的代码是该库的服务器和客户端实现,与我的项目的唯一区别是,我在与主线程不同的线程中调用 Android Activity onCreate 方法中的相同行,而不是 java 的 main 方法线程。

库的服务器端实现:

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();
    }
}

库的客户端实现:

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();
    }
}

编辑2019年8月25日

我安装了Port Scanner在 Android 设备上,可以检查端口是否打开,这些是我遵循的步骤,全部通过 WIFI 连接:

  1. 安装端口扫描器
  2. 检查 127.0.0.1 (localhost) 和端口 8080,结果:0 个端口开放
  3. 使用 Websocket 服务器运行应用程序并启动它。
  4. 检查 127.0.0.1 (localhost) 和端口 8080,结果:端口 8080 开放:备用 HTTP(我不知道你必须启动服务器才能打开端口,我认为路由器的端口转发配置应该足够了,我错了)
  5. 检查192.168.1.X(同一设备(即与服务器相连的设备)的私有(private)静态本地 IP)和端口8080。结果:打开 0 个端口

我不明白为什么端口对本地主机开放,而不是通过私有(private)IP。 我需要将其对私有(private)和公共(public) IP 开放,我缺少什么?

最佳答案

ISP阻止端口的人。一旦我尝试了一个随机端口,它就工作了。

在这种情况下可能有用的步骤:

  1. 在服务器设备上使用WIFI
  2. 在服务器设备中使用静态 IP,而不是 DHCP 提供的 IP。
  3. 转至路由器的端口转发部分打开服务器将运行的端口。您需要设置您的服务器设备私有(private)IP。
  4. 运行服务器(重要且不可错过的步骤)。
  5. 使用任何工具/网络或应用检查您的端口是否已使用公共(public) IP 打开
  6. 如果这些都不起作用,请尝试禁用您的操作系统路由器的防火墙,一旦您得到答案,请再次启用它们(如果它是防火墙之一)阻止了您的端口,在防火墙中为该端口创建规则
  7. 如果这些方法都不起作用,您的 ISP 可能会阻止它们,请在 Google 中搜索有关您的 ISP 阻止端口的信息。

关于java - Websocket 服务器连接被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639911/

相关文章:

java - 我的 JAR 文件太大,无法上传到 AWS lambda

java - 多线程 Zip 文件读取器

java - 从 SQLite 数据库删除数据时,应用程序需要重新启动才能实际删除

websocket - 用于 websocket 应用程序的 nginx-ingress 粘性 session

javascript - Phonegap 应用程序 - Pusher 和 PubNub 的替代品

java - 从 Firebase Firestore 将文档的一部分获取到对象中

Java 2d 游戏不使用平铺 map

android - ConstraintLayout的优缺点?

android - utf-8 的问题

python - Tornado Websockets 在没有 ping 的情况下不会调用 on_message