C#pcapdotnet ReceivePackets在回调中使用设备

C#pcapdotnet ReceivePackets在回调中使用设备,c#,networking,packet-capture,pcap.net,C#,Networking,Packet Capture,Pcap.net,我正在尝试制作一个程序,该程序将在多个网络设备上接收数据包,并在其他设备上发送数据包(类似于软件集线器)。我正在使用C#和pcapdotnet。此简单方法捕获设备上的通信: public void sniff(LivePacketDevice device) { using(PacketCommunicator comm = device.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))

我正在尝试制作一个程序,该程序将在多个网络设备上接收数据包,并在其他设备上发送数据包(类似于软件集线器)。我正在使用C#和pcapdotnet。此简单方法捕获设备上的通信:

    public void sniff(LivePacketDevice device)
    {
        using(PacketCommunicator comm = device.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
        {
            comm.ReceivePackets(0, printandsend);
        }          
    }
在回调方法“printandsend”中,我希望在每个设备上发送数据包,但数据包来自的设备除外。为了防止将数据包发送到它来自的设备,我需要知道数据包来自哪个设备。问题是我不知道如何将当前使用的设备解析为回调方法。方法之外的变量对我来说不是一个解决方案,因为捕获数据包是在多个设备上同时使用的。标题设备上同时提供si创建线程的“嗅探”方法

使用方法receivePacket有一个可能的解决方法:

Packet packet;
While(true)
{
    comm.ReceivePacket(out packet);
    send(packet,device)                
   //this would send packet on every device except the one which is used as a parameter
}

问题是,我不确定此解决方案是否能够成功接收和发送所有数据包。

我建议使用ReceivePacket(),除非您发现由于使用它而出现实际问题

ReceivePacket()使用简单,适合大多数应用程序

如果仍要使用ReceivePackets(),可以使用回调方法创建一个类,并为每个设备创建该类的实例