serialization - Java - Invalid stream header -
i working on networking java , having issue converting object byte array, splitting array 2 parts, sending each on tcp stream, receiving it, reconstructing byte array, , reforming object.
so far working. have except reconstruction of object. error when using objectinputstream:
java.io.streamcorruptedexception: invalid stream header: 34323435
which common error see online. have tried fixing it. 1 of causes i've heard of stream not flushed after sending bytes, code flus steam before sending it. code send data is:
public void sendtcp(objectoutputstream tcpout) { try { synchronized(tcpout) { tcpout.write(data); tcpout.flush(); } } catch (ioexception e) { e.printstacktrace(); } }
and able read bytes on server side. problem comes when combining bytes together. once done use recreate object:
bytearrayinputstream in = new bytearrayinputstream(data); objectinputstream = new objectinputstream(in); object object = is.readobject(); is.close(); in.close();
but error gets thrown on objectinputstream line. have looked @ raw data debugging , matches up. bytes of object, before split , sent, matches bytes recombined after received. i've been stuck on little while , helpful if help.
i having issue converting object byte array, splitting array 2 parts, sending each on tcp stream, receiving it, reconstructing byte array, , reforming object.
of course are. it's pointless. there's fluffing around here. you're over-complicating , making mistakes in process. don't need of this. it's waste of time , space. tcp splitting segments; ip splitting packets, , routers splitting fragments. don't need add layer of that.
- get rid of
bytearrayoutputstream
,bytearrayinputstream
- create 1
objectoutputstream
, 1objectinputstream,
in order, wrapped around socket output , input streams respectively, @ both ends, , keep them life of socket - use
writeobject()
,readobject()
directly on these object streams - don't use other streams or readers or writers on same socket.
Comments
Post a Comment