Deserialisation issue - java.io.StreamCorruptedException: invalid type code: 00

后端 未结 2 472
既然无缘
既然无缘 2021-01-25 23:11

I\'m writing a java file-transfer app, and i have some troubles with deserialisation myself-defined class Message from Datagramms.

Other topics at StackOverflow has simi

2条回答
  •  梦谈多话
    2021-01-25 23:46

    receiveMSG = (Message) deserialize(receivedPacket.getData());

    You need to change this to

    receiveMSG = (Message) deserialize(receivedPacket.getData(), receivedPacket.getOffset(), receivedPacket.getLength);
    

    and adjust your 'deserialise()' method accordingly, to accept 'offset' and 'length' parameters, and to use them when constructing the ByteArrayInputStream.

    EDIT The code you posted doesn't compile: the expression that constructs the outgoing datagram packet is incorrect. It should be

    new DatagramPacket(test, test.length, datagramSocket.getInetAddress(), datagramSocket.getPort())
    

    I have no idea what packetOverhead is supposed to be, but you don't need it.

    NB You don't need to create a new Message() the line before you call readObject().

提交回复
热议问题