C# How to stop capturing Packets Using Pcap.net

Status
Not open for further replies.

rkt_167

Junior Member level 1
Joined
Aug 9, 2013
Messages
19
Helped
3
Reputation
6
Reaction score
3
Trophy points
3
Visit site
Activity points
163
hi all,
I have build a GUI to capture packet through Ethernet and to implement this I have used Pcap.net libraries but If capturing starts it goes on until and unless I stop debugging.
Question:
So I want to stop this during the execution of program by using a button and also I should be able to start it again??
I want to tell that I searched and I found that we can do that by using communicator.break(); but HOW??

Here is My code:

Code C# - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
            int deviceIndex = 0;
            while (deviceIndex == 0)
            {
                //Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
                string deviceIndexString = box.Text;
                if ((cmbInterfaces.Text == "") || (deviceIndexString == ""))
                {
                    MessageBox.Show("Select an Interface to capture the packets AND Enter its Number", "**PLEASE ENTER**",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (!int.TryParse(deviceIndexString, out deviceIndex) ||
                    deviceIndex < 1 || deviceIndex > allDevices.Count)
                {
                    deviceIndex = 0;
                }
            };
            // Take the selected adapter
            // Retrieve the device list from the local machine
            PacketDevice selectedDevice = allDevices[deviceIndex - 1];
 
            // Open the device
            using (PacketCommunicator communicator =
                selectedDevice.Open(65536,                                  // portion of the packet to capture
                // 65536 guarantees that the whole packet will be captured on all the link layers
                                    PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                    1000))                                   // read timeout
            {              
                    // Check the link layer. We support only Ethernet for simplicity.
                    {
                        if (communicator.DataLink.Kind != DataLinkKind.Ethernet)
                        {
                            Console.WriteLine("This program works only on Ethernet networks.");
                            return;
                        }
                    }
                    // Compile the filter
                    //                    using (BerkeleyPacketFilter filter = communicator.CreateFilter("ip and tcp"))
                    //                  {
                    // Set the filter
                    //                    communicator.SetFilter(filter);
                    //             }
 
                    Console.WriteLine("Listening on " + selectedDevice.Description + "...");
                    // start the capture
                    communicator.ReceivePackets(0, PacketHandler);



thanks
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…