简体   繁体   English

Android客户端未连接到服务器KryoNet

[英]Android client not connecting to server KryoNet

I am trying to connect from an Android client (from my phone) to a server (my PC). 我正在尝试从Android客户端(从手机)连接到服务器(我的PC)。 Both the server and the client are using KryoNet. 服务器和客户端都使用KryoNet。 The connection is succesful when I try to connect from a client which is on the same PC as the server. 当我尝试从与服务器位于同一台PC上的客户端进行连接时,连接成功。 The connection is also succesful when i try to connect from my Android phone connected to wireless (to the same router as the PC which runs the server). 当我尝试从连接至无线的Android手机(与运行服务器的PC所在的路由器)连接时,连接也成功。 The connection fails when i try to connect from the phone while using mobile data (it gives a timeout error). 当我在使用移动数据时尝试通过电话进行连接时,连接失败(它给出了超时错误)。 I did port forwarding and opened the port from Windows firewall setting. 我做了端口转发,并从Windows防火墙设置中打开了该端口。 Any idea what's wrong? 知道有什么问题吗?

Client code: 客户代码:

final Client client = new Client();     
        client.getKryo().register(StringRequest.class);
        final StringRequest request = new StringRequest();
        new Thread(client).start();

        System.out.println("Client started.");

        try {
            client.connect(5000, ipAddress, 54555);
            client.addListener(new Listener() {
                public void received(Connection connection, Object object) {
                    if (object instanceof StringRequest) {
                        StringRequest response = (StringRequest) object;
                        Gdx.app.log("Client",response.data);
                    }
                }
            });
        } catch (IOException e) {
            Gdx.app.log("Exception", e.getMessage());
            e.printStackTrace();
        }
        request.data = "Hello";
        client.sendTCP(request);

Server code: 服务器代码:

Server server = new Server();
    Kryo kryo = server.getKryo();
    kryo.register(StringRequest.class);
    server.start();
    try {
        server.bind(54555);
    } catch (IOException e) {
        e.printStackTrace();
    }
    server.addListener(new Listener() {
        public void received (Connection connection, Object object) {
           if (object instanceof StringRequest) {
              StringRequest request = (StringRequest)object;
              System.out.println("Server"+request.data);
              StringRequest response = new StringRequest();
              response.data = "response";
              connection.sendTCP(response);
           }
        }
     });

Does your Android application have the Internet permission? 您的Android应用程序是否具有Internet许可?

<uses-permission android:name="android.permission.INTERNET" /> 

It is needed to communicate over the network. 需要通过网络进行通信。 Also, make sure you are trying to connect with your PC's external IP, not local IP ( 192.168.x.xxx ). 另外,请确保尝试连接PC的外部IP,而不是本地IP( 192.168.x.xxx )。 You can get your external IP here . 您可以在此处获取外部IP。

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

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