c# - 使用 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);
}
解决方案
似乎处理的方法PacketSampleStatistics
应该是使用接口 id 初始化的类的成员。
推荐阅读
- python - 如何让 conda 知道我的包的数据和配置文件在哪里
- php - PHP循环遍历多维数组
- api - Hotstar API 使用有状态架构吗?即使没有登录,它如何跟踪用户?
- python - 在递归中使用全局变量
- javascript - 用JS拖拽后显示图片
- python - OpenVino:如何在模型优化器中添加对 FusedBatchNormV3 的支持?
- scrollview - 滚动视图中的内容作为列表项在滚动(swiftui)时消失,为什么?
- mysql - 在 Laravel Eloquent 中使用 MySQL PASSWORD()
- c - C:文件存在但打不开。数字母?
- python - pygame定义不同的位置顺序产生不同的结果