java.io.StreamCorruptedException:invalid stream header:00000001 [英] java.io.StreamCorruptedException: invalid stream header: 00000001

查看:433
本文介绍了java.io.StreamCorruptedException:invalid stream header:00000001的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继续得到这个异常

java.io.StreamCorruptedException: invalid stream header: 00000001

服务器端我用它来发送和接收int,工程

Server side I used this to send and receive int, works fine.

服务器:

new DataOutputStream(player1.getOutputStream()).writeInt(P1);

客户端:

dataFromServer = new DataInputStream(socket.getInputStream());
dataFromServer.readInt();

但是,当我尝试发送一个对象时,就会出现错误。

But when I try to send an object, like this, it gives the error.

服务器:

new ObjectOutputStream(player2.getOutputStream()).writeObject(gameCrossword);

客户端:

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

根据jtahlborn的建议,我更改了只使用对象流而不是数据流的代码

I changed the code to use only object streams rather than data streams, upon the advice of jtahlborn

服务器

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);

客户端

              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。

regards, C.

推荐答案

不要用多个输入/输出流包装套接字流。这样会打破各种不好的方式。在这种具体情况下,ObjectInputStream从构建上的流读取一个头,这是在从流中读取int之前发生的。不管怎样,只需使用一个ObjectOutputStream和ObjectInputStream,并将数据流拖放(注意ObjectOutputStream有一个 writeInt 方法)。

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).

这篇关于java.io.StreamCorruptedException:invalid stream header:00000001的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆