繁体   English   中英

Websocket服务器连接被拒绝

[英]Websocket server connection refused

我想制作一个小小的Android游戏,其中一个人是主机(websocket服务器),而另一个人是客户端(websocket客户端)。

我将TooTallNate的Java-WebSocket库用于客户端和服务器。

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

我尝试了在互联网上找到的几个端口,以及与WIFI和3G以及localhost或我的公共IP的多种组合,所有这些都以连接被拒绝或超时而告终。

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

这至少应通过WIFI起作用,并且通过3G也会很好。

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

我需要打开任何端口吗?

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

库的服务器端实现:

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

编辑25/08/2019

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

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

我不明白为什么端口是为本地主机而不是通过私有IP打开的。 我需要向私有和公共IP开放, 我想念什么?

ISP阻塞端口的那个 一旦尝试了随机端口,它就会起作用。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM