Winpcap: pcap_breakloop 函数到 pause/stop 嗅探将不起作用

Winpcap: pcap_breakloop function to pause/stop sniffing won't work

因此,在大学的最后一年 project/dissertation,我正在使用 C++ 和 pcap 编写数据包嗅探器。该程序目前可以嗅探数据包,但我正在尝试对其进行编码,以便在按下转义键时,嗅探将停止。这是有问题的代码:

while ((result = pcap_loop(adhandle, 0, packet_handler, NULL)) == 0) {

        if (result == 0)
            continue;

        if (GetAsyncKeyState(VK_ESCAPE))
        {
            result = -2;
        }

        if (result == -2)
        {
            pcap_breakloop(adhandle);
        }
    }

当按下退出键时,没有任何反应,嗅探会继续,直到程序关闭。任何关于为什么这不起作用的帮助将不胜感激!

来自 pcap_loop 上的文档:

pcap_loop() processes packets from a live capture or ''savefile'' until cnt packets are processed, the end of the ''savefile'' is reached when reading from a ''savefile'', pcap_breakloop() is called, or an error occurs. It does not return when live read timeouts occur. A value of -1 or 0 for cnt is equivalent to infinity, so that packets are processed until another ending condition occurs.

因为你给 pcap_loop 的第二个参数是 0,它会无限期地处理数据包,除非发生错误,否则不会到达你的 while 循环体。