C# Pcap.net 通信 [英] C# Pcap.net communication

查看:63
本文介绍了C# Pcap.net 通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下为什么我的通讯器会收到发送的帧.我正在尝试使用标志 PacketDeviceOpenAttributes.NoCaptureLocal 来解决此问题以接收通信器,但我仍在接收发送的帧.有谁知道如何解决这个问题?谢谢你.这是我的代码:

I would like to ask why is my communicator receiving sent frames. I'm trying to fix this problem using flag PacketDeviceOpenAttributes.NoCaptureLocal for receiving communicator but I'm still receiving sent frames. Could anyone know how to fix this problem? Thank you. Here is my code:

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;

    }
} }

推荐答案

您似乎使用了不同的 PacketCommunicator,一种用于发送,一种用于接收.

You seem to be using different PacketCommunicators, one for sending and one for receiving.

NoCaptureRemote 的文档说明定义本地适配器是否将捕获自己生成的流量.".

The documentation for NoCaptureRemote states "Defines if the local adapter will capture its own generated traffic.".

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

Since you're using two different communicators, the receiving communicator captures the sending communicator.

这篇关于C# Pcap.net 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆