Capturing packets in a thread throw ArgumentOutOfRangeException

May 10, 2015 at 3:12 PM
Edited May 10, 2015 at 3:29 PM
Hi there

I'm trying to capture tcp packets in a new thread using pcapdotnet libray. I'm testing it on my local machine. When I don't apply the filter, it seems to capture everything but the actual packet, whereas when I apply it, I don't receive anything. Thought first the port I was using was wrong, then tried 1234, and others but same result. At that point, I don't see what I'm doing wrong. Thanks in advance for your help.
Here is the code I'm using :

PacketCommunicator communicator = LivePacketDevice.AllLocalMachine[1].Open(65536, PacketDeviceOpenAttributes.Promiscuous,1000);

static void Main(string[] args)
Thread checkDataServer = new Thread(checkData);
communicator.SendPacket(PacketCreator.BuildTcpPacket(6666, null, myInt.getMac(), mac, ip, myInt.getIp(), Encoding.UTF8.GetBytes("test")));

private void checkData()
while (true)
            communicator.SetFilter("ip and tcp and dst port 6666");
            Packet packet;
            PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
            switch (result)
                case PacketCommunicatorReceiveResult.Timeout: break;
                case PacketCommunicatorReceiveResult.Ok:
                    Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length);
                    throw new InvalidOperationException("The result " + result + " should never be reached here");
        catch (Exception e) { throw e; }

Also I noticed that when I send my packet in a while true loop, I can capture the packets in the thread. And it doesn't capture one, or a thousand packets. I can't figure out why.
May 16, 2015 at 11:06 AM
Hi julgandit,

Please note that Pcap.Net has moved to GitHub.

For Q&A, see Pcap.Net Q&A Group.