Kryonet客户端连接问题Java

时间:2013-10-07 19:38:44

标签: java libgdx lan kryonet

我有一个kryonet客户端/服务器可以找到..主要是好的。客户端仍然处于闲置状态,并且最终会在一段时间后断开连接,但这不是我正在尝试解决的问题。目前,只要客户端和服务器在同一台计算机上,服务器和客户端就可以建立连接并来回发送数据(在客户端超时之前)。如果您尝试连接到LAN上的其他计算机,则连接超时并失败。

所以这是我的问题:

  1. 连接问题的可能原因是什么?

  2. 保持客户端活着的正确方法是什么? (次要目标,但如果你知道,那就太好了)

  3. *我正在使用LibGDX和Kryo​​net。据我所知,他们不应该有任何冲突。

    服务器:

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

    客户:

    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
    
        }
    }
    

1 个答案:

答案 0 :(得分:0)

我建议您在启动客户端之前设置主机

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

我对kryonet Client不熟悉,但这样做是有意义的。

通常要确保您的客户端正在尝试连接到运行服务器的主机...