0

当我使用这条线进行连接时,我的 Kryonet 服务器在 5000 毫秒后断开连接 client.connect(5000, host, Network.port); 我认为 5000 是连接超时,但是当我运行连接时,它能够连接并接收我发送的类,但它在之后断开与服务器的连接5000 毫秒。

我正在修改 Kryonet 提供的基本 ChatClient.java。这就是我想出的。

import java.awt.EventQueue;
import java.io.IOException;

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;

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

        // For consistency, the classes to be sent over the network are
        // registered by the same method for both the client and server.
        Network.register(client);

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

            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());
                    return;
                }else {
                    System.out.println("invalid packet");
                }
            }

            public void disconnected (Connection connection) {
                EventQueue.invokeLater(new Runnable() {
                    public void run () {
                        client.close();
                        // Closing the frame calls the close listener which will stop the client's update thread.
                    }
                });
            }
        });

        final String host = "localhost";

        // We'll do the connect on a new thread so the ChatFrame can show a progress bar.
        // Connecting to localhost is usually so fast you won't see the progress bar.
        new Thread("Connect") {
            public void run () {
                try {
                    client.connect(5000, host, Network.port);
                    // Server communication after connection can go here, or in Listener#connected().
                } catch (IOException ex) {
                    ex.printStackTrace();
                    System.exit(1);
                }
            }
        }.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

1

尝试使用 client.SetKeepAliveTCP(int smallthendisconnecttime);

于 2013-10-27T20:15:56.523 回答
0

如果您将服务器配置为 TCPUDP,但随后让您的客户端通过 TCP 连接,则可能会出现您所描述的问题。

如果您想利用主机发现但此后只需要 TCP 连接,那么建议您“为 UDP 发现运行单独的服务器”

基本上,您需要确保客户端和服务器协议使用匹配(服务器和客户端都设置为仅 TCP,或者服务器和客户端都设置为 TCP 和 UDP)。

于 2014-05-17T11:07:44.210 回答