gpt4 book ai didi

java - Kryonet 刚连接就断开

转载 作者:搜寻专家 更新时间:2023-11-01 01:36:06 28 4
gpt4 key购买 nike

我关注了this Youtube tutorial涵盖 Kryonet 的基础知识。

基本上它是一个 Kryonet Hello World,它解释了如何设置一个基本的服务器和一个客户端,允许客户端向服务器发送数据包并进行非常基本的通信。

source code 的链接. Server 和 Client 都有相同的 Packet 类。

我可以让服务器运行,客户端请求 IP 进行连接。但是,当我输入 IP 时,客户端会在连接后立即终止。

客户端输出:

00:03  INFO: Connecting: /127.0.0.1:54555
00:03 INFO: [kryonet] Connection 1 connected: /127.0.0.1
00:03 INFO: [CLIENT] You have connected.
BUILD SUCCESSFUL (total time: 3 seconds)

服务器命令行日志:

00:00  INFO: [kryonet] Server opened.
00:04 DEBUG: [kryonet] Port 54555/TCP connected to: /127.0.0.1:53217
00:04 DEBUG: [kryo] Write: RegisterTCP
00:04 INFO: [kryonet] Connection 1 connected: /127.0.0.1
00:04 INFO: [SERVER] Someone has connected.
00:04 DEBUG: [kryonet] Connection 1 update: Se ha forzado la interrupcion de una
conexion existente por el host remoto
00:04 INFO: [SERVER] Someone has disconnected.
00:04 INFO: [kryonet] Connection 1 disconnected.

好像系统关闭了TCP连接,但我不太清楚。我必须在 Windows 或/和路由器中启用某些功能才能允许 Kryonet 通信吗?

有人能发现问题吗?提前致谢。

命令行日志中以西类牙语显示的行类似于“远程主机已强制中断现有连接。”

在 user1816380 建议后编辑:

大多数时候它仍然显示原始错误,但有时您会看到:

00:00  INFO: [kryonet] Server opened.
00:07 DEBUG: [kryonet] Port 54555/TCP connected to: /127.0.0.1:50787
00:07 DEBUG: [kryo] Write: RegisterTCP
00:07 INFO: [kryonet] Connection 1 connected: /127.0.0.1
00:07 INFO: [SERVER] Someone has connected.
00:07 DEBUG: [kryo] Read: Packet0LoginRequest
00:07 DEBUG: [kryonet] Connection 1 received TCP: Packet0LoginRequest
00:07 DEBUG: [kryo] Write: Packet1LoginAnswer
00:07 DEBUG: [kryonet] Connection 1 sent TCP: Packet1LoginAnswer (6)
00:07 DEBUG: [kryonet] Connection 1 update: Se ha forzado la interrupcion de una
conexion existente por el host remoto
00:07 INFO: [SERVER] Someone has disconnected.
00:07 INFO: [kryonet] Connection 1 disconnected.

最佳答案

为了让您的客户端保持连接,它需要发送和接收 KeepAlive 数据包。当你调用 client.start();客户端连接();客户端线程在后台启动,自动为您处理。

您似乎在阻止客户端线程执行此操作,因为您使用无限循环阻止它来处理用户输入(While(true) 获取用户输入)。

相反,您应该有一个单独的线程来接收用户输入。这是实现客户端接收功能的一种方法(可能不是最好的):

public void received(Connection c, Object o) {
System.out.println("received something");
if (o instanceof Packet1LoginAnswer){

boolean answer = ((Packet1LoginAnswer) o).accepted;

if (answer) {
System.out.println("Please enter your message for server");
new Thread("Get User Input") {
public void run () {
try {

if (ChatClient.scanner.hasNext()){
Packet2Message mpacket = new Packet2Message();
mpacket.message = ChatClient.scanner.nextLine();
client.sendTCP(mpacket);
System.out.println("Please enter another message");
}

} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
}.start();

} else {
System.out.println("Answer is false");
c.close();
}
}

if (o instanceof Packet2Message){
String message = ((Packet2Message) o).message;
Log.info(message);
}
}

我还注意到您正在使用 Log.info()。这仅在您使用 Kryonet 的调试版本时有效。最好只使用标准输出函数。

关于java - Kryonet 刚连接就断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13324632/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com