1
我想問爲什麼我的通信器接收發送的幀。我試圖解決這個問題使用標誌PacketDeviceOpenAttributes.NoCaptureLocal接收通信,但我仍然收到發送的幀。誰能知道如何解決這個問題?謝謝。這裏是我的代碼:C#Pcap.net通信
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;
}
} }
我犯了一個錯誤。 PacketDeviceOpenAttributes.NoCaptureRemote只是嘗試。即使我使用的是NoCaptureLocal,它仍然無法正常工作。請幫忙。 –
你應該使用'break'來退出for循環。 *但是,'sendDevice = allDevices.FirstOrDefault(d => d.Name.Equals(listDevices.SelectedItems [0] .Text)'將會有點整潔,而不是整個循環。名稱沒有找到(出於某種原因),然後'sendDevice'結束爲'allDevices'中的最後一項,你可能不想這樣做。 –
yea我的代碼不起作用,但是這不能解決我的問題,我是對嗎?我只是不想捕捉髮送幀,這就是全部:/ –