将数据从 32 位机器传输到 64 位机器,反之亦然

发布于 2024-09-27 05:27:23 字数 112 浏览 0 评论 0原文

数据通过 IP 从 32 位机器传输到 64 位机器(反之亦然)。由于数据将以网络字节顺序传输,因此传输过程中不会出现问题。 问:数据到达接收端会不会出现问题? 如果出现问题,应该注意什么才能避免出现问题?

data is transfered over IP from a 32-bit machine to a 64-bit machine (or vice versa). Since the data will be transfered in network byte order there will be no problem during the transfer.
Question: Will there be any problem when the data reaches the receiving end?
What should be taken care of to avoid any problems, if there are any?

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

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

发布评论

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

评论(3

笑着哭最痛 2024-10-04 05:27:23

我觉得前面两位答主都误解了这个问题。 OP 的评论表明他们正在将一个long从 LP64 系统传输到 ILP32 系统,大概都是 x86。

在 LP64 x86 系统上,long 基元类型为 64 位宽,最低有效字节在前。在 ILP32 x86 系统上,相同的 long 基元类型为 32 位宽,最低有效字节在前。

如果您天真地使用long 数据类型进行编码并来回传输数字,就会出现问题。您可能正在 x86_64 开发 PC 上进行调试,并且一切似乎都正常(使用 memcpy() 处理所有内容),但它在 i386 系统上以神秘的方式中断,因为您试图将 8 字节压缩到 4 字节

这就是 C99 标准库中引入 stdint.h 的原因。它允许您通过指定整数的精确宽度来编写更可移植的代码。如果您想要 64 位宽的无符号整数,请使用 uint64_t。此类型在 ILP32 和 LP64 x86 系统中保持不变。同样,如果您想要 32 位宽的有符号整数,请使用 int32_t。有关该主题的更多信息,请参阅维基百科上的这篇文章。

I think the question has been misunderstood by the previous two answerers. The OP's comments indicate that they are transferring a long from an LP64 system to an ILP32 system, presumably both x86.

On an LP64 x86 system, the long primitive type is 64 bits wide, least significant byte first. On an ILP32 x86 system, the same long primitive type is 32 bits wide, least significant byte first.

Problems will arise if you naively code with the long datatype and transfer numbers to and fro. You might be debugging on your x86_64 development PC and everything seems to work (with you memcpy()ing everything) and it breaks in mysterious ways on an i386 system because you're trying to squeeze 8 bytes into a 4-byte long.

This is why stdint.h was introduced in the C99 standard library. It allows you to write more portable code by specifying precise widths for your integers. If you want a 64-bit wide unsigned integer, use uint64_t. This type is invariant across ILP32 and LP64 x86 systems. Likewise, if you want a 32-bit wide signed integer, use int32_t. See this article on Wikipedia for much more information on the topic.

梦旅人picnic 2024-10-04 05:27:23

在你所说的抽象层次上,应该没有问题。 32 vs 64 不会影响套接字级别的网络通信。

At the level of abstraction you are talking about, there should be no problem. 32 vs 64 will not affect network communication at the socket level.

诗酒趁年少 2024-10-04 05:27:23

不是问题...数据通常以字节形式存储(和传输),其大小通常为 8 位,并且不依赖于体系结构是 32 位还是 64 位。

系统规范的“32 位”或“64 位”部分通常指两件事:地址的大小(用于指向内存的指针)和寄存器的大小(用于 CPU 上的计算)。这对实际数据没有影响,无论是存储在磁盘上还是通过网络发送。

Not an issue... data is generally stored (and transfered) as bytes, the size of which is generally 8 bits, and is not dependent on whether the architecture is 32-bit or 64-bit.

The "32-bit" or "64-bit" part of a system specification generally refers to two things: the size of is addresses (for pointers to memory) and the size of its registers (for on-CPU computation). This has no impact on actual data, whether stored on disk or sent over the network.

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