SharpPcap.PcapOfflineDevice.Open C# (CSharp) Method

Open() public method

Opens the device for capture
public Open ( ) : void
return void
        public override void Open()
        {
            //holds errors
            StringBuilder errbuf = new StringBuilder( Pcap.PCAP_ERRBUF_SIZE ); //will hold errors
            //opens offline pcap file
            IntPtr adapterHandle = SafeNativeMethods.pcap_open_offline( this.Name, errbuf);

            //handle error
            if ( adapterHandle == IntPtr.Zero)
            {
                string err = "Unable to open offline adapter: " + errbuf.ToString();
                throw new Exception( err );
            }

            //set the local handle
            this.PcapHandle = adapterHandle;
        }

Same methods

PcapOfflineDevice::Open ( bool promiscuous_mode ) : void
PcapOfflineDevice::Open ( bool promiscuous_mode, int read_timeout ) : void

Usage Example

        private void SendCaptureFile()
        {
            if (!ValidateUserInput())
            {
                return;
            }

            // Get the network device through which the packets will be sent.
            PcapDevice device = chooseDeviceUserControl.Device;

            // Get an offline file pcap device
            PcapDevice offlineDevice = new PcapOfflineDevice(textBoxCaptureFile.Text);

            PcapSendQueue sendQueue = null;

            bool isDeviceOpenedInThisForm = false;

            try
            {
                Cursor = Cursors.WaitCursor;

                if (!device.Opened)
                {
                    device.Open();
                    isDeviceOpenedInThisForm = true;
                }

                // Open the device for capturing
                offlineDevice.Open();

                // Allocate a new send queue
                sendQueue = new PcapSendQueue
                    ((int)((PcapOfflineDevice)offlineDevice).FileSize);

                Packet packet;

                // Go through all packets in the file and add to the queue
                while ((packet = offlineDevice.GetNextPacket()) != null)
                {
                    if (!sendQueue.Add(packet))
                    {
                        MessageBox.Show(
                            "Packet buffer too small, not all the packets will be sent.",
                            "Warning",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Warning);

                        break;
                    }
                }

                if (device.PcapDataLink != offlineDevice.PcapDataLink)
                {
                    DialogResult answer = MessageBox.Show(
                        "The datalink of the capture differs from the one of the" +
                        "\nselected interface. Do you want to continue?",
                        "Question",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question);

                    if (answer == DialogResult.No)
                    {
                        return;
                    }
                }

                int bytesSent = device.SendQueue(sendQueue, false);

                if (bytesSent < sendQueue.CurrentLength)
                {
                    MessageBox.Show(
                        String.Format("An error occurred sending the packets: {0}.\nOnly {1} bytes were sent.", device.LastError, bytesSent),
                        "Error",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // Close the pcap device
                offlineDevice.Close();

                // Free the queue
                if (sendQueue != null)
                {
                    sendQueue.Dispose();
                }

                if (isDeviceOpenedInThisForm)
                {
                    device.Close();
                }

                Cursor = Cursors.Default;
            }
        }
All Usage Examples Of SharpPcap.PcapOfflineDevice::Open