1

所以我正在制作一个小的 UDP 数据包发送者,但我有一个问题。我已经设置好了,当用户点击“按钮 2”时,他们会自动向我指定的 IP 发送一个数据包。我怎样才能使用户可以将自己的IP地址放入其中并成为数据包发送到的IP?这是我到目前为止的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace ProjectTakedown
{
    public partial class Form1 : Form
    {
        public Form1() //where the IP should be entered
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e) //button to start takedown
        {
            byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes("<Packet OF Data Here>");
            string IP = "127.0.0.1";
            int port = 80;

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);

            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            client.SendTo(packetData, ep);
        }

        private void Stop_Click(object sender, EventArgs e)
        {

        }
    }
}

另外,我如何获得停止按钮来停止该过程?

4

1 回答 1

0

您可以TextBox在 GUI 上有一个允许用户输入表示 IP 地址的字符串,当单击按钮时,您可以获取内容并使用它们发送数据包:

private void button2_Click(object sender, EventArgs e) //button to start takedown
{
     byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes("<Packet OF Data Here>");
     string IP = textBox1.Text; // take input by user
     int port = 80;

     IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);

     Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
     client.SendTo(packetData, ep);
}
于 2012-06-30T14:29:31.500 回答