【问题标题】:How to convert UDP header of port IAX2 to readable string如何将端口 IAX2 的 UDP 标头转换为可读字符串
【发布时间】:2016-12-12 09:48:00
【问题描述】:

我正在尝试在 C# 中创建一个读取 IAX2 端口 4569 活动的应用程序。我已经创建了 UDP 和 TCP 侦听器,但是当我尝试将 UDP 数据部分转换为字符串时,我发现了一些奇怪的代码。我不知道我做得对不对。我需要一些帮助。 这个类是我获取数据的UDPHeader。

public class UDPHeader
{
    //UDP header fields
    private ushort usSourcePort;            //Sixteen bits for the source port number        
    private ushort usDestinationPort;       //Sixteen bits for the destination port number
    private ushort usLength;                //Length of the UDP header
    private short sChecksum;                //Sixteen bits for the checksum
                                            //(checksum can be negative so taken as short)              
    //End UDP header fields

    private byte[] byUDPData = new byte[4096];  //Data carried by the UDP packet

    public UDPHeader(byte [] byBuffer, int nReceived)
    {
        MemoryStream memoryStream = new MemoryStream(byBuffer, 0, nReceived);
        BinaryReader binaryReader = new BinaryReader(memoryStream);

        //The first sixteen bits contain the source port
        usSourcePort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the destination port
        usDestinationPort = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the length of the UDP packet
        usLength = (ushort)IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());

        //The next sixteen bits contain the checksum
        sChecksum = IPAddress.NetworkToHostOrder(binaryReader.ReadInt16());            

        //Copy the data carried by the UDP packet into the data buffer
        Array.Copy(byBuffer, 
                   8,               //The UDP header is of 8 bytes so we start copying after it
                   byUDPData, 
                   0, 
                   nReceived - 8);
    }}

接下来我有一个类将数据从 UDPHeader 转换为普通文本。 这是构造函数:

public IAXHeader(byte[] byBuffer, int nReceived)
{ 
MemoryStream memoryStream = new MemoryStream(byBuffer, 0, nReceived);
StringReader stringReader = new   StringReader(Encoding.UTF8.GetString(memoryStream.ToArray()));

/** iterate lines of stringReader **/
string aLine = stringReader.ReadLine();
}

aLine 的 Console.WriteLine 是这样的:

我需要知道我在解码 IAX2 UDP 数据中的字节时做错了什么。

【问题讨论】:

    标签: c# sockets udp voip ports


    【解决方案1】:

    由于该协议不是文本可读的协议,只需将其解析为 UTF8 字符串就会得到意想不到的结果。

    您应该阅读协议说明(例如 [https://www.rfc-editor.org/rfc/rfc5456])并根据此说明解析数据。

    首先,您可以将数据逐字节打印为十六进制代码。

    【讨论】:

    • 谢谢,我阅读了协议并修改了我的代码。 IAX2 标头数据是加密的并且是二进制的。我仍在进行加密,但我有一些我需要的数据。再次感谢。
    猜你喜欢
    • 2016-06-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多