我需要捕获 SNMP 并完成以下代码,我正确获取数据但 objectID 出现乱码,有什么原因吗
int port=162;
UdpClient listener;
IPEndPoint groupEP;
byte[] packet = new byte[1024];
int commlength, miblength, datatype, datalength, datastart, Objecttype, Objectlength;
int objectstart;
string objectid;
string output;
Console.WriteLine("Initializing SNMP Listener on Port:" + port + "...");
// try
// {
listener = new UdpClient(port);
groupEP = new IPEndPoint(IPAddress.Any, port);
while (true)
{
Console.WriteLine("Waiting for messages....");
packet = listener.Receive(ref groupEP);
Console.WriteLine("Processing new message...");
if (packet.Length != null)
{
Console.Out.WriteLine("New message from {0} :\n {1}\n", groupEP.ToString(), packet);
if (packet[0] == 0xff)
{
Console.WriteLine("Invalid Packet");
return;
}
commlength = Convert.ToInt16(packet[6]);
miblength = Convert.ToInt16(packet[10 + commlength]);
Objecttype = Convert.ToInt16(packet[30 + commlength + miblength]);
Objectlength = Convert.ToInt16(packet[31 + commlength + miblength]);
objectstart = 32 + commlength + miblength;
datatype = Convert.ToInt16(packet[26+ Objecttype + Objectlength+commlength+ miblength]);
datalength = Convert.ToInt16(packet[27 + Objecttype + Objectlength + commlength + miblength]);
datastart = 28 + Objecttype + Objectlength + commlength + miblength;
output = Encoding.ASCII.GetString(packet, datastart, datalength);
objectid = Encoding.ASCII.GetString(packet, objectstart , Objectlength);
Console.WriteLine("sysLocation - Datatype: {0}, Value: {1}", datatype,output);
Console.WriteLine(objectid);
}
}
数据存储为十六进制字符串,objectid 的存储方式是否与包含“。”的方式相同 我知道我可以使用现有的库(可在网上免费获得),但我刚刚为自己创造了一个学习的机会。
请指教。