C#发送UDP包

时间:2014-11-03 03:13:35

标签: c# sockets udp

我想知道,他们是否可以检查您的程序是否正在将UDP个软件包发送到所需的IP?我是初学者套接字程序员。因此,如果您决定帮助我,请详细说明。我只有15岁并且仅在2个月内学习c#

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.Net;
using System.Net.Sockets;

namespace Challenger
{
    public partial class Form1 : Form
    {
        int ipWidth;
        String x;
        String methodValue;
        int threadNumber;
        IPEndPoint endPoint;
        byte[] buffer;

        public Form1()
        {
            InitializeComponent();
            urlTextbox.Text ="www.";
            this.MessageTextBox.Size = new System.Drawing.Size(231, 40);
            MessageTextBox.Text = "When harpoons, air strikes, and nukes fail.";
            threadValue();
            methodSetter();
            ipLabelText();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.FromArgb(0, 47, 80); //Dark blue background
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Net.IPAddress[] addressess = System.Net.Dns.GetHostAddresses(urlTextbox.Text);
                String ipTextLength = Convert.ToString(addressess[0]);
            SendUDPPacket(ipTextLength, 80, "Hello!", 100000000);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Color pixelColor;//Initialize pixelColor           
            SolidBrush pixelBrush = new SolidBrush(Color.FromArgb(0, 83, 146)); //RGB Brush           
            e.Graphics.FillRectangle(pixelBrush, 0, 0, 500, 400); //Light blue rectangle for displaying IP address
        }

        private void urlTextbox_TextChanged(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {      
                System.Net.IPAddress[] addresses = System.Net.Dns.GetHostAddresses(urlTextbox.Text);
                String ipTextLength = Convert.ToString(addresses[0]);
                label2.Text = Convert.ToString(addresses[0]); //Puts ip into a string-> Label for Display  
                label2.Location = new Point(80, 20);                    
        }
        public void ipLabelText()
        {
            label2.Parent = panel1;
            label2.BackColor = Color.Transparent;
            label2.ForeColor = Color.White;         
        }

        private void label2_Click(object sender, EventArgs e)
        {
        }

        private void TimeoutLabel_Click(object sender, EventArgs e)
        {
        }

        private void portLabel_Click(object sender, EventArgs e)
        {
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            this.MessageTextBox.Size = new System.Drawing.Size(231, 40);
        }

        private void label4_Click(object sender, EventArgs e)
        {
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void methodClick(object sender, MouseEventArgs e)
        {
        }
        public void methodSetter()
        {
            comboBox1.SelectedIndex = 0;
            if (comboBox1.SelectedIndex == 0)
            {
                methodValue = "TCP";
            }
            if (comboBox1.SelectedIndex == 1)
            {
                methodValue = "UDP";
            }
            if (comboBox1.SelectedIndex == 2)
            {
                methodValue = "HTTP";
            }
        }

        private void textBox3_TextChanged_1(object sender, EventArgs e)
        {
        }

        public void threadValue()
        {
            textBox3.Text = "10";//Default thread value
            threadNumber = Convert.ToInt32(threadNumber);
        }

        private void ipLockOn_Click(object sender, EventArgs e)
        {
            IPHostEntry hostEntry;
            hostEntry = Dns.GetHostEntry(ip1.Text+"."+ip2.Text+"."+ip3.Text+"."+ip4.Text);
            String x = Convert.ToString(hostEntry.AddressList);

            label2.Text = x; //Puts ip into a string-> Label for Display    

            label2.Location = new Point(80, 20);    
        }
        public void SendUDPPacket(string hostNameOrAddress, int destinationPort, string data, int count)
        {
            // Validate the destination port number
            if (destinationPort < 1 || destinationPort > 65535)
                throw new ArgumentOutOfRangeException("destinationPort", "Parameter destinationPort must be between 1 and 65,535.");

            // Resolve the host name to an IP Address
            IPAddress[] ipAddresses = Dns.GetHostAddresses(urlTextbox.Text);
            if (ipAddresses.Length == 0)
                throw new ArgumentException("Host name or address could not be resolved.", "hostNameOrAddress");

            // Use the first IP Address in the list
            IPAddress destination = ipAddresses[0];
            IPEndPoint endPoint = new IPEndPoint(destination, destinationPort);
            byte[] buffer = Encoding.ASCII.GetBytes(data);

            // Send the packets
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);           
            while (true)
            {
                socket.SendTo(buffer, endPoint);
            }
        }
    }
}
  • 在C#中移植LOIC Android应用程序

1 个答案:

答案 0 :(得分:3)

https://www.wireshark.org/ - 这是网络调试的最佳工具。您可以通过UDP&amp;过滤端口,它将为您提供所有数据包的详细分类和标题,包括来源和目的地IP。