java.io.StreamCorruptedException:无效的流标题:4C756E69

时间:2013-03-18 18:10:42

标签: java udp objectinputstream

我很抱歉,如果这个问题被多次询问,但经过数小时的谷歌搜索/实验,我还没有解决我的问题。

我正在尝试通过UDP套接字将一个对象(ServerPlayer)从我的服务器发送到我的客户端,当客户端收到数据包时,我得到一个无效的流标头。我不确定什么可能修改数据导致损坏/不存在的标头。

发件人主题:

//Thread loop
@Override
public void run()
{
    System.out.println("Thread " + threadCount + " running");
    //Test object transmission code.
    data = new byte[2048];
//  -----------------------------------------------------------------------------------
//  TRANSMISSION SETUP/OBJECT TRANSFER
    try 
    {
        //In case of delay
        socket.setSoTimeout(1000);

        //Object Transmission setup
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bStream);
        oo.writeObject(p);
        oo.close();

        //Transmit object
        data = bStream.toByteArray();
        packet = new DatagramPacket(data, data.length, address, 4415);
        socket.send(packet);
//  -----------------------------------------------------------------------------------
//  GAME LOOP
        while(socket.getSoTimeout() > 0)
        {

        }
// -----------------------------------------------------------------------------------
//  GET TO THE CHOPPA CODE BELOW
    } 
    catch (SocketException e) 
    {
        System.out.println("Player " + username + " timed out");
    } 
    catch (IOException e) 
    {
        System.out.println("Server: ");
        e.printStackTrace();
    }
}

客户端/接收器:

public MultiplayerGameStart() throws IOException
{
    address = InetAddress.getByName("127.0.0.1");
    serverip = InetAddress.getByName(NetSettingsManager.ipAdd);

}

@Override
public void run() 
{
    //Packet byte array
    data = new byte[2048];
    try 
    {


        //Send connection REQ
        socket = new DatagramSocket(4415, address);
        data = PacketUtil.intTobyte(NetSettingsManager.NET_FUNCTION.REQ.getFunctionCode(), data.length);
        packet = new DatagramPacket(data, data.length, serverip, serverport);
        socket.send(packet);

        //Wait for ACK
        packet = new DatagramPacket(data, data.length);
        socket.receive(packet);

        //Converts ACK bytes to int to be read. If ACK code is correct, continue.
        if(PacketUtil.byteToint(packet.getData()) == NetSettingsManager.NET_FUNCTION.ACK.getFunctionCode())
        {
            //Returns username to be checked on playerlist.
            System.out.println("Logging into server..."); //DEBUG output
            data = NetSettingsManager.username.getBytes();
            packet = new DatagramPacket(data, data.length, serverip, serverport);
            socket.send(packet);

            //Stub code below. Will receive player object generated by server.
            packet = new DatagramPacket(data, data.length);
            socket.receive(packet);

            ObjectInputStream iStream = new ObjectInputStream(new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength()));
            //iStream.reset();
            ServerPlayer messageClass = (ServerPlayer) iStream.readObject();
            iStream.close();

            space = new PlayState();
            space.addEntity(new ClientPlayer(received));



        }

    } 
    catch (IOException e)
    {
        System.out.println("Client: ");
        e.printStackTrace();
    }
    catch(ClassNotFoundException e)
    {
        System.out.println("Client: ");
        e.printStackTrace();
    }
}

ServerPlayer(注意,我已从此示例中省略了我的函数,但如果需要,我可以提供它们)。

public class ServerPlayer extends ServerEntity implements Serializable 
{
    int[] material = new int[4];
    Chunk pChunk;

public ServerPlayer(String username, float posX, float posY)
{
    super(username, posX, posY);
    //Material table.
    material[0] = 25;    //metal
    material[1] = 33;    //energy
    material[2] = 43;    //ion
    material[3] = 131;   //gas
}
}

堆栈追踪:

   java.io.StreamCorruptedException: invalid stream header: 4C756E69
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at client.multiplayer.MultiplayerGameStart.run(MultiplayerGameStart.java:80)
at java.lang.Thread.run(Unknown Source)

感谢您的时间:)。

更新

所以事实证明在线程中运行我的发送者代码导致了问题。现在我的数据包输出是

server:sent 243 bytes: -84,-19,0,5 | client: received 8 bytes -84, -19, 0, 5 

但遗憾的是我现在正在

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
at java.io.ObjectInputStream.readUTF(Unknown Source)
at java.io.ObjectStreamClass.readNonProxy(Unknown Source)
at java.io.ObjectInputStream.readClassDescriptor(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at client.multiplayer.MultiplayerGameStart.run(MultiplayerGameStart.java:80)
at java.lang.Thread.run(Unknown Source)`

1 个答案:

答案 0 :(得分:1)

取消对reset()的调用。它们不添加任何内容,我相信对bStream.reset();的服务器端调用正在删除创建ObjectOutputStream时写入的标题。

此外,构造ByteArrayInputStream时,应使用三参数变量,从数据报包中传递getOffset()getLength()

在将来,当提出这样的问题时,请发布异常的实际堆栈跟踪,以及足够的代码来验证异常是否发生在您认为的位置(在这种情况下,完整的接收方法)


编辑:似乎客户端和服务器都在发送和接收到同一端口(4415)。根据您提供的其他调试信息,我相信客户端正在获取它尝试发送到服务器的初始数据报。尝试为每个端口使用不同的端口(并使用常量,例如CLIENT_PORTSERVER_PORT来指定它们。)