コード例 #1
0
ファイル: MainForm.cs プロジェクト: guohengkai687/RTPDemo
        private void btn_Monitor_Click(object sender, EventArgs e)
        {
            btn_Monitor.Text = btn_Monitor.Text == "开启监控" ? "关闭监控" : "开启监控";

            ICaptureDevice dev = _devices.FirstOrDefault(x => x.ToString() == cb_NetworkCard.Text.ToString());

            if (dev != null)
            {
                int readTimeoutMilliseconds = 1000;
                if (btn_Monitor.Text == "关闭监控")
                {
                    dev.OnPacketArrival += new PacketArrivalEventHandler(device_OnPacketArrival);
                    if (dev is SharpPcap.WinPcap.WinPcapDevice)
                    {
                        var winPcap = dev as SharpPcap.WinPcap.WinPcapDevice;
                        winPcap.Open(SharpPcap.WinPcap.OpenFlags.DataTransferUdp | SharpPcap.WinPcap.OpenFlags.NoCaptureLocal, readTimeoutMilliseconds);
                        //winPcap.Open(SharpPcap.WinPcap.OpenFlags.Promiscuous, readTimeoutMilliseconds);
                    }
                    if (dev is SharpPcap.LibPcap.CaptureFileReaderDevice)
                    {
                        var reader = dev as SharpPcap.LibPcap.CaptureFileReaderDevice;
                        reader.Open();
                    }
                    // Start the capturing process
                    dev.StartCapture();
                }
                else
                {
                    dev.OnPacketArrival -= new PacketArrivalEventHandler(device_OnPacketArrival);
                    if (dev is SharpPcap.WinPcap.WinPcapDevice)
                    {
                        var winPcap = dev as SharpPcap.WinPcap.WinPcapDevice;
                        winPcap.Close();
                    }
                    if (dev is SharpPcap.LibPcap.CaptureFileReaderDevice)
                    {
                        var reader = dev as SharpPcap.LibPcap.CaptureFileReaderDevice;
                        reader.Close();
                    }
                    // Stop the capturing process
                    dev.StopCapture();
                }
            }
        }