重新启动我的部分代码? SharpPCap 的问题

Restarting part of my Code? Problems with SharpPCap

我是 运行 我正在玩游戏的一个小网络嗅探器,我正在尝试捕获某些流量,但遇到一个问题,这可能是由于 SharpPcap 有多个 TCP 连接的问题(例如,当我打开或关闭第二个客户端。我假设它可能会在其中一个 TCP 连接关闭时停止跟踪,即使第二个仍然 运行)

这是代码的重要部分,它非常基础,取自另一个项目:

        // 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();
    }

通常我会用一个计时器来解决这个问题,但我不确定如何处理仅从主要方法中重新启动选定的部分,因为事件处理程序以及先前选择的捕获设备都在那里注册..

重启抓包希望掉线的时候重新抓包..

我该如何处理?

抱歉,如果这是一个菜鸟问题,但我离成为一名优秀的程序员还有很长的路要走,到目前为止,我只是从自己的小项目中学到的..

不太了解 SharpPCap 从您的代码看来您正在重用设备实例,这很可能是第二个客户端导致它失败的原因。

查看项目的 GitHub,我猜你可以在每次打开客户端时创建一个新实例。

变化: var device = devices[Initialization(args)]

收件人: var device = devices.New()[Initialization(args)]

Example from GitHub