首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Pcap.net通信

C# Pcap.net通信
EN

Stack Overflow用户
提问于 2016-10-09 01:40:34
回答 1查看 909关注 0票数 1

我想问为什么我的通信器接收发送的帧。我正在尝试使用接收通信器的标志PacketDeviceOpenAttributes.NoCaptureLocal来解决这个问题,但我仍然在接收已发送的帧。有人知道如何解决这个问题吗?谢谢。下面是我的代码:

代码语言:javascript
复制
using PcapDotNet.Core;
using PcapDotNet.Packets;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSIP
{
    public partial class Form2 : Form
    {
    private Packet packet;
    private List<Packet> buffer;
    private IList<LivePacketDevice> allDevices;
    private LivePacketDevice selectedDevice, sendDevice;
    private int pkt_cnt;
    private Thread thrReceive,thrSend;
    public Form2(LivePacketDevice device)
    {
        InitializeComponent();
        Show();
        selectedDevice = device;
        pkt_cnt = 0;
        buffer = new List<Packet>();
        allDevices = LivePacketDevice.AllLocalMachine;

        for (int i = 0; i != allDevices.Count; ++i)
        {
            LivePacketDevice tempDevice = allDevices[i];
            if (device.Description != null)
            {
                ListViewItem row = new ListViewItem(tempDevice.Name);
                listDevices.Items.Add(row);
            }
        }
    }

    private void PacketHandler(object obj)
    {
        throw new NotImplementedException();
    }

    private void PacketHandler(Packet packet)
    {
        ListViewItem itemPacket = new ListViewItem(pkt_cnt.ToString());
        itemPacket.SubItems.Add(packet.Ethernet.Destination.ToString());
        itemPacket.SubItems.Add(packet.Ethernet.Source.ToString());

        pktView.Items.Add(itemPacket);
        buffer.Add(packet);
        pkt_cnt++;
    }

    private void Sending()
    {
        for (int i = 0; i <= allDevices.Count; ++i)
        {
            sendDevice = allDevices[i];
            if (sendDevice.Name.Equals(listDevices.SelectedItems[0].Text))
            {
                i = allDevices.Count;
            }
        }
        using (PacketCommunicator communicator = sendDevice
        .Open(100, PacketDeviceOpenAttributes.Promiscuous | PacketDeviceOpenAttributes.NoCaptureLocal, 1000))
        {
            int index = Int32.Parse(pktView.SelectedItems[0].Text);
            Packet tmpPacket = buffer.ElementAt(index);
            communicator.SendPacket(tmpPacket);
        }
    }

    private void Receiving()
    {
        int c = int.Parse(packetcount.Text);

        using (PacketCommunicator communicator = selectedDevice.Open(65536,
         PacketDeviceOpenAttributes.NoCaptureRemote | PacketDeviceOpenAttributes.Promiscuous, 1000))
        {
            communicator.ReceivePackets(c, PacketHandler);
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        thrReceive = new Thread(Receiving);
        thrReceive.Start();
    }

    private void buttonSend_Click(object sender, EventArgs e)
    {
        thrSend = new Thread(Sending);
        thrSend.Start();

    }

    private void pktView_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            int index = Int32.Parse(pktView.SelectedItems[0].Text);
            Packet tmpPacket = buffer.ElementAt(index);
            textPacket.ResetText();
            textPacket.AppendText(tmpPacket.Ethernet.ToHexadecimalString());
        }
        catch (Exception E)
        {
            Console.WriteLine(E.ToString());
        }
    }

    private void buttonClear_Click(object sender, EventArgs e)
    {
        pktView.Items.Clear();
        buffer.Clear();
        pkt_cnt = 0;

    }
} }
EN

回答 1

Stack Overflow用户

发布于 2016-10-15 17:56:41

您似乎正在使用不同的PacketCommunicators,一个用于发送,另一个用于接收。

NoCaptureRemote状态的文档“定义了本地适配器是否会捕获它自己生成的流量。”

因为您使用的是两个不同的通信器,所以接收通信器捕获发送通信器。

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

https://stackoverflow.com/questions/39935404

复制
相关文章

相似问题

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