ObjectInputStream / ObjectOutputStream java and sockets

This is to give generic advice to people writing code that has Object level streaming on sockets. I went thru some pain to get my piece of code working and by this blog I hope to give some quick tips to you.

Tip 1 ) If you expect to read/write objects between a client to your server, then create just one objectoutputstream and outputstream on your client. Similarly create just one objectinputstream and inputstream on your server for that client. Many code fragments will give you reason to believe that you need to create a objectinput/outputstream for every message that you pass. No, you dont. Its wasteful and not needed. Moreover it will return a java.io.StreamCorruptedException: invalid type code: AC exception. AC being the header that is passed from outputstream to inputstream. The inputstream expects only one header for that stream. And the constructor of outputstream creates that :). Sorry, went too deep..

Tip 2 ) If you are doing so much of sockets, I am sure you are using a threadpool to ensure that you dont get screwed on perf. So watch what you lock in a synchronized block. Most often you have locked even the “processing” of data that you received on your objectstream. All you need to have in your sync block is your wait’s, notifies, your calls to add/remove data from the shared queue. Everything else should be outside the sync block. Remember if you are using a threadpool, you are using a shared queue, and all your threads need access to that sharedqueue, so release it asap.

Tip 3 ) Exception handling :). You thought u got a hold on it. Nope. it can be really crazy if you dont. Do some thinking here. You dont want to do a generic, printstacktrace on all. You want “handle” errors correctly. Infact this starts of a new discussion. You need a errormanager framework that handles errors for you. I know, it becomes crazy when you have 10+ dev’s and none of them know how to handle errors. I’ll share some tips to help centralize and control that issue. If you are interested to know more, put some commends on this blog, so that I know its worth my time.. :).

Tip 4) Documentation and Coding. I read some good articles recently which hinted at documenting/commenting only the why’s and not the how’s and what’s.

Tip 5 ) Create all your threads upfront. Dont create threads as and when you get client requests. Remember, clients expect quick response, if multiple clients are going to get alive at the same time, you dont want your server waiting for cpu time to create threads. It will slow down all the clients.

Tip 6 ) EOFException peekByte. Most probably you dont have a InputStream ready. Remember readObject is a blocking call. You dont need to call inputStream.available() to check if there are bytes.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s