gpt4 book ai didi

java - 发送包裹而不关闭时,出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 09:03:49 24 4
gpt4 key购买 nike

当我尝试在游戏中发送有关玩家坐标的包而不关闭任何内容时,出现此错误:

java.io.StreamCorruptedException: invalid stream header: 74000632

当主机/客户端尝试发送包时会发生这种情况。另外,当我收到该错误时,它指向 ois = new ObjectInputStream(client.getInputStream()); 线该怎么办? (我只是很困惑,请帮忙)

托管代码:

static void hosting() throws IOException, ClassNotFoundException {
ServerSocket hoster = new ServerSocket(9876);
System.out.println("-1");
Socket client = hoster.accept();
System.out.println("-2");
oos = new ObjectOutputStream(client.getOutputStream());
while (true) {
ois = new ObjectInputStream(client.getInputStream());
String message = (String) ois.readObject();
if (message.equals("Connected")) {
game.add(otherPlayer);
connected = true;
continue;
}
otherX = Character.digit(message.charAt(0), 10);
otherX *= 10;
otherX += Character.digit(message.charAt(1), 10);
otherX *= 10;
otherX += Character.digit(message.charAt(2), 10);
otherY = Character.digit(message.charAt(3), 10);
otherY *= 10;
otherY += Character.digit(message.charAt(4), 10);
otherY *= 10;
otherY += Character.digit(message.charAt(5), 10);
otherPlayer.setBounds(otherX, otherY, 50, 50);
oos.flush();
}
}

客户端代码:

static void client() throws IOException, ClassNotFoundException {
Socket client = new Socket("127.0.0.1", 9876);
oos = new ObjectOutputStream(client.getOutputStream());
oos.writeObject("Connected");
connected = true;
game.add(otherPlayer);
while (true) {
ois = new ObjectInputStream(client.getInputStream());
String message = (String) ois.readObject();
otherX = Character.digit(message.charAt(0), 10);
otherX *= 10;
otherX += Character.digit(message.charAt(1), 10);
otherX *= 10;
otherX += Character.digit(message.charAt(2), 10);
otherY = Character.digit(message.charAt(3), 10);
otherY *= 10;
otherY += Character.digit(message.charAt(4), 10);
otherY *= 10;
otherY += Character.digit(message.charAt(5), 10);
otherPlayer.setBounds(otherX, otherY, 50, 50);
oos.flush();
}
}

最佳答案

尝试输入 ois = new ObjectInputStream(client.getInputStream());跳出循环。

关于java - 发送包裹而不关闭时,出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60466597/

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