如何将端口IAX2的UDP头转换为可读字符串

How to convert UDP header of port IAX2 to readable string

我正在尝试在 C# 中创建一个读取 IAX2 端口 4569 的 activity 的应用程序。我已经创建了 UDP 和 TCP 侦听器,但是当我尝试将 UDP 数据部分转换为字符串时我发现了一些奇怪的代码。我不知道我做的对不对。我需要一些帮助。 这个class是我得到数据的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);
    }}

接下来我有一个 class 将数据从 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 数据解码字节时做错了什么。

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

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

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