使用java通过UDP发送/接收不同的对象

发布于 2024-09-03 14:13:38 字数 515 浏览 3 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最单纯的乌龟 2024-09-10 14:13:38

您序列化的内容(使用 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.

倦话 2024-09-10 14:13:38

为什么不将它们包装在具有 Object 类型有效负载的已知类型(Packet)中?然后,您可以反序列化您的Packet并询问生成的有效负载的类型。

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.

琉璃繁缕 2024-09-10 14:13:38

基本上,它会反序列化对象,然后执行 instanceof 检查。然而,由于您在使用 UDP 时明确提到了字节缓冲区的问题,我建议看看一个消息传递库,它已经执行了这种字节缓冲区的管理:JGroups

在伪代码中,使用 jgroups 看起来像这样:

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));

在接收端

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 自动执行消息传递通道的抽象,您可以这样做不需要知道这是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: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文