1

I have a kryonet client/server that work find.. well mostly. 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.

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

    }
}
4

2 回答 2

0

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

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

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

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

于 2013-10-07T20:02:49.433 回答
0

导致此类连接问题的一个可能原因是防火墙阻止了您的 Network.port

另一个,对不起,但我不得不问:服务器应用程序是否在另一台机器上运行?

我问是因为我在您的服务器代码中没有看到主要功能

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

我用这个终端命令来运行我的服务器应用程序

java -jar myserverfile.jar 

你如何让它在“远程”机器上运行?

顺便说一句,我在我的游戏中使用了 libgdx 和 kryonet,到目前为止,我还没有遇到一起使用它们的问题。

在我的例子中,我有一个 AWS 实例中的服务器从我的计算机监听游戏客户端测试。

于 2015-04-29T22:33:48.917 回答