【问题标题】:How to enable a custom IP-address for a UDP packet sender?如何为 UDP 数据包发送者启用自定义 IP 地址?
【发布时间】:2012-07-01 18:07:06
【问题描述】:

所以我正在制作一个小的 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)
        {

        }
    }
}

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

【问题讨论】:

    标签: c# .net windows visual-studio-2010 sockets


    【解决方案1】:

    你可以在 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 2011-01-30
      • 2012-10-14
      • 1970-01-01
      • 2011-01-26
      • 2020-01-20
      相关资源
      最近更新 更多