C# 重新启动部分代码?SharpPCap的问题
我在玩游戏时运行了一个小网络嗅探器,我试图捕获某些流量,但遇到了一个问题,可能是因为SharpPcap在多个TCP连接中遇到问题(例如,当我打开或关闭第二个客户端时。我假设它可能在其中一个TCP连接关闭时停止跟踪,即使第二个仍在运行) 下面是代码的重要部分,它非常基本,取自另一个项目:C# 重新启动部分代码?SharpPCap的问题,c#,tcp,timer,packet-sniffers,sharppcap,C#,Tcp,Timer,Packet Sniffers,Sharppcap,我在玩游戏时运行了一个小网络嗅探器,我试图捕获某些流量,但遇到了一个问题,可能是因为SharpPcap在多个TCP连接中遇到问题(例如,当我打开或关闭第二个客户端时。我假设它可能在其中一个TCP连接关闭时停止跟踪,即使第二个仍在运行) 下面是代码的重要部分,它非常基本,取自另一个项目: // A few variable used throughout the program private static byte[] EncryptionKey = { XYZ }
// A few variable used throughout the program
private static byte[] EncryptionKey = { XYZ }
private static string filter = { XYZ }
private static int readTimeoutMilliseconds = 1000;
private static int defaultDevice = -1;
private static System.Collections.Generic.List<byte> _incomingBuffer = new System.Collections.Generic.List<byte>();
////////////////////////////////////////////////
static void Main(string[] args)
{
var devices = CaptureDeviceList.Instance;
var device = devices[Initialization(args)];
//Register our handler function to the 'packet arrival' event
try
{
device.OnPacketArrival += new PacketArrivalEventHandler(PacketCapturer);
}
catch (Exception ex)
{
Console.WriteLine("Error registering handler! : "+ ex);
}
Console.WriteLine
("\n-- The following filter will be applied: \"{0}\"", filter);
Console.WriteLine
("-- Listening on {0} {1}. \n\n Hit 'Enter' to stop...\n\n", device.Name, device.Description);
-> This is were I would like to start again every time.. (Closing the device beforehand, so it fully resets everything.. thinking of try{device.close()}, catch ... )
// Open the device for capturing
device.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds);
//filter to capture only packets from the Application that have data
device.Filter = filter;
// Start capture:
device.StartCapture();
// Wait for 'Enter' from the user.
Console.ReadLine();
device.Close();
}
//整个程序中使用的一些变量
私有静态字节[]EncryptionKey={XYZ}
私有静态字符串筛选器={XYZ}
私有静态int readtimeoutmillizes=1000;
私有静态int defaultDevice=-1;
private static System.Collections.Generic.List _incomingBuffer=new System.Collections.Generic.List();
////////////////////////////////////////////////
静态void Main(字符串[]参数)
{
var devices=CaptureDeviceList.Instance;
var设备=设备[初始化(args)];
//将我们的处理程序函数注册到“数据包到达”事件
尝试
{
device.OnPacketArrival+=新的PacketArrivalEventHandler(PacketCapturer);
}
捕获(例外情况除外)
{
WriteLine(“注册处理程序时出错:”+ex);
}
控制台写入线
(“\n--将应用以下筛选器:\“{0}\”,筛选器);
控制台写入线
(“--在{0}{1}上侦听。\n\n单击“回车”停止…\n\n”、device.Name、device.Description);
->这是我每次都想重新启动的情况…(事先关闭设备,这样它会完全重置所有内容..考虑尝试{device.close()},catch…)
//打开设备进行捕获
device.Open(DeviceMode.Promiscuous,ReadTimeOut毫秒);
//筛选以仅捕获应用程序中包含数据的数据包
设备。过滤器=过滤器;
//开始捕获:
device.StartCapture();
//等待用户输入。
Console.ReadLine();
设备。关闭();
}
通常我会用定时器来解决这个问题,但我不确定如何处理只从主方法中重新启动所选部分的问题,因为事件处理程序和先前选择的捕获设备都在主方法中注册
通过重新启动捕获,我希望在捕获停止时重新启动捕获
我应该如何处理这个问题
很抱歉,如果这是一个不切实际的问题,但我远不是一个好的程序员,到目前为止,我只从自己的小项目中学到了一些东西。对SharpPCap不太了解。从您的代码中可以看出,您正在重用一个设备实例,这很可能是第二个客户端导致它失败的原因 在GitHub项目中,我猜每次打开客户端时都可以创建一个新实例 更改:
var device=devices[初始化(args)]
致:
var device=devices.New()[初始化(args)]
重新启动我的部分代码?
因此您遇到了一个问题,您想创建另一个问题来尝试解决它。我的建议是找出第一个问题为什么是第一个问题