当前位置: 首页 > 工具软件 > KryoNet > 使用案例 >

客户端收到服务器响应的端口,java - Java Kryonet服务器,客户端无法接收服务器响应 - 堆栈内存溢出...

沈单弓
2023-12-01

客户端成功发送消息“这是请求!” 到服务器(服务器正在打印出来)但是,即使服务器正在发送服务器,客户端也没有从服务器接收任何响应。

我试过修复它没有成功,任何人都可以看到或建议可能的问题/解决方案与代码?

(代码如下)

客户

public class Client_test {

Client client = new Client();

public Client_test() {

Kryo kryo = client.getKryo();

kryo.register(SomeRequest.class);

kryo.register(SomeResponse.class);

client.start();

try {

client.connect(50000, "127.0.0.1", 54555, 54777);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

client.addListener(new Listener() {

public void received (Connection connection, Object object) {

if (object instanceof SomeResponse) {

SomeResponse response = (SomeResponse)object;

System.out.println(response.text);

}

}

});

SomeRequest request = new SomeRequest();

request.text = "Here is the request!";

client.sendTCP(request);

}

}

服务器

public class ServerGame {

Server server = new Server();

public ServerGame() {

Kryo kryo = server.getKryo();

kryo.register(SomeRequest.class);

kryo.register(SomeResponse.class);

server.start();

try {

server.bind(54555, 54777);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

server.addListener(new Listener() {

public void received (Connection connection, Object object) {

if (object instanceof SomeRequest) {

SomeRequest request = (SomeRequest)object;

System.out.println(request.text);

SomeResponse response = new SomeResponse();

response.text = "Thanks!";

connection.sendTCP(response);

}

}

});

}

}

响应和请求类

public class SomeRequest {

public String text;

public SomeRequest(){}

}

public class SomeResponse {

public String text;

public SomeResponse(){}

}

 类似资料: