Reputation: 1135
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) ?
Upvotes: 2
Views: 1281
Reputation: 533472
If you must use an ObjectOutputStream, it really doesn't matter which object you send because after the first one is sent, a reference to that object will be sent in the future. For this reason I suggest you send a specific enum like.
enum Control {
HEARTBEAT
}
You could make the wire format much smaller using DataOutputStream where 1 byte is one byte. Given the IP packet header is about 20 bytes, it doesn't really matter whether you send 1 byte or 8 bytes as the overhead is much higher than this.
Upvotes: 1
Reputation: 23265
Probably neither. First, a byte
is autoboxed into a Byte
. Then the Byte
is 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 name java.lang.Byte
and the byte itself, plus probably a few more control bytes.
An easy way to tell - serialize your byte to a ByteArrayOutputStream
, flush your ObjectOutputStream
, then see how many bytes your ByteArrayOutputStream
ends up with.
Upvotes: 2