代码之家  ›  专栏  ›  技术社区  ›  berry wer

Pcap。Net PacketCommunicator。Dispose()在没有任何错误的情况下关闭我的应用程序

  •  1
  • berry wer  · 技术社区  · 10 年前

    我正在使用 Pcap.Net Pcap 通过我的机器归档并传输所有数据包 Network Adapter . 为了做到这一点,我使用了代码示例 Sending packets using Send Buffer :

    class Program
        {
            static void Main(string[] args)
            {
                string file = @"C:\file_1.pcap";
                string file2 = @"C:\file_2.pcap";
                // Retrieve the device list from the local machine
                IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
    
                // Take the selected adapter
                PacketDevice selectedOutputDevice = allDevices[1];
    
                SendPackets(selectedOutputDevice, file);
                SendPackets(selectedOutputDevice, file2);
            }
    
            static void SendPackets(PacketDevice selectedOutputDevice, string file)
            {
                // Retrieve the length of the capture file
                long capLength = new FileInfo(file).Length;
    
                // Chek if the timestamps must be respected
                bool isSync = false;
    
                // Open the capture file
                OfflinePacketDevice selectedInputDevice = new OfflinePacketDevice(file);
    
                using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
                {
                    using (PacketCommunicator outputCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
                    {
                        // Allocate a send buffer
                        using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength))
                        {
                             // Fill the buffer with the packets from the file
                            Packet packet;
                            while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok)
                            {
                                //outputCommunicator.SendPacket(packet);
                                sendBuffer.Enqueue(packet);
                            }
    
                            // Transmit the queue
                            outputCommunicator.Transmit(sendBuffer, isSync);                        
                            inputCommunicator.Dispose();
                        }
    
                        outputCommunicator.Dispose();
                    }
    
                    //inputCommunicator.Dispose();
                }
            }
        }
    

    为了发送数据包 Pcap.净 提供两种方式:

    1. 发送缓冲区。

    2. 使用发送每个数据包 SendPacket() .

    现在,在完成发送我的2个文件(如我的示例)后,我想使用 Dispose() 以释放资源。

    当使用第一个选项时,一切都很好,这一完成可以处理我的2 Pcap公司 文件夹。

    使用第二个选项时 发送数据包() (目前在我的代码示例中,这是一个注释)在第一个文件完成后,我的应用程序将关闭,无法访问第二个文件。 我也尝试过 Console Application WPF 并且在两种情况下结果相同。 具有 UI (WPF)我的应用程序 GUI 只需关闭,无任何错误。

    有什么建议可以导致这种情况?

    1 回复  |  直到 10 年前
        1
  •  0
  •   brickner    10 年前

    当您使用 using 关键字,表示您隐式调用 Dispose() 在范围的末尾。

    如果你打电话 处置() 也明确地说,这意味着你打电话 处置() 在同一实例上执行两次,这可能会导致程序崩溃。