Java 在未关闭的情况下发送包时,会出现错误
当我试图在游戏中发送关于玩家坐标的包而不关闭任何东西时,我会出现以下错误:Java 在未关闭的情况下发送包时,会出现错误,java,Java,当我试图在游戏中发送关于玩家坐标的包而不关闭任何东西时,我会出现以下错误: java.io.StreamCorruptedException:无效流头:74000632 当主机/客户端尝试发送包时会发生这种情况。另外,当我得到那个错误时,它指向一行ois=newobjectinputstream(client.getInputStream())怎么办?(我只是糊涂了,请帮忙) 托管代码: static void hosting() throws IOException, ClassNotFoun
java.io.StreamCorruptedException:无效流头:74000632
当主机/客户端尝试发送包时会发生这种情况。另外,当我得到那个错误时,它指向一行ois=newobjectinputstream(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();
}
}
尝试putois=newobjectinputstream(client.getInputStream())代码>循环外。尝试putois=newobjectinputstream(client.getInputStream())代码>循环外。为什么要在循环中调用getInputStream?请尝试使用put ois=new ObjectInputStream(client.getInputStream());超出循环。@Tix它实际起作用了!谢谢(请在答案中键入,以便我可以将其设置为工作答案)谢谢!为什么要在循环中调用getInputStream?请尝试put ois=new ObjectInputStream(client.getInputStream());超出循环。@Tix它实际起作用了!谢谢(请在答案中键入,以便我可以将其设置为工作答案)谢谢!