Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C#__how can I receive packet from ethernet in the form of byte[] using Pcap.net

Status
Not open for further replies.

rkt_167

Junior Member level 1
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 am building a GUI for packet capturing through Ethernet interface.
i am using this code for selecting interface.

Ques: The packet which i am getting how can i pass it in the form of byte array. pls give syntax..
actually i want to pass the received packet in the form of bytes through a function for its further processing.

Code:
// Retrieve the device list from the local machine
            IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;

            if (allDevices.Count == 0)
            {
                MessageBox.Show("Select an Interface to capture the packets.", "**ERROR**",
                   MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Print the list
            for (int i = 0; i != allDevices.Count; ++i)
            {
                LivePacketDevice device = allDevices[i];
                cmbInterfaces.Items.Add(device.Name);
                //Console.Write((i + 1) + ". " + device.Name);
                if (device.Description != null)
                    cmbInterfaces.Items.Add(device.Description);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Retrieve the device list from the local machine
            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 (!int.TryParse(deviceIndexString, out deviceIndex) ||
                    deviceIndex < 1 || deviceIndex > allDevices.Count)
                {
                    deviceIndex = 0;
                }
            };

            // Take the selected adapter
            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
            {
                Console.WriteLine("Listening on " + selectedDevice.Description + "...");

                // Retrieve the packets
                Packet packet;
                do
                {
                    PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
                    switch (result)
                    {
                        case PacketCommunicatorReceiveResult.Timeout:
                            // Timeout elapsed
                            continue;
                        case PacketCommunicatorReceiveResult.Ok:
                            Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" +
                                              packet.Length);
                            break;
                        default:
                            throw new InvalidOperationException("The result " + result + " shoudl never be reached here");
                    }
                } while (true);
 }
 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top