Question

Let's assume I want to send many messages between 2 programs made in java that use TCP sockets.

I think the most convienient way is to send objects like:

PrintStream ps = new PrintStream(s.getOutputStream());

ObjectOutputStream oos = new ObjectOutputStream(ps);

  some_kind_of_object_here;

  oos.writeObject(some_kind_of_object_here);

 ps.print(oos);

I want to send, strings, numbers, HashMaps, boolean values How can I do this using fx 1 object that can store all that properties? I though about ArrayList that is serializable and we can put there everything but is not elegant way. I want to send different types of data because user can choose from a variety of options that server can do for it. Any advices?

Was it helpful?

Solution

Master, you know you can send any serializable object across a socket to another JVM, yes?

If so, the easiest way is to have a serializable object containing all your objects and then just forward that. A list of objects is probably the most simple. You can then deserialize it on the other side, and process the objects in the list in whatever way you need.

I would suggest you read up on the serialization technology in Java, so you know all the smart things you can do.

http://java.sun.com/javase/7/docs/technotes/guides/serialization/index.html

OTHER TIPS

Have you considered using RMI?

Otherwise stick with Serializable or even Externalizable, if you need extra performance.

You don't need wrapper classes like Lists. Of course you can build your own wrapper object thats holds togeher all associated data. Such "application data units" will help you to take care of the right order you send the entites. As long as all member are Serializable, the complete object graph will be serialized automagically, once you put the wrapper object into the object stream.

If you don't use wrapper classes, just be sure every object/type you want to transer implements at least Serializable. Then you have to verify that the order of data send corresponds with the order you except data to arrive.

The way you wrote you code, you already can send any Java object. Those objects, of course, ihclude "strings, numbers, HashMaps, boolean values".

Just write into ByteArrayOutputStream:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.write(javaObject);
oos.close();
socket.write(baos.toByteArray())

If you need to keep packets small you can use a plain Object[], place whatever you want inside and use an enum that specifies the packet type, allowing you to how many and which kinds of objects are store inside the message.

class Message
{
  Type t;
  Object[] params;
}

of course you will have to switch according to Type t when choosing what to do with every packet, but since protocols are usually FSMs (it depends on what you are coding) this would work well without wasting too much space or allocating lists when it's not needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top