C#Pcap.net通信

C#Pcap.net通信,c#,pcap,pcap.net,communicator,C#,Pcap,Pcap.net,Communicator,我想问一下,为什么我的通讯器接收到发送的帧。我正在尝试使用接收通信器的flag PacketDeviceOpenAttributes.NoCaptureLocal来解决此问题,但我仍在接收发送的帧。有人知道如何解决这个问题吗?非常感谢。这是我的密码: using PcapDotNet.Core; using PcapDotNet.Packets; using System; using System.Collections.Generic; using System.ComponentModel

我想问一下,为什么我的通讯器接收到发送的帧。我正在尝试使用接收通信器的flag PacketDeviceOpenAttributes.NoCaptureLocal来解决此问题,但我仍在接收发送的帧。有人知道如何解决这个问题吗?非常感谢。这是我的密码:

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;

    }
} }
使用PcapDotNet.Core;
使用PcapDotNet.Packets;
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间PSIP
{
公共部分类表单2:表单
{
专用分组;
私有列表缓冲区;
私人IList所有设备;
私有LivePacketDevice选择设备、发送设备;
私立国际大学;
私有线程thrReceive,thrSend;
公共表单2(LivePacketDevice)
{
初始化组件();
Show();
所选设备=设备;
pkt_cnt=0;
buffer=新列表();
allDevices=LivePacketDevice.AllLocalMachine;
for(int i=0;i!=allDevices.Count;++i)
{
LivePacketDevice tempDevice=所有设备[i];
如果(device.Description!=null)
{
ListViewItem行=新ListViewItem(tempDevice.Name);
listDevices.Items.Add(行);
}
}
}
私有void PacketHandler(对象obj)
{
抛出新的NotImplementedException();
}
专用void PacketHandler(数据包)
{
ListViewItemPacket=新的ListViewItem(pkt_cnt.ToString());
itempack.SubItems.Add(packet.Ethernet.Destination.ToString());
itempack.SubItems.Add(packet.Ethernet.Source.ToString());
pktView.Items.Add(itemPacket);
buffer.Add(数据包);
pkt_cnt++;
}
私有无效发送()
{

对于(inti=0;i而言,您似乎使用了不同的包通信器,一个用于发送,一个用于接收

nocapturemote
的文档说明“定义本地适配器是否将捕获其自己生成的流量。”


由于您使用的是两个不同的通信器,接收通信器会捕获发送通信器。

我犯了一个错误。PacketDeviceOpenAttributes.NoCaptureRemote只是一次尝试。即使我使用的是NoCaptureLocal,它仍然无法正常工作。请提供帮助。您应该使用
break
退出for循环。但是,
sendDevice=allDevices.FirstOrDefault(d=>d.Name.Equals(listDevices.SelectedItems[0].Text)
将比整个循环更整洁。此外,由于循环的结构,如果找不到名称(出于某种原因)然后,
sendDevice
最终成为
allDevices
中的最后一项,这可能是您不想要的。是的,我的代码无效,但这不会解决我的问题,对吗?我只是不想捕获发送的帧,仅此而已:/即使我使用一个通信器接收和发送同一个数据包,我也总是会收到发送的p阿克特。