I am writing a program in Java where there are communications between two or more machines using UDP. My application sends objects after serializing them through the network to the other machine where it will be deserialized and dealt with it. I was successful in sending one kind of objects so far.
My problem is that I want the sender to be able to send different kind of objects, and for the receiver to be able to receive them and cast them again to their appropriate types. However, since UDP allocates a byte buffer then receive the data into the buffer, it is impossible to cast or detect the type of the received object as different objects have different sizes.
Is there is a way that I can use to send different kind of objects using UDP and then receive them at the other end? (I don't ask for code here, just some ideas)
Thanks
Edit: I am looking for the best way to send/receive different objects types without knowing what is the type of the next expected object. Say I have three objects types and I expect receiving any of them at any givin time. Another thing which crossed my mind after Brian's comment: How to set the buffer size for variable sized data-types like Strings, Arrays,...etc. As when receiving a UDP packet you have first to allocate a buffer with a size to receive that object. This is somehow related to my original question.开发者_JAVA技巧
What you serialize (use ObjectOutputStream), can also be deserialized (with ObjectInputStream). You retrieve an object then. It plays no role if you transport it over UDP, TCP, a pipe, a local file or whatever. Serialization writes to an OutputStream, deserialization reads from an InputStream.
You myObject.getClass() to detect what you received.
Why not wrap them in a known type (a Packet
) that has a payload of type Object
? You can then deserialise your Packet
and interrogate the resultant payload for its type.
Basically, it's deserializing the object and then perform an instanceof
check. However, since you explicitly mention the problem of the byte buffers when using UDP, i suggest to have a look at a messaging library which already performs this kind of management of byte buffers: JGroups
In pseudo code, working with jgroups looks like this:
JChannel channel = new JChannel("myapp");
Customer c = new Customer("IniTrodeInc.);
Book b = new Book("Twilight");
channel.send(new Message(c));
channel.send(new Message(b));
And on the receiving end
JChannel channel = new JChannel("myapp");
channel.addListener(new Listener(){
public void onMessage(Message m) {
if (m.getObject() instanceof Customer) {
handleNewCustomer((Customer)m.getObject()));
} else if (m.getObject() instanceof Book) {
handleNewBook((Book)m.getObject()));
} else {
// Ups, we received s.t.h else
}
}
});
JGroups performs the abstraction of the messaging channel automatically, you do not need to know whether this is UDP, TCP or multicasting etc. JGroups will also handle peer-communication, e.g. sending messages to peer nodes which have not received the message yet etc.
精彩评论