I’m using an ObjectOutputStream over my socket because I created a new class that needs to be transported between the client and the server.
I also created a unique thread that sends one byte every one second over the stream to the server in order to inspect the connection continuously and check that its alive.
byte b=1;
oos.writeObject(b);
I use “byte” because it is the smallest object that I can send (right?) so that the server will not read longer objects.
My question is if the server reads one byte object (size of byte) or an 8 byte object (the size of an object)
?
Probably neither. First, a
byteis autoboxed into aByte. Then theByteis serialized to your output stream. It probably takes quite a bit more than 8 bytes to send. I don’t know the spec exactly, but it probably sends the class namejava.lang.Byteand the byte itself, plus probably a few more control bytes.An easy way to tell – serialize your byte to a
ByteArrayOutputStream, flush yourObjectOutputStream, then see how many bytes yourByteArrayOutputStreamends up with.