简体   繁体   中英

How to enable a custom IP-address for a UDP packet sender?

So i'm making a little UDP packet sender, but I have a problem. I have it set so that when the user clicks "button 2" they will automatically send a packet to the IP which I have specified. How can I make it so that the user can put there own IP address in and that becomes the IP which a packet is sent to? Here is the code I have so far:

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)
        {

        }
    }
}

Also how do I get the stop button to stop the process?

You could have a TextBox on the GUI that allows the user to input a string representing the IP address and when the button is clicked you take the contents and use them to send the packet:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM