Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Transfering data between JAVA programs

Comments Blog About Development Research Sites

Transfering data between JAVA programs

Apr 4, 2010
With the advent of the world wide web, a crucial role is often reserved for data transfer between hosts. A great many protocols exist to accomplish this - as you read this, every word that passes your eyes has been send using the hypertext transfer protocol over a Transmission Control Protocol connection (which in turn uses IP as internet layer and often ethernet as link layer). While programmers very seldom have to worry about lower level protocols, the specific application layer protocol choice is sometimes less obvious or easy (except of course for frontend developers who really only have the choice of HTTP / xmlhttp).

While writing a client/server application in JAVA there were two obvious choices: plain text (Telnet in essence) and SOAP. I will discuss each shortly first and then move on to perhaps a less obvious alternative.

Plain text (Telnet) communication
Perhaps the easiest protocol available, it really is little more than plain text being send back and forth between a client and a server. Setting up a SocketServer in JAVA is extremely easy and can be accomplished with a few lines of code. A complete tutorial can be found here.

An obvious problem I stumbled upon was that the example uses inputStream.readLine() to gather messages. This is fine if you only send one line of text at a time, but what if you want to send several lines? What if you do not know how many lines of text you are going to send?

One solution, of course, is to simply hack in an ugly string replace on the message before transmission and remove it upon receiving. Much neater however is to use the appropriate ascii control character. By appending "(char) 23" to your message you can send an end-of-transmission-block signal indicating your message is done sending. Serverside it will be necesary to look for and detect these control characters but this is a fairly trivial task.

Less trivial however is the matter of content transmission: what if you want to send the contents of the last ten e-mails someone received for example? These will contain loads of characters, HTML maybe, and definately EOL characters. Of course, you could first send a message anouncing how many messages you will add and then send them one by one, but this greatly increases the complexity of your handling server. Alternatively you could add control sequences - tags indicating where a message starts and ends (), but following this logic you are in essence implementing your very own version of XML-RPC / SOAP, and I daresay you will be hard pressed to do better than their creators.

While I have little experiencing writing a SOAP server, building clients has these days become second nature: load a WSDL if available, fool around with namespaces a bit, throw a couple of insults at .NET servers for their intricate handling, yell some more at your libraries et voila: you can now talk easily between your application and the external one.

Of course, implementations vary slightly - in PHP you can call methods on SOAP objects as if they were local classes, in JAVA you are forced to either use an external library or write an interface to use the MessageFactory class and his comrades to build up SOAP messages before transmission. You can find an example JAVA SOAP interface here, but keep in mind this was written for a SOAP server that did not provide a WSDL file - which made the exercise the more difficult.

While in general SOAP is awesome, it does have one major disadvantage: it is hopelessly bloated. Of course, for sending over large sets of data the amount of overhead is negligable, but a single information request command ("server info") will increase several hundred times in size before it reaches the wire - not in the least because each message requires an extensive header and a great many XML tags. Depending on your platform, it might not even keep open the connection resulting in frequent reconnects. Close then, but no cigar.

Serialized JAVA objects
So we do not want to bother with seperating different aspects of our message, yet we also want to avoid using a bloated system of tags to do so. This is where object serialization comes into play: simply create an object, add the data you want, point in the direction of your connection and tell it to get moving!

It really is as easy and beautifull as it sounds: JAVA has special in- and output stream classes for objects, the ObjectInputStream and ObjectOutputStream class respectively, which both accept a Socket stream. You do not even have to worry about control characters or suchlike, just create a while loop reading objects and you're set:
Code (php) (nieuw venster):
ObjectInputStream ois     = new ObjectInputStream(socket.getInputStream());

while ((
command = (Transfer) ois.readObject()) != null)

For the complete example, see my source here or one of the excelent examples at

Things to do keep in mind:

- Transfered objects will have to be known both on the server as well as the client. Thus, a shared library of possible transfer objects will help. Alternatively, you can use a single object with data managing abilities, by for example extending HashTable.
- Transferable objects must be serializable. This can be achieved by implementing the Serializable interface. Adding a serialVersionUID won't hurt either.
- Have a look at Remote Method Invocation - I wish I had before I started.

FragFrog out!

New comment

Your name: