2012-06-30 39 views
1

所以我正在做一個UDP數據包發送者,但我有一個問題。我設置了它,當用戶點擊「按鈕2」時,它們會自動發送一個數據包到我指定的IP地址。我該如何做到這一點,以便用戶可以在那裏放置自己的IP地址,併成爲數據包發送到的IP?這裏是我到目前爲止的代碼:如何爲UDP數據包發送者啓用自定義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) 
     { 

     } 
    } 
} 

另外如何獲得停止按鈕來停止該過程?

+0

嗯, 「ProjectTakedown」? –

回答

0

你可能會對它允許用戶輸入代表IP地址的字符串和GUI一個TextBox當按鈕被點擊你需要的內容,並利用它們來發送數據包:

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); 
} 
相關問題