使用Pcapdotnet通过互联网发送数据包

时间:2016-03-09 21:03:39

标签: c# .net pcap.net

我想使用pcapdotnet将数据包发送到我的免费网站,但没有收到数据包,任何人都可以告诉我哪里是我的错,

我的网站地址:www.infinitypower.somee.com 我用ping来获取ip地址:204.27.56.226 代码:


using System;

namespace ClientApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            fillDevice();
        }

        private void btnInvoke_Click(object sender, EventArgs e)
        {
            // Take the selected adapter
            if (lsDevice.SelectedItem != null)
            {
                PcapDotNet.Core.PacketDevice selectedDevice = (PcapDotNet.Core.PacketDevice)lsDevice.SelectedItem;

                // Open the output device
                using (PcapDotNet.Core.PacketCommunicator communicator = selectedDevice.Open(100, PcapDotNet.Core.PacketDeviceOpenAttributes.Promiscuous, 1000))
                {   
                    communicator.SendPacket(BuildEthernetPacket());
                }
            }
            else
                MessageBox.Show("Select device from the list");

        }

        private void fillDevice()
        {
            lsDevice.Items.Clear();

            IList allDevices = PcapDotNet.Core.LivePacketDevice.AllLocalMachine;

            if (allDevices.Count == 0)
            {
                lsDevice.Items.Add("No interfaces found! Make sure WinPcap is installed.");
                return;
            }

            // Print the list
            for (int i = 0; i != allDevices.Count; ++i)
            {
                PcapDotNet.Core.LivePacketDevice device = allDevices[i];
                lsDevice.Items.Add(device);
            }
        }

        private PcapDotNet.Packets.Packet BuildEthernetPacket()
        {
            PcapDotNet.Packets.Ethernet.EthernetLayer ethernetLayer = new PcapDotNet.Packets.Ethernet.EthernetLayer();
            ethernetLayer.Source = new PcapDotNet.Packets.Ethernet.MacAddress("01:02:03:04:05:06");
            ethernetLayer.Destination = new PcapDotNet.Packets.Ethernet.MacAddress("01:02:03:04:05:67");
          ethernetLayer.EtherType = PcapDotNet.Packets.Ethernet.EthernetType.IpV4;

            ipV4Layer.CurrentDestination = new PcapDotNet.Packets.IpV4.IpV4Address("204.27.56.226");
                        ipV4Layer.Identification = (ushort)100;

            PcapDotNet.Packets.PayloadLayer payloadLayer = new PcapDotNet.Packets.PayloadLayer();
            payloadLayer.Data = new PcapDotNet.Packets.Datagram(System.Text.Encoding.UTF8.GetBytes(txPrayload.Text));

            PcapDotNet.Packets.PacketBuilder builder = new PcapDotNet.Packets.PacketBuilder(ethernetLayer, ipv4layer, payloadLayer);
            return builder.Build(DateTime.Now);
        }
    }
}


0 个答案:

没有答案