Friday, January 9, 2009

Java Serialization and Sockets

I remember working with sockets years ago and thinking how much work was involved to get simple messages over the pipe. Even though I was doing the sockets programming in Java, and Java greatly simplifies the actual socket connections, it was still intimidating designing a communications library and breaking everything up into bit streams.

Since then I've tried a number of high level communications schemes like CORBA, RMI, and web services. Let's face it, CORBA was just painful. The price of getting that high level connection was just too high. RMI was big improvement. With surprisingly little effort you could send fully instantiated objects over the pipe .. as long as the pipe didn't include any firewalls. Web services work great with firewalls, but you have to jump through hoops to maintain state. In case you haven't noticed, state is a pretty useful thing to have. Working in a stateless environment seems to be a step back into the dark ages. Of course, I can implement my own session state, but wasn't the initial goal to make things easier?

Recently I've been working with sockets again. But this time I've thrown Java serialization into the mix. This allows me to serialize high level Java objects and send them over the pipe without writing all the usual code to break the object down into bits and then recreate it on the other end. Sure, you still have to develop a communication language for the sockets, but far fewer commands are required, as everything is essentially just treated as a single piece of data.

While using sockets may seem like a step backwards, it is the single solution that provides both inherent statefulness and simple firewall traversal. I certainly won't be advocating sockets for everything, but if you find yourself working too hard just to get your network layer functioning maybe you should consider it.