I wish to send RMI stubs over UDP, only I have no idea how to create a new DatagramPacket on the sender and reconstitute the stub from the array returned from DatagramPacket.getData() on the receiver. How, for instance, can I reliably calculate the size of the packet?
Can anyone please help me out?
Thanks,
Owen.
You can do it as follows:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(stub);
oos.close();
byte[] bytes = baos.toByteArray();
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, target, port);
// etc ... send this datagram
and at the receiver, the converse of this process, which I will leave as an exercise for the reader.
But my first question is 'why?' Why aren't you using the RMI Registry as the designers intended? Or RMI itself? You don't need UDP here.
NB RMI stubs are indeed serializable, which is the foundation of the Registry.