C# Pcap.Net PacketCommunicator.Dispose()关闭我的应用程序,无任何错误
我正在使用C# Pcap.Net PacketCommunicator.Dispose()关闭我的应用程序,无任何错误,c#,pcap.net,C#,Pcap.net,我正在使用Pcap.Net获取Pcap文件,并通过我的机器网络适配器传输所有数据包。 因此,为了做到这一点,我使用代码示例: 类程序 { 静态void Main(字符串[]参数) { 字符串文件=@“C:\file_1.pcap”; 字符串file2=@“C:\file_2.pcap”; //从本地计算机检索设备列表 IList allDevices=LivePacketDevice.AllLocalMachine; //使用选定的适配器 PacketDevice selectedOutputD
Pcap.Net
获取Pcap
文件,并通过我的机器网络适配器
传输所有数据包。
因此,为了做到这一点,我使用代码示例:
类程序
{
静态void Main(字符串[]参数)
{
字符串文件=@“C:\file_1.pcap”;
字符串file2=@“C:\file_2.pcap”;
//从本地计算机检索设备列表
IList allDevices=LivePacketDevice.AllLocalMachine;
//使用选定的适配器
PacketDevice selectedOutputDevice=所有设备[1];
发送数据包(选择输出设备、文件);
发送数据包(选择输出设备,文件2);
}
静态无效发送数据包(PacketDevice selectedOutputDevice、字符串文件)
{
//检索捕获文件的长度
long capLength=新文件信息(file).Length;
//检查是否必须遵守时间戳
bool-isSync=false;
//打开捕获文件
OfflinePacketDevice selectedInputDevice=新的OfflinePacketDevice(文件);
使用(PacketCommunicator inputCommunicator=selectedInputDevice.Open(65536,PacketDeviceOpenAttribute.Promiscous,1000))
{
使用(PacketCommunicator outputCommunicator=selectedOutputDevice.Open(100,PacketDeviceOpenAttributes.Promiscuous,1000))
{
//分配发送缓冲区
使用(PacketSendBuffer sendBuffer=新PacketSendBuffer((uint)capLength))
{
//用文件中的数据包填充缓冲区
数据包;
while(inputCommunicator.ReceivePacket(out packet)=PacketCommunicator ReceiveResult.Ok)
{
//outputCommunicator.SendPacket(数据包);
sendBuffer.Enqueue(数据包);
}
//传输队列
outputCommunicator.Transmit(发送缓冲区,isSync);
inputCommunicator.Dispose();
}
outputCommunicator.Dispose();
}
//inputCommunicator.Dispose();
}
}
}
为了发送数据包,Pcap.Net提供了两种方式:
SendPacket()
发送每个数据包Dispose()
释放资源
当使用第一个选项时,所有选项都可以正常工作,并且此完成可以处理我的2Pcap
文件
当在第一个文件完成后使用第二个选项SendPacket()
(目前在我的代码示例中,这是一个注释)时,我的应用程序将关闭,无法到达第二个文件。
我也在控制台应用程序
和WPF
中尝试了它,在这两种情况下都得到了相同的结果。
使用UI
(WPF),我的应用程序GUI
只需关闭,无任何错误
任何可能导致这种情况的建议?当您使用
using
关键字时,意味着您在作用域末尾隐式调用Dispose()
如果同时显式调用
Dispose()
,则表示在同一实例上调用Dispose()
两次,这可能会使程序崩溃。使用using
关键字时,表示在作用域末尾隐式调用Dispose()
如果同时显式调用
Dispose()
,则意味着在同一实例上调用Dispose()
两次,这可能导致程序崩溃。您不需要显式调用Dispose()
,因为您使用的是using
语句。当在无休止的lop中播放大量文件时,我注意到我的内存在增加,开始时我的应用程序内存使用量约为70MB,一晚后这个使用量为180MB,所以我想知道是否需要在这里实现Dispose方法,我知道我的SendPackets方法正在做这项工作,并且我没有任何其他可能是内存不足的根源的东西,是否有可能是我负责的打开的文件一次又一次地对此负责?您不需要显式调用dispose()
,因为您使用的是using
语句。当在无休止的lop中播放大量文件时,我注意到我的内存在增加,开始时我的应用程序内存使用量约为70MB,一晚后这个使用量为180MB,所以我想知道是否需要在这里实现Dispose方法,我知道我的SendPackets方法正在做这项工作,我没有任何其他可能是内存不足的根源的东西,有没有可能是我一次又一次负责的打开文件?
class Program
{
static void Main(string[] args)
{
string file = @"C:\file_1.pcap";
string file2 = @"C:\file_2.pcap";
// Retrieve the device list from the local machine
IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
// Take the selected adapter
PacketDevice selectedOutputDevice = allDevices[1];
SendPackets(selectedOutputDevice, file);
SendPackets(selectedOutputDevice, file2);
}
static void SendPackets(PacketDevice selectedOutputDevice, string file)
{
// Retrieve the length of the capture file
long capLength = new FileInfo(file).Length;
// Chek if the timestamps must be respected
bool isSync = false;
// Open the capture file
OfflinePacketDevice selectedInputDevice = new OfflinePacketDevice(file);
using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
{
using (PacketCommunicator outputCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
{
// Allocate a send buffer
using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength))
{
// Fill the buffer with the packets from the file
Packet packet;
while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok)
{
//outputCommunicator.SendPacket(packet);
sendBuffer.Enqueue(packet);
}
// Transmit the queue
outputCommunicator.Transmit(sendBuffer, isSync);
inputCommunicator.Dispose();
}
outputCommunicator.Dispose();
}
//inputCommunicator.Dispose();
}
}
}