java.io.StreamCorruptedException:无效的流标头:00000001
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20773657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.io.StreamCorruptedException: invalid stream header: 00000001
提问by user3131312
I keep getting this get this Exception
:
我不断得到这个得到这个Exception
:
java.io.StreamCorruptedException: invalid stream header: 00000001
Server side I used this to send and receive int, works fine.
服务器端我用它来发送和接收 int,工作正常。
Server:
服务器:
new DataOutputStream(player1.getOutputStream()).writeInt(P1);
Client:
客户:
dataFromServer = new DataInputStream(socket.getInputStream());
dataFromServer.readInt();
But when I try to send an object, like this, it gives the error.
但是当我尝试像这样发送一个对象时,它给出了错误。
Server:
服务器:
new ObjectOutputStream(player2.getOutputStream()).writeObject(gameCrossword);
Client:
客户:
objectFromServer = new ObjectInputStream(socket.getInputStream());
crossword = (Crossword)objectFromServer.readObject();
Any help would be good. Here is me sending the crossword initially prior to game session
任何帮助都会很好。这是我在游戏会话之前最初发送填字游戏
I changed the code to use only object streams rather than data streams, upon the advice of jtahlborn
根据 jtahlborn 的建议,我将代码更改为仅使用对象流而不是数据流
server
服务器
player1 = serverSocket.accept();
serverLog.append(new Date() + ": Player 1 joined session " + sessionNo + '\n');
serverLog.append("Player 1's IP address" + player1.getInetAddress().getHostAddress() + '\n');
new ObjectOutputStream(player1.getOutputStream()).writeInt(P1);
new ObjectOutputStream(player1.getOutputStream()).writeObject(gameCrossword);
player2 = serverSocket.accept();
serverLog.append(new Date() + ": Player 2 joined session " + sessionNo + '\n');
serverLog.append("Player 2's IP address" + player2.getInetAddress().getHostAddress() + '\n');
new ObjectOutputStream(player2.getOutputStream()).writeInt(P2);
new ObjectOutputStream(player2.getOutputStream()).writeObject(gameCrossword);
client
客户
private void connectToServer() {
try {
Socket socket = new Socket(host, 8000);
objectFromServer = new ObjectInputStream(socket.getInputStream());
objectToServer = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException ex) {
System.err.println(ex);
}
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
try {
player = objectFromServer.readInt();
crossword = (Crossword)objectFromServer.readObject();
System.out.println(crossword);
regards, C.
问候,C.
采纳答案by jtahlborn
don't wrap the socket streams with more than one input/output streams. this will break in all kinds of bad ways. in this specific case, the ObjectInputStream reads a header from the stream on construction, which is happening before you have read the int from the stream. regardless, just use a single ObjectOutputStream and ObjectInputStream and ditch the Data streams (note that ObjectOutputStream has a writeInt
method).
不要用多个输入/输出流包装套接字流。这将以各种糟糕的方式打破。在这种特定情况下, ObjectInputStream在构造时从流中读取标头,这是在您从流中读取 int 之前发生的。无论如何,只需使用单个 ObjectOutputStream 和 ObjectInputStream 并丢弃数据流(注意 ObjectOutputStream 有一个writeInt
方法)。