I don't really know what im doing wrong, just trying to send an object called 'packet' from server to client using TCP.
This is how its serialized and sent server side
1 2 3 4 5 6
| bos = new ByteArrayOutputStream(); objectOut = new ObjectOutputStream(bos); objectOut.writeObject(packet); String objectBytes = bos.toString(); ps.println(objectBytes); |
Here is the client side code
1 2 3 4 5 6 7 8
| String data = br.readLine();
byte[] objectData = data.getBytes(); bis = new ByteArrayInputStream(objectData); in = new ObjectInputStream(bis); packet = (Packet) in.readObject(); in.close(); |
This is sending strings, i tried doing it by just sending a the length of the byte array, then the byte array and reciving it like that client side but just couldnt get it working. Ive had several errors probably most commen is this 'java.io.StreamCorruptedException: invalid stream header', but i also get a 'java.io.EOFException' error. Am i go going about this completely the wrong way?