提问人:Gabriele Troviso 提问时间:10/6/2023 更新时间:10/6/2023 访问量:36
在环回设备上使用 SharpPCAP 发送数据包
Send packet with SharpPCAP on Loopback device
问:
首先对不起我的英语。 我尝试使用 SharpPCAP 在环回设备上发送和接收数据包,但发生错误。我向你展示我的程序。 首先,我找到环回设备。其次,我打开设备并创建数据包。最后,我用 SendPacket 方法发送
LibPcapLiveDevice senderDevice = null;
LibPcapLiveDeviceList devices = LibPcapLiveDeviceList.Instance;
foreach(LibPcapLiveDevice dev in devices)
{
if (dev.Addresses.Count>0 &&
IPAddress.Loopback.Equals(dev.Addresses?[1].Addr.ipAddress))
{
senderDevice = dev;
break;
}
}
senderDevice.Open();
EthernetPacket packet = PacketCreate(File.ReadAllBytes(".txt"));
senderDevice.SendPacket(packet) // also with packet.Bytes
其中 PacketCreate 函数是
EthernetPacket PacketCreate(byte[] bytesToSend)
{
PhysicalAddress macAdd = GetMACAddress(); //simple function to obtain mac address
ushort tcpSourcePort = 123, tcpDestinationPort = 45678;
TcpPacket tcpPacket = new TcpPacket(tcpSourcePort, tcpDestinationPort);
tcpPacket.PayloadData = bytesToSend;
var ipPacket = new IPv4Packet(IPAddress.Loopback, IPAddress.Loopback);
var ethernetPacket = new EthernetPacket(macAdd, macAdd, EthernetType.None);
// Now stitch all of the packets together
ipPacket.PayloadPacket = tcpPacket;
ethernetPacket.PayloadPacket = ipPacket;
return ethernetPacket;
}
如果我在调试模式下抛出应用程序,我会在 SendPacket 函数上出现此错误
SharpPcap.PcapException:“无法发送数据包:发送错误:PacketSendPacket 失败:不支持请求。(50)'
为什么?我有什么不明白的?.
答: 暂无答案
评论