2016-08-05 25 views
1

我想在C#中創建一個讀取IAX2端口4569活動的應用程序。我已經創建了UDP和TCP偵聽器,但是當我嘗試將UDP數據部分轉換爲字符串我發現了一些奇怪的代碼。我不知道我是否做得對。一些幫助我需要這個。 這個類是我獲取數據的UDPHeader。如何將UDP端口IAX2轉換爲可讀字符串

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(); 
} 

艾琳的Console.WriteLine命令是這樣的: Console log for aLine of stringReader

我需要知道我做錯了從IAX2 UDP數據字節進行解碼。

回答

1

由於這個協議不是一個文本可讀的協議,只是將它解析爲一個UTF8字符串會給你意想不到的結果。

您應該閱讀協議描述(例如[https://tools.ietf.org/html/rfc5456]))並根據此描述解析數據。

要開始,你可以打印數據字節作爲十六進制代碼。

+0

謝謝,我讀了協議並修改了我的代碼。 IAX2標題數據以二進制形式加密。我仍在研究加密,但我有一些我需要的數據。再次感謝。 – Lichblitz