如何为UDP数据包发送者启用自定义IP地址?

所以我正在做一个UDP数据包发送者,但我有一个问题。 我已经设置了这样,当用户点击“button2”,他们会自动发送一个数据包到我指定的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) { } } } 

另外我如何获得停止button来停止进程?

Solutions Collecting From Web of "如何为UDP数据包发送者启用自定义IP地址?"

您可以在GUI上有一个TextBox ,允许用户输入一个代表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); }