在 1 个 UDP 数据报中发送多个数据

发布于 2024-07-17 11:47:35 字数 466 浏览 6 评论 0原文

我正在做一个网络编程作业,关于编写一个简单的 IM 系统(非常类似于最简单版本的 Windows Messenger)。

规范指定我必须在单个数据报包中发送超过 4 个数据字段,它们是:

To From Type Message
where type refers to message type, implemented as a user defined enum class.

我想学习如何将所有这些数据打包到单个数据包中。

更新:感谢迄今为止的帮助, 但说我有字符串句子和字符串来自 单独修补数据包的正常方法是,

byte[] sendData = new byte [256]
sendData = sentence.getBytes();

但是我到底如何将“from”字符串与句子字符串一起附加到 sendData ?

i'm working on a network programming assignment about writing a simple IM system (pretty much like the simplest version of windows messenger).

the spec specifies that i must send over 4 fields of data in a single datagram packet, those are:

To From Type Message
where type refers to message type, implemented as a user defined enum class.

I would like to be taught how to pack all these datas into a single packet.

UPDATE: thx for the help so far,
but say i have String sentence and String From
the normal way to patch the packet individually would be

byte[] sendData = new byte [256]
sendData = sentence.getBytes();

but how exactly can i append the "from" string to sendData along with the sentence string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

夕色琉璃 2024-07-24 11:47:35

您可以使用类似的方法发送任何可序列化的对象。

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(objectYouWantToSend);
out.close();
buffer.close();
DatagramPacket packet = new 
    DatagramPacket(buffer.toByteArray(), 
                   buffer.size(), 
                   InetAddress.getByName(...),
                   portNumber);
socket.send(packet);

You can send any Serializable object using something like this.

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer);
out.writeObject(objectYouWantToSend);
out.close();
buffer.close();
DatagramPacket packet = new 
    DatagramPacket(buffer.toByteArray(), 
                   buffer.size(), 
                   InetAddress.getByName(...),
                   portNumber);
socket.send(packet);
白龙吟 2024-07-24 11:47:35

简而言之,您需要做的是:

  1. 创建一个包含 4 个字段(from/to/enum/message)的对象(类)
  2. 并将其序列化。 它必须实现Serialized。 请参阅此处的其他解决方案,了解如何序列化
  3. 转换为字节数组并向下发送套接字(有关详细信息,请参阅其他地方)

在另一端,您将读取此字节流,反序列化并将其转换为上面 1. 中定义的类的实例。

通过创建包含 4 个字段的一个对象并序列化该对象,您可以将所有四个字段一起发送(我的印象是这是一个绊脚石?)。

请注意,数据报将受到网络传输层施加的大小限制,但对于此应用程序,我怀疑这不是问题。

Briefly, what you need to do is:

  1. create an object (class) which contains your 4 fields (from/to/enum/message)
  2. serialise this. It has to implement Serializable. See other solutions here for how to serialise
  3. convert to a byte array and send down the socket (see elsewhere for details)

At the other end you'll read this bytestream, deserialise and cast it to an instance of your class defined in 1. above.

By creating the one object containing the 4 fields and serialising this object, this allows you to send all four fields together (I get the impression that this is the stumbling block?).

Note that datagrams will have size limits imposed by the network transport layers, but for this application I suspect that's not a problem.

猛虎独行 2024-07-24 11:47:35

您只需在将它们传递到网络接口之前附加它们即可。 大致如下:

byte[] buff = new byte[256];
// Add al your fields here to buff.
DatagramPacket packet = new DatagramPacket(buff, buff.length, address, 1234);
socket.send(packet);

如果它们不都是字符串,则需要对它们进行编码。

You simply append them before passing them to the network interface. Something along the lines of:

byte[] buff = new byte[256];
// Add al your fields here to buff.
DatagramPacket packet = new DatagramPacket(buff, buff.length, address, 1234);
socket.send(packet);

If they're not all strings, you'll need to encode them as such.

跨年 2024-07-24 11:47:35

有很多用于编码数据的选项,所有这些选项都可以归结为将四个字段放入一个数据结构中,然后一次性发送。

重要的部分是编码需要显示四个字段中的哪一个位于数据包,否则远端将无法可靠地对其进行解码。

There are plenty of options for encoding the data, all of which come down to putting the four fields into one data structure, and then sending that all in one go.

The important part is that the encoding needs to show which of the four fields is at which point in the packet, otherwise the far end won't be able to decode it reliably.

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