C# 网络编码

发布于 2024-08-08 22:59:05 字数 797 浏览 3 评论 0 原文

我正在使用 C# 开发一个网络应用程序,通过网络发送大量纯数字。我发现了 IPAddress.HostToNetworkOrderIPAddress.NetworkToHostOrder 方法,它们非常有用,但它们给我留下了几个问题:

  1. 我知道我需要编码和解码整数,无符号整数怎么样?我想是的,所以目前我正在通过将指向无符号 int 的指针转换为指向 int 的指针,然后进行网络转换来实现这一点int(因为没有采用无符号整数的方法重载)

    公共静态 UInt64 HostToNetworkOrder(UInt64 i)
    {
        Int64 a = *((Int64*)&i);
        a = IPAddress.HostToNetworkOrder(a);
        返回 *((UInt64*)&a);
    }
    
    公共静态 UInt64 NetworkToHostOrder(UInt64 a)
    {
        Int64 i = *((Int64*)&a);
        i = IPAddress.HostToNetworkOrder(i);
        返回 *((UInt64*)&i);
    }
    

    2。 浮点数(单精度和双精度)怎么样?。我认为不,但是如果我确实需要,我应该对无符号整数执行类似的方法并将单个指针转换为 int 指针并像这样进行转换吗?

编辑:: 乔恩的答案没有回答后半部分的问题(它也没有真正回答第一部分!),我希望有人回答第二部分

I'm working on a networking application in C#, sending a lot of plain numbers across the network. I discovered the IPAddress.HostToNetworkOrder and IPAddress.NetworkToHostOrder methods, which are very useful, but they left me with a few questions:

  1. I know I need to encode and decode integers, what about unsigned ones? I think yes, so at the moment I'm doing it by casting a pointer to the unsigned int into a pointer to an int, and then doing a network conversion for the int (since there is no method overload that takes unsigned ints)

    public static UInt64 HostToNetworkOrder(UInt64 i)
    {
        Int64 a = *((Int64*)&i);
        a = IPAddress.HostToNetworkOrder(a);
        return *((UInt64*)&a);
    }
    
    public static UInt64 NetworkToHostOrder(UInt64 a)
    {
        Int64 i = *((Int64*)&a);
        i = IPAddress.HostToNetworkOrder(i);
        return *((UInt64*)&i);
    }
    

    2. What about floating point numbers (single and double). I think no, however If I do need to should I do a similar method to the unsigned ints and cast a single pointer into a int pointer and convert like so?

EDIT:: Jons answer doesn't answer the second half of the question (it doesn't really answer the first either!), I would appreciate someone answering part 2

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

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

发布评论

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

评论(2

水溶 2024-08-15 22:59:05

我怀疑您会发现在 EndianBinaryReader 和 EndianBinaryWriter 会更容易="nofollow noreferrer">MiscUtil - 然后你可以自己决定字节序。或者,对于单个值,您可以使用 EndianBitConverter

I suspect you'd find it easier to use my EndianBinaryReader and EndianBinaryWriter in MiscUtil - then you can decide the endianness yourself. Alternatively, for individual values, you can use EndianBitConverter.

羅雙樹 2024-08-15 22:59:05

您最好阅读几篇 RFC 文档,了解不同的 TCP/IP 协议(​​应用程序级别,例如 HTTP/FTP/SNMP 等)有何不同。

一般来说,这是一个特定于协议的问题(都是您的问题),因为您的数据包必须以协议定义的格式封装整数或浮点数。

对于 SNMP,这是一种将整数/浮点数更改为几个字节然后再更改回来的转换。使用 ASN.1。

http://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One

You'd better read several RFC documents to see how different TCP/IP protocols (application level, for example, HTTP/FTP/SNMP and so on).

This is generally speaking, a protocol specific question (both your questions), as your packet must encapsulate the integers or floating point number in a protocol defined format.

For SNMP, this is a conversion that changing an integer/float number to a few bytes and changing it back. ASN.1 is used.

http://en.wikipedia.org/wiki/Abstract_Syntax_Notation_One

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