これはよくある問題のようですが、トラブルシューティングに多くの時間を費やした後でも解決策を見つけるのに苦労しています. 更新されたソリューションがあることを願っています。
KryoNet Java ネットワーク ライブラリを使用して、単純なサーバーとクライアントをセットアップしています。私の問題は、サーバーに接続した直後にクライアントが切断されることです。
これが私のコードです:
サーバ
public class TheServer extends Listener {
static Server server;
static final int PORT = 8215;
public static void main(String[] args) throws IOException {
server = new Server();
server.start();
server.bind(PORT);
server.addListener(new TheServer());
System.out.println("server started on " + PORT);
}
public void connected(Connection c) {
System.out.println("connected: " + c.getID());
}
public void disconnected(Connection c) {
System.out.println("disconnected: " + c.getID());
}
}
クライアント
public class TheClient extends Listener {
static Client client;
static final String IP = "localhost";
static final int PORT = 8215;
public static void main(String[] args) throws IOException {
client = new Client();
client.start();
client.connect(5000, IP, PORT);
client.addListener(new TheClient());
//client.setKeepAliveTCP(2000);
}
}
を実行TheServer
した後TheClient
、コンソールに次のように表示されます。
server started on 8215
connected: 1
disconnected: 1
接続と切断の間の時間はほぼ即時であり、設定した接続タイムアウト時間よりも確実に短いことに注意してください。setKeepAliveTCP()
また、必要ではないと思いますが、機能するかどうかを確認するために挿入したため、メソッドをコメントアウトしたことにも注意してください。