1

I am using Pcap.Net take Pcap File and transmit all it's packet through my machine Network Adapter. So in order to do that i am using the code example 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();
            }
        }
    }

In order to send packet Pcap.Net offers 2 ways:

  1. Send buffer.

  2. Send each packet using SendPacket().

Now after finish to send my 2 files (like in my example) i want to use the Dispose() to free resources.

when using the first option all works fine and this finish to handle my 2 Pcap files.

When using the second option SendPacket() (currently in my code example this is as a comments) after the first file finish my application is closing and not reach to the second file. I try it also in Console Application and in WPF and in both cases same result. With UI (WPF) my application GUI just close without any error.

Any suggestions what could cause this ?

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
berry wer
  • 637
  • 2
  • 12
  • 25
  • 1
    You don't need to explicitly call `dispose()` because you are using the `using` statement. [`using` documentation](https://msdn.microsoft.com/en-us/library/yh598w02.aspx) – Nasreddine Aug 11 '15 at 07:09
  • When play a lot of files in endless lop i notice that my memory is increment, at the start my application memory usage is about 70MB and after a night this usage is 180 MB so i wonder if i need to implement here Dispose method, i know that my SendPackets method is doing the work and i don't have any other things that can be the root of this memory lack, is it possible that the open file that i am dong over and over responsible for this ? – berry wer Aug 11 '15 at 11:25

1 Answers1

0

When you use the using keyword it means you implicitly call Dispose() at the end of the scope.

If you call Dispose() explicitly as well, it means you call Dispose() twice on the same instance, which is likely to crash your program.

brickner
  • 6,595
  • 3
  • 41
  • 54