1

I am using PcapDot.Net project Dlls to send packets (using Pcap file).

my question is if i want to stop the transmit in the middle i try PacketCommunicator.Break() and i can see with Wireshark that it still continue to send packets. I also try PacketCommunicator.Dispose() and in this case i only get an crash: vshots.exe has stopped working.

user2214609
  • 4,713
  • 9
  • 34
  • 41

1 Answers1

4

PacketCommunicator.Break() will not help here. It's meant to stop a capture, not a transmission. As far as I see, there is no clear way to do what you wish from the Library, so I only propose workarounds. See if they help you, if not - contact the developer and ask for this feature.

Option 1 - You can send each packet separately in a loop, using PacketCommunicator.SendPacket(). This will be slower but will allow you to stop after each packet by modifying the loop's condition.

Option 2 - You can send use PacketCommunicator.Transmit but with smaller batches of packets

Change

while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok)
{
    sendBuffer.Enqueue(packet);
    ++numPackets;
}

into something like

int packetsInBatch = MAX_PACKETS_IN_BATCH;    
while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok && packetsInBatch > 0)
{
    sendBuffer.Enqueue(packet);
    ++numPackets;
    --packetsInBatch;
}

and put everything in another for loop. This will allow you to stop the loop after the end of the batch and is a trade-off between speed and delay after you signal to stop.

Option 3 - Mercilessly kill the send buffer. Call sendBuffer.Dispose() and handle the consequences.

[HandleProcessCorruptedStateExceptions]
private static void Transmit(PacketCommunicator outputCommunicator, PacketSendBuffer sendBuffer, bool isSync)
    {
        try
        {
            outputCommunicator.Transmit(sendBuffer, isSync);                
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

You'll have to handle AccessViolationException. I have done this by adding the HandleProcessCorruptedStateExceptions attribute to a new method I created which performs the transmit (see How to handle AccessViolationException). It seems to work on my machine, but this is really a last resort solution. I wouldn't use it in anything but the simplest command line utilities without a (very) through testing. There's work with unmanaged code going on and I don't know what happens to resources when we pull this trick.

Community
  • 1
  • 1
Vadim
  • 2,847
  • 15
  • 18
  • I think using SendPacket() will be the best option but the only thing i saw that problematic is that using SendPacket() transmit all the packets without delay between the packets compare to SendBuffer() that respect the packet TimeStamp, i don't want to use Thread.Sleep, what is the best option to do it ? – user2214609 Aug 12 '13 at 04:39
  • I'm not sure Thread.Sleep will be good enough, but that depends on your scenario - the amount of packets and the delays between them. I don't know how crucial it is for you to stop sending immediately after you wish to stop. I think the second option is the best here overall. You can create a buffer of a second or two, send it and in the meantime prepare the next, to minimize the delay. I only added the third option for completeness, I wouldn't touch it with a stick. – Vadim Aug 12 '13 at 05:10
  • Why the third option is a bad idea ? – user2214609 Aug 12 '13 at 16:29
  • Because there's unmanaged code going on. I don't know what'll keep running after the murder, or what what resources will not get freed properly and so on. Like I said, i think it's a last resort which you should use with caution – Vadim Aug 12 '13 at 19:44