0
votes

Im running a little Network Sniffer for a Game im playing and im trying to capture certain traffic but encounter a problem that is probably caused by SharpPcap having troubles with multiple TCP-Connections (E.g. when I open or close a second Client. I assume that it possibly stops tracking when one of the TCP Connections closes, even tho the second one is still running)

Here is the important part of the code, its pretty basic and taken from another project:

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

Usually I would approach this with a timer, but im not sure how to handle restarting only the selected part from within the main method, since the Event Handler is registered there as well as the previously selected Capture Device..

By restarting the capture I hope to restart the capturing when it drops..

How should I approach this?

Sorry if its a noobish question, but im far, far away from beeing a good programmer, I only learned from my own little projects so far..

1
Restarting part of my Code? so you have a problem, and you want to create another problem to try and solve it. my advice is to work out why the first problem is firstTheGeneral

1 Answers

1
votes

Not knowing much about SharpPCap it appears from your code that you are reusing a device instance, and this is most likely why the second client causes it to fail.

Looking at GitHub for the project, my guess is you could create a new instance each time the client is opened.

Change: var device = devices[Initialization(args)]

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

Example from GitHub