0

我正在为正在玩的游戏运行一个小网络嗅探器,并且我试图捕获某些流量,但遇到一个问题,这可能是由于 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();
    }

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

通过重新启动捕获,我希望在捕获下降时重新启动捕获..

我应该如何处理这个?

抱歉,如果这是一个愚蠢的问题,但我离成为一名优秀的程序员还很远,到目前为止,我只从自己的小项目中学到了东西。

4

1 回答 1

1

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

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

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

至: var device = devices.New()[Initialization(args)]

来自 GitHub 的示例

于 2018-07-31T12:46:48.577 回答