首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pcap.net ReceivePacket不能正常工作

pcap.net ReceivePacket不能正常工作
EN

Stack Overflow用户
提问于 2019-05-20 16:42:04
回答 1查看 300关注 0票数 0
  1. 接收数据包。 在我发送了一个数据包之后,我还想接收来自上一个数据包的目的地的数据包。 我尝试使用ReceivePacket,但总是得到packet12的空值。 还有什么需要我做的吗?
  2. 设置过滤器。 我查了一下pcap.net的文件 我假设参数("ip和icmp")与我们可以在wireshark中使用的过滤器相同。 ip.src == 192.168.15.32和icmp 但是这个参数有个错误。这和wireshark的过滤器不同吗?

代码:

代码语言:javascript
复制
using (PacketCommunicator inputCommunicator =
            selectedInputDevice.Open(65536, // portion of the packet to capture
                                            // 65536 guarantees that the whole packet will be captured on all the link layers
                PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                1000)) // read timeout
        {
            using (PacketCommunicator outputCommunicator =
                selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000))
            {
                // Check the MAC type
                if (inputCommunicator.DataLink != outputCommunicator.DataLink)
                {
                    Console.WriteLine("Warning: the datalink of the capture differs from the one of the selected interface.");
                    Console.WriteLine("Press a key to continue, or CTRL+C to stop.");
                    Console.ReadKey();
                }

                // Allocate a send buffer
                using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength))
                {
                    // Fill the buffer with the packets from the file

                    PcapDotNet.Packets.Packet tpacket;
                    while (inputCommunicator.ReceivePacket(out tpacket) ==
                           PacketCommunicatorReceiveResult.Ok)
                    {
                        sendBuffer.Enqueue(tpacket);
                        ++numPackets;
                    }

                    //                        bool isSync = true;

                    // Transmit the queue
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    long startTimeMs = stopwatch.ElapsedMilliseconds;
                    Console.WriteLine("Start Time: " + DateTime.Now);

                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Sending a pcap file is in progress. Wait until it is finished");
                    Console.ResetColor();

                    outputCommunicator.Transmit(sendBuffer, isSync);

                    long endTimeMs = stopwatch.ElapsedMilliseconds;
                    Console.WriteLine("End Time: " + DateTime.Now);
                    long elapsedTimeMs = endTimeMs - startTimeMs;
                    Console.WriteLine("Elapsed Time: " + elapsedTimeMs);

                    Console.WriteLine("Elapsed time: " + elapsedTimeMs + " ms");
                    Console.WriteLine("Total packets generated = " + numPackets);
                    //                        Console.WriteLine("Average packets per second = " + averagePacketsPerSecond);
                    Console.WriteLine("============================================================");

                }
                inputCommunicator.SetFilter("ip and icmp");

                Console.WriteLine("Listening on " + selectedInputDevice.Description + "...");

                PcapDotNet.Packets.Packet packet12;

                inputCommunicator.ReceivePacket(out packet12);

               foreach (var options in packet12)
                    Console.WriteLine(options);
EN

回答 1

Stack Overflow用户

发布于 2019-06-21 06:44:11

1.

在某种程度上,您似乎使用inputCommunicator

//用文件中的数据包填充缓冲区

但是在不同的地方,您使用相同的inputCommunicator从某个目的地获取数据包发送。

似乎您需要使用两个不同的通信器,并使用不同的设备创建它们。

2.

如果您有错误,请提供错误。

ip.src的语法是从哪里得到的?你试过src host了吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56225046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档