首页 > 解决方案 > 使用 Pcap.net 获取采样周期内流量最大的网络接口

问题描述

我正在尝试使用 Pcap.net 包来分析在一个小样本周期(例如 10 秒)内跨多个网络接口的每秒流量(以比特为单位)。然后我想从结果中计算每秒总字节数的平均值,然后通过突出显示关联的 listBox 项来指示哪个接口的流量最大。

我正在使用包 github 中的这个示例:Statistics Example

我需要同时为每个接口执行此操作,因此我启动了一个方法,该方法最终PacketSampleStatistics在每个接口的单独线程中调用该类。

我的问题是,一旦我在使用PacketSampleStatistics类的方法中,我不知道如何通过它的索引来识别接口,因此我不能将它绑定到列表框项。

我填充列表框并为每个界面启动一个新线程,如下所示:

public Form1()
{
    InitializeComponent();
    // Retrieve the device list from the local machine

    IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;

    if (allDevices.Count == 0)
    {
        Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
        return;
    }

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


    foreach (var netInts in listBox1.Items)
    {
        int curIndex = 1 + listBox1.Items.IndexOf(netInts);
        Console.WriteLine(curIndex);
        Thread getStats = new Thread(() => GetNetStatistics(curIndex));
        Console.WriteLine("Interface index: " + curIndex);
        getStats.Start();
    }
}

这会为每个网络接口创建一个新线程,并通过启动一个名为GetNetStatistics. 然后,此方法开始StatisticsHandler使用PacketSampleStatistics该类将每个接口的每秒位数写入其自己的线程中的控制台,该线程正在工作。

我尝试调用一个委托来更新我的主窗体上的对象,如果我一次只为一个网络接口/一个线程执行此操作,则此方法有效。但我需要能够同时为每个网络接口执行此操作。

我在课程末尾注释掉的代码PacketSampleStatistics显示了我试图解决这个问题的尝试。

private void StatisticsHandler(PacketSampleStatistics statistics)
{

    // Current sample time
    DateTime currentTimestamp = statistics.Timestamp;

    // Previous sample time
    DateTime previousTimestamp = _lastTimestamp;

    // Set _lastTimestamp for the next iteration
    _lastTimestamp = currentTimestamp;

    // If there wasn't a previous sample than skip this iteration (it's the first iteration)
    if (previousTimestamp == DateTime.MinValue)
        return;

    // Calculate the delay from the last sample
    double delayInSeconds = (currentTimestamp - previousTimestamp).TotalSeconds;

    // Calculate bits per second
    double bitsPerSecond = Math.Truncate(statistics.AcceptedBytes * 8 / delayInSeconds);

    // Calculate packets per second
    double packetsPerSecond = statistics.AcceptedPackets / delayInSeconds;

    // Print timestamp and samples
    Console.WriteLine(statistics.Timestamp + " BPS: " + bitsPerSecond + " PPS: " + packetsPerSecond);

    //invoke delegate to update main form
    //MethodInvoker inv = delegate
    //{
        //bytesSecond.Text = bitsPerSecond.ToString();
        //listBox1.Items[0] = listBox1.Items[0] + bitsPerSecond.ToString();
    //};
    //this.Invoke(inv);

}

标签: c#pcap.net

解决方案


似乎处理的方法PacketSampleStatistics应该是使用接口 id 初始化的类的成员。


推荐阅读