简体   繁体   English

通过UDP发送RMI存根

[英]Sending RMI stub over UDP

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. 我希望通过UDP发送RMI存根,只有我不知道如何在发送方上创建新的DatagramPacket并从接收方从DatagramPacket.getData()返回的数组中重构存根。 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? 您为什么不按设计人员的意图使用RMI Registry? Or RMI itself? 还是RMI本身? You don't need UDP here. 您在这里不需要UDP。

NB RMI stubs are indeed serializable, which is the foundation of the Registry. NB RMI存根确实是可序列化的,这是注册表的基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM