2014-07-05 95 views
0

https://github.com/lextm/sharpsnmplib/blob/master/SharpSnmpLib/IP.csSharpSnmpLib |字节必须包含4或16个元素 - 导致此错误的原因是什么?

System.ArgumentException: bytes must contain 4 or 16 elements 
at Lextm.SharpSnmpLib.IP..ctor(Tuple`2 length, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Stream stream) 
at Lextm.SharpSnmpLib.Sequence..ctor(Tuple`2 length, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Stream stream) 
at Lextm.SharpSnmpLib.Sequence..ctor(Tuple`2 length, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Stream stream) 
at Lextm.SharpSnmpLib.ResponsePdu..ctor(Tuple`2 length, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Stream stream) 
at Lextm.SharpSnmpLib.Sequence..ctor(Tuple`2 length, Stream stream) 
at Lextm.SharpSnmpLib.DataFactory.CreateSnmpData(Int32 type, Stream stream) 
at Lextm.SharpSnmpLib.Messaging.MessageFactory.ParseMessage(Int32 first, Stream stream,   UserRegistry registry) 
at Lextm.SharpSnmpLib.Messaging.MessageFactory.ParseMessages(Byte[] buffer, Int32 index, Int32 length, UserRegistry registry) 
at Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension.GetResponse(ISnmpMessage request, Int32 timeout, IPEndPoint receiver, UserRegistry registry, Socket udpSocket) 
at Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension.GetResponse(ISnmpMessage request, Int32 timeout, IPEndPoint receiver, Socket udpSocket) 
at Lextm.SharpSnmpLib.Messaging.SnmpMessageExtension.GetResponse(ISnmpMessage request, Int32 timeout, IPEndPoint receiver) 
at Lextm.SharpSnmpLib.Messaging.Messenger.BulkHasNext(VersionCode version, IPEndPoint endpoint, OctetString community, Variable seed, Int32 timeout, Int32 maxRepetitions, IList`1& next, IPrivacyProvider privacy, ISnmpMessage& report) 
at Lextm.SharpSnmpLib.Messaging.Messenger.BulkWalk(VersionCode version, IPEndPoint endpoint, OctetString community, ObjectIdentifier table, IList`1 list, Int32 timeout, Int32 maxRepetitions, WalkMode mode, IPrivacyProvider privacy, ISnmpMessage report) 
at Maprinter.snmpWalk..ctor(String IP, String ID, Int32 timeOut) 

我使用的是为了从网络打印机拉一些数据这个库。到目前为止,所有的打印机都能正常工作,大多数打印机都会返回我正在查找的数据。但是,当我得到这个错误,我没有收到打印机的任何东西,那么是什么原因导致了这个错误?

Messenger.BulkWalk(VersionCode.V2, 
            new IPEndPoint(IPAddress.Parse("10.0.0.101"), 161), 
            new OctetString("public"), 
            new ObjectIdentifier("1.3.6.1"), 
            result, 
            timeOut, 
            10, 
            WalkMode.Default, 
            null, 
            null); 
+0

请向我们展示导致异常的代码?更具体地告诉我们你传递给IP类构造函数的参数 –

+0

我直接从打印机获取IP,并更新了问题以显示我的代码。 @YuvalItzchakov –

+0

哪一行导致异常? –

回答

1

的异常可能是由该设备上的SNMP代理谁送一个空IpAddress体引起的(0×40,0×00)。这违反了标准,因为结果应该是Null正文(0x05,0x00)。

IpAddressRFC2578中定义,严格来说是4个字节。这就是为什么#SNMP将针对4进行检查。针对16的检查是针对IPv6地址的,尽管实际上它们应该由自定义约定来支持。

在你的情况下,该选项就可以了,

  • 修复固件,以便正确的身体被发送。
  • 修改#SNMP代码库(MIT/X11许可证)并满足您的需求。
+0

我希望能够使用支持SNMP的每台打印机。修复固件不能解决其他打印机的问题。我清楚地看到你明白应该做什么,我知道你是这个图书馆的开发人员。你能否请求我建议需要做些什么?我应该在库中修改哪些内容以及应该使用哪个文件? @Lex Li –

+0

正如我理解你的答案,我需要允许我的软件从打印机接收null(0x40,0x00)和(0x05,0x00)。所以我找到了这个文件:https:// github。com/lextm/sharpsnmplib/blob/master/SharpSnmpLib/SnmpType.cs#L59它将Null定义为0x05。我怎样才能将Null都定义为0x05和0x40? @Lex Li –

+0

我希望你有时间仔细阅读我输入的每一个单词。很明显,我向你展示了0x40是用于'IpAddress'的。要允许0x40,0x00在没有例外的情况下被接收,你可以简单地注释掉引发异常的行。如果你甚至无法理解,你可能需要学习更多的C#。我无法进一步向你提出建议,因为所有要点都包括在内。如果您确实需要商业咨询服务,请告诉我。 –

相关问题