【问题标题】:SharpPcap SendQueue does not work as in the exampleSharpPcap SendQueue 不像示例中那样工作
【发布时间】:2013-04-16 14:11:07
【问题描述】:

我尝试使用 SharpPcap 并使用 SharpPcap 中的示例从 pcap 文件 (libpcap) 发送数据包:http://sourceforge.net/p/sharppcap/code/ci/SharpPcap_4_1_0/tree/Examples/Example10.SendQueue/Example10.SendQueues.cs

我的问题在第 127 行 - int sent = winPcapDevice.SendQueue(squeue, SharpPcap.WinPcap.SendQueueTransmitModes.Synchronized); 中,当尝试执行此命令时,我收到异常 对象引用未设置为对象实例。 我已经用调试器逐行检查,似乎一切正常,直到这个命令,我的Pcap文件OK,所有数据包都放在Queue

【问题讨论】:

    标签: c#


    【解决方案1】:

    在调试器中检查 winPcapDevice 的值。如果它为空,那么这是你的问题。否则请发布堆栈跟踪,可能是从SendQueue 内部抛出异常。

    要找出 winPcapDevice 为何显示为空,请在调试器中跟踪您期望在此变量中结束的实例的生命周期:device 变量,然后是 devices 数组。单步执行代码时检查其中的内容。

    如果device 不为空但运行以下代码行:

    var winPcapDevice = device as WinPcapDevice;
    

    导致winPcapDevice 为空,这意味着device 所持有的实例无法转换为WinPcapDevice 类型。基本上它属于不同的类型,也不是从WinPcapDevice 继承的,请参阅this msdn article 以了解“as”运算符的工作原理。

    【讨论】:

    • @user2214609 我已添加到答案中
    • 我的问题出在这一行:var winPcapDevice = device as WinPcapDevice;, device not null but var is null
    • @user2214609 那么类型不是预期的。有关“as”运算符如何工作的说明,请参阅下面的链接。 msdn.microsoft.com/en-us/library/cscsdfbt%28v=vs.110%29.aspx
    • 我认为应该是 WinPcapDevice 类型,因为 SendQueue 方法引用了这种类型
    猜你喜欢
    • 2015-05-09
    • 2021-07-14
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多