在 Java 中为我的应用程序创建数据包标头

发布于 2024-08-20 07:35:32 字数 198 浏览 8 评论 0原文

我有一个简单的文件共享应用程序。 用户通过大小为 1024 KB 的数据包相互发送数据。 一切都很完美,但我接下来需要的是每个数据包的标头。 我不完全确定我理解标头的概念,但我相信它应该包含以下信息:拥有该文件的用户的用户名、文件的大小、文件的名称,以便正在下载的用户该文件可以确定他正在下载正确的文件。

任何示例或链接都将受到欢迎,因为这是我第一次处理此类事情。

I have a simple file sharing application.
Users are sending data among each other via packets of size 1024 KB.
Everything works perfectly, but what I need next is header for each packet.
I'm not completely sure that I understand the concept of a header, but I believe it should contain information such as: username of the user that has the file, size of the file, name of the file so that the user that is downloading the file can be sure he is downloading the right file.

Any example or link would be most welcome as this is the first time I'm dealing with this type of thing.

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

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

发布评论

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

评论(1

梦归所梦 2024-08-27 07:35:32

标头的目的是传达有关标头后面的包的信息。为了便于讨论,请考虑一个文件传输应用程序,例如您正在开发的应用程序,其中可以将一个文件同时从多个用户传输到需要该文件的用户。如果每个发送方发送一个包含文件一部分的数据包,您将需要某种形式的信息来了解这是哪一部分,例如序列号或文件中的偏移量,以便接收方能够将各个部分组合到一起实际文件。

为了方便起见,您可以查看 Google Protocol 缓冲区,它允许您可以指定组成消息的字段以及序列化和反序列化它们。

例如,您可以在 TCP 协议 中找到要放入标头的示例,尽管您不应该认为您需要该标头或类似内容中的所有内容。不过,查看其他协议是一种很好的学习方法。

The purpose of a header is to convey information about the package that follows the header. For the sake of argument, consider a file transfer application such as the one you are developing, where for instance a file can be transferred from several users at the same time to a user that wants the file. If each sender sends a packet containing a part of the file, you will need some form of information about which part this is - for instance a sequence number or an offset into the file so that the receiver is able to put the pieces together to the actual file.

To make it easy for yourself, you can take a look at Google Protocol buffers, which allows you to specify what fields make up the messages as well as serialize and deserialize them.

You can find examples of things to put in a header in the TCP protocol for instance, although you shouldn't think that you need everything that is in that header or something like that. Looking at other protocols are a good way to learn though.

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