使用java通过UDP发送/接收不同的对象
我正在用 Java 编写一个程序,其中两台或多台机器之间使用 UDP 进行通信。我的应用程序将对象序列化后通过网络发送到另一台机器,在那里它将被反序列化并处理。到目前为止我已经成功发送了一种对象。
我的问题是我希望发送者能够发送不同类型的对象,并且接收者能够接收它们并将它们再次转换为适当的类型。然而,由于UDP分配一个字节缓冲区然后将数据接收到缓冲区中,因此不可能强制转换或检测接收到的对象的类型,因为不同的对象具有不同的大小。
有没有一种方法可以让我使用 UDP 发送不同类型的对象,然后在另一端接收它们? (我在这里不要求代码,只是一些想法)
谢谢
编辑: 我正在寻找发送/接收不同对象类型的最佳方法,而不知道下一个预期对象的类型是什么。假设我有三种对象类型,我希望在任何给定时间收到其中任何一种。 在 Brian 发表评论后,我想到的另一件事是:如何为可变大小的数据类型(如字符串、数组等)设置缓冲区大小。当接收 UDP 数据包时,您必须首先分配一个大小的缓冲区来接收该对象。这在某种程度上与我原来的问题有关。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您序列化的内容(使用 ObjectOutputStream)也可以反序列化(使用 ObjectInputStream)。然后你检索一个对象。如果您通过 UDP、TCP、管道、本地文件或其他方式传输它,它就不起作用。序列化写入OutputStream,反序列化从InputStream读取。
您 myObject.getClass() 来检测您收到的内容。
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.
为什么不将它们包装在具有
Object
类型有效负载的已知类型(Packet
)中?然后,您可以反序列化您的Packet
并询问生成的有效负载的类型。Why not wrap them in a known type (a
Packet
) that has a payload of typeObject
? You can then deserialise yourPacket
and interrogate the resultant payload for its type.基本上,它会反序列化对象,然后执行
instanceof
检查。然而,由于您在使用 UDP 时明确提到了字节缓冲区的问题,我建议看看一个消息传递库,它已经执行了这种字节缓冲区的管理:JGroups在伪代码中,使用 jgroups 看起来像这样:
在接收端
JGroups 自动执行消息传递通道的抽象,您可以这样做不需要知道这是UDP、TCP还是多播等。JGroups还将处理对等通信,例如向尚未收到消息的对等节点发送消息等。
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: JGroupsIn pseudo code, working with jgroups looks like this:
And on the receiving end
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.