【发布时间】: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 数据中的字节时做错了什么。
【问题讨论】: