I recently started the development of an application making intensive usage of networking. A first attempt was made using RMI and for a couple of reasons, we switched over
Found it:
Instead of:
out = new ObjectOutputStream(kkSocket.getOutputStream());
You should use:
out = new ObjectOutputStream(new BufferedOutputStream(kkSocket.getOutputStream()));
And
out.flush();
when sending a message.
...makes a huge difference ...though I don't know exactly why.
I would not trust the results of that benchmark. It does not ensure that the JVM is warmed up; i.e. that classes are loaded, initialized and compiled to native code before measuring execution times. There is a good chance that native code compilation kicks in part way through running the benchmark, and this inflates the time taken by one or more of the loops.
The problem that you're running into isn't low throughput with sockets; it's slow default Java serialization.
When you're sending the same object over and over, it's only really being serialized once, and then a reference to it is being sent each time; this explains why it goes so quickly.
When you're creating a new object every time, that new object has to be serialized using the relatively slow Java serialization mechanism, which is also probably much more complex than what you need.
What you can do to improve this is either implement custom serialization code for your class, or make your own object serialization protocol that is external to the class (and use DataOutput instead of ObjectOutputStream).
This article has a lot of good info, even if it is somewhat dated: http://java.sun.com/developer/technicalArticles/Programming/serialization/ See the last part on Performance Considerations
You should set the TCP_NODELAY
option when working with small packets. Otherwise they will get delayed in an attempt to optimize network communication (a.k.a. Nagle's algorithm).
socket.setTcpNoDelay(true);
The reason that using a buffer helps is because the delay hits small packets only. So it's not the buffering per se that helps, but the size of the packet.