简体   繁体   English

Kryonet客户端连接问题Java

[英]Kryonet Client connection issues Java

I have a kryonet client/server that work find.. well mostly. 我有一个工作的kryonet客户/服务器。 The client remains idle and eventually disconnects after awhile but thats not the issue i'm trying to solve currently. 客户端保持空闲状态,并在一段时间后最终断开连接,但这不是我当前要解决的问题。 Currently, the server and client can establish a connection and send data back and forth(Before the client times out) as long as the client and server are on the same computer. 当前,只要客户端和服务器在同一台计算机上,服务器和客户端就可以建立连接并来回发送数据(在客户端超时之前)。 If you try to connect to a different computer on the LAN the connection times out and fails. 如果您尝试连接到LAN上的另一台计算机,则连接超时并失败。

So here's my question(s): 所以这是我的问题:

  1. What would be a possible cause for the connection issue? 连接问题的可能原因是什么?

  2. What is the proper way to keep a client alive? 使客户存活的正确方法是什么? ( secondary goal but if you know it, that'd be great) (次要目标,但如果您知道,那就太好了)

*I'm using LibGDX and Kryonet for this. *我为此使用LibGDX和Kryo​​net。 As far as I know, they shouldn't have any conflicts. 据我所知,他们不应该有任何冲突。

Server: 服务器:

package com.me.mygdxgame;
import java.io.IOException;
import java.util.ArrayList;

import com.badlogic.gdx.math.Vector2;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.me.mygdxgame.Network.Obstacles;

public class GameServer {
    Server server;

    public GameServer () throws IOException {
        server = new Server() {
            protected Connection newConnection () {
                return new PlayerConnection();
            }
        };
        Network.register(server);


        //Sends Stuff to Client
        server.addListener(new Listener() {
            public void received (Connection c, Object object) {
                PlayerConnection connection = (PlayerConnection)c;

                if (object instanceof Obstacles) {
                    if (connection.name != null) return;
                    ArrayList<Vector2> obs = ((Obstacles)object).obstacles;
                    if (obs == null) return;
                    System.out.println("Obstacles recieved.");
                    for(int i = 0; i < obs.size(); i++) 
                        System.out.println("Obstacle " + i + "- x: " + obs.get(i).x  );
                    return;
                }
            }
        });


        server.bind(Network.port);
        server.start();

    }

    public void sendAll () { //Send out data
        Obstacles ob = new Obstacles();
        ob.obstacles = new ArrayList<Vector2>();
        for(int i =0; i < Map.obstacles.size(); i++){
            ob.obstacles.add(new Vector2(Map.obstacles.get(i).x,Map.obstacles.get(i).y));
        }
        server.sendToAllTCP(ob);
    }

    static class PlayerConnection extends Connection {
        public String name;
    }
}

Client: 客户:

package com.me.mygdxgame;

import java.awt.EventQueue;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import com.badlogic.gdx.ApplicationListener;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.me.mygdxgame.Network.Obstacles;

public class GameClient implements ApplicationListener{
    Client client;
    String name;
    String RefreshHost;
    boolean Connected = false;
    ArrayList<String> hosts = new ArrayList<String>();
    public static String host;

    public GameClient (String host) {
        client = new Client();
        client.start();
        this.host = host;

        Network.register(client);

        client.addListener(new Listener() {
            public void connected (Connection connection) {
                System.out.println("connected");
                Connected = true;
            }

            public void received (Connection connection, Object object) {
                if (object instanceof Obstacles) {
                    Obstacles obs = (Obstacles)object;
                    System.out.println("Obstacle recieved on client - " + obs.obstacles.size());
                    client.sendTCP(obs);
                    System.out.println("Obstacles sent back.");
                    return;
                }else {
                    System.out.println("invalid packet");
                }
            }

            public void disconnected (Connection connection) {
                EventQueue.invokeLater(new Runnable() {
                    public void run () {
                        System.out.println("closed");
                        Connected = false;
                        client.close();
                    }
                });
            }
        });

        new Thread("Connect") {
            public void run () {
                try {
                    client.connect(5000, GameClient.host, Network.port);
                    System.out.println("Connected!");
                    client.setKeepAliveTCP(NORM_PRIORITY);
                    while(Connected) {
                        //System.out.println(client.isIdle());
                    }
                    client.run();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }

    @Override
    public void create() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void render() {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }
}

I suggest you set the host BEFORE you start the client 我建议您在启动客户端之前设置主机

   public GameClient (String host) {
        client = new Client();
        this.host = host;
        client.start();

I am not familiar with kryonet Client, but it makes sense to do it that way. 我对kryonet客户不熟悉,但是这样做是有意义的。

Generally make sure that your client is trying to connect to the host that you have server running on... 通常,请确保您的客户端正在尝试连接到运行服务器的主机...

One possible cause for such connection issue is a firewall blocking your Network.port 造成这种连接问题的一个可能原因是防火墙阻止了您的Network.port

Another one, sorry but I have to ask: Is the server-app running in the other machine? 另一个,对不起,但我不得不问:服务器应用程序是否在另一台计算机上运行?

I ask because I dont'see a main function in your server code 我问是因为我没有在您的服务器代码中看到主要功能

public static void main(String[] args) throws IOException {
        Log.set(Log.LEVEL_DEBUG);
        new GameServer();
    }

I use to get running my server-app with this terminal command 我经常使用此终端命令来运行我的服务器应用程序

java -jar myserverfile.jar 

How do you get it running in the "remote" machine? 您如何使其在“远程”计算机上运行?

By the way, I am using libgdx and kryonet for my game and so far I haven't get issues using them together. 顺便说一句,我在游戏中使用libgdx和kryonet,到目前为止,我并没有遇到一起使用它们的问题。

In my case I have the server in a AWS instance listening for game-client testing from my computer. 以我为例,我在AWS实例中让服务器监听计算机上的游戏客户端测试。

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

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