1
我正在使用pcap.net來捕獲數據包。 捕獲數據包的方法正在新線程中運行。 當我想停止/恢復捕獲時,我使用ManualResetEvent來停止/恢復線程。pcap.net communicator在線程停止時緩衝數據包
它工作正常,問題是當我中斷捕獲並重新啓動它(停止並恢復線程) - 通信器收到在線程停止期間發生的數據包。 我認爲這是因爲傳播者的緩衝區。
是要溝通到,而線程停止不了包,當線程重新開始將再次得到數據包。 有幫助嗎?
我的代碼:
#region Members
private PacketCommunicator _Communicator;
private IList<LivePacketDevice> _allDevices;
private PacketDevice selectedDevice;
private Thread captureThread;
private ManualResetEvent _pauseEvent = new ManualResetEvent(true);
#endregion
#region Methods
public PacketGateway()
{
try
{
_allDevices = LivePacketDevice.AllLocalMachine;
selectedDevice = _allDevices[0];
captureThread = new Thread(StartListening);
}
catch (Exception e)
{
throw e;
}
}
// Starts/Resumes the Thread
public void Start()
{
/// Starts the Thread the first time
if (captureThread.ThreadState == ThreadState.Unstarted)
{
_Communicator = selectedDevice.Open();
captureThread.Start();
}
/// Resumes the Thread
if (captureThread.ThreadState == ThreadState.WaitSleepJoin)
{
_pauseEvent.Set();
}
}
public void Stop()
{
/// stop the thread
_pauseEvent.Reset();
}
// Starts to recieve packets
public void StartListening()
{
try
{
_Communicator.ReceivePackets(0, HandlePacket);
}
catch (Exception ex)
{
throw ex;
}
}
// Handles Packet
private void HandlePacket(Packet packet)
{
// some work..
}
#endregion
非常感謝!
謝謝你!它效果很好!很簡單.. –