如何启用自定义的IP地址对于UDP数据包发送者? [英] How to enable a custom IP-address for a UDP packet sender?

查看:159
本文介绍了如何启用自定义的IP地址对于UDP数据包发送者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在做一个小的UDP包的发送者,但我有一个问题。我把它设定为使得当用户点击按钮2,它们将自动发送一个数据包到我所指定的IP。我怎样才能让这个用户可以把有自己的IP地址,并且成为其中一个数据包被发送到IP?这里是code我到目前为止有:

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?

推荐答案

您可以有一个文本框上,允许用户输入一个字符串,再$ P $的GUI psenting的IP地址,单击该按钮,当你把内容,并利用它们来发送数据包:

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

这篇关于如何启用自定义的IP地址对于UDP数据包发送者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆