Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#发送UDP包_C#_Sockets_Udp - Fatal编程技术网

C#发送UDP包

C#发送UDP包,c#,sockets,udp,C#,Sockets,Udp,我想知道,他们是否有办法检查您的程序是否正在将UDP包发送到所需的IP?我是一个初学者socket程序员。因此,如果你决定帮助我,请解释一些细节。我才15岁,学了两个月 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Sys

我想知道,他们是否有办法检查您的程序是否正在将
UDP
包发送到所需的
IP
?我是一个初学者socket程序员。因此,如果你决定帮助我,请解释一些细节。我才15岁,学了两个月

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);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
Net系统;
使用System.Net.Sockets;
命名空间挑战者
{
公共部分类Form1:Form
{
宽度;
字符串x;
字符串方法值;
int线程数;
i终点;
字节[]缓冲区;
公共表格1()
{
初始化组件();
urlTextbox.Text=“www.”;
this.MessageTextBox.Size=新系统.Drawing.Size(231,40);
MessageTextBox.Text=“当鱼叉、空袭和核武器失败时。”;
threadValue();
methodSetter();
ipLabelText();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
this.BackColor=Color.FromArgb(0,47,80);//深蓝色背景
}
私有无效标签1_单击(对象发送方,事件参数e)
{
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
System.Net.IPAddress[]address=System.Net.Dns.GetHostAddresses(urlTextbox.Text);
字符串ipTextLength=Convert.ToString(addressess[0]);
SendUDPPacket(ipTextLength,80,“Hello!”,100000000);
}
私有void panel 1_Paint(对象发送器,PaintEventArgs e)
{
颜色pixelColor;//初始化pixelColor
SolidBrush pixelBrush=新的SolidBrush(Color.FromArgb(0,83,146));//RGB笔刷
e、 Graphics.FillRectangle(pixelBrush,0,0,500,400);//用于显示IP地址的浅蓝色矩形
}
私有无效urlTextbox\u TextChanged(对象发送方,事件参数e)
{
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{      
System.Net.IPAddress[]地址=System.Net.Dns.GetHostAddresses(urlTextbox.Text);
字符串ipTextLength=Convert.ToString(地址[0]);
label2.Text=Convert.ToString(地址[0]);//将ip放入字符串->标签以显示
标签2.位置=新点(80,20);
}
公共无效ipLabelText()
{
标签2.父项=面板1;
label2.BackColor=颜色。透明;
label2.ForeColor=颜色。白色;
}
私有无效标签2_单击(对象发送方,事件参数e)
{
}
私有void timeextabel_单击(对象发送方,事件参数e)
{
}
私有void端口标签单击(对象发送方,事件参数e)
{
}
私有void textBox3\u TextChanged(对象发送方,事件参数e)
{
this.MessageTextBox.Size=新系统.Drawing.Size(231,40);
}
私有无效标签4_单击(对象发送方,事件参数e)
{
}
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
}
私有void方法单击(对象发送者,MouseEventArgs e)
{
}
公共void methodSetter()
{
comboBox1.SelectedIndex=0;
如果(comboBox1.SelectedIndex==0)
{
methodValue=“TCP”;
}
如果(comboBox1.SelectedIndex==1)
{
methodValue=“UDP”;
}
如果(comboBox1.SelectedIndex==2)
{
methodValue=“HTTP”;
}
}
私有无效文本框3\u TextChanged\u 1(对象发送方,事件参数e)
{
}
public void threadValue()
{
textBox3.Text=“10”//默认线程值
threadNumber=转换为32(threadNumber);
}
单击时私有无效IPLock(对象发送方,事件参数e)
{
IPHostEntry-hostEntry;
hostEntry=Dns.GetHostEntry(ip1.Text+“+ip2.Text+”+ip3.Text+“+ip4.Text);
字符串x=Convert.ToString(hostEntry.AddressList);
label2.Text=x;//将ip放入字符串->标签中显示
标签2.位置=新点(80,20);
}
public void SendUDPPacket(字符串主机名或地址、int destinationPort、字符串数据、int计数)
{
//验证目标端口号
如果(目的港<1 | |目的港>65535)
抛出新ArgumentOutOfRangeException(“destinationPort”,“参数destinationPort必须介于1和65535之间”);
//将主机名解析为IP地址
IPAddress[]IPAddresss=Dns.GetHostAddresses(urlTextbox.Text);
如果(ipAddresses.Length==0)
抛出新ArgumentException(“无法解析主机名或地址。”,“主机名或地址”);
//使用列表中的第一个IP地址
IP地址目的地=IP地址[0];
IPEndPoint端点=新IPEndPoint(目的地,目的地端口);
byte[]buffer=Encoding.ASCII.GetBytes(数据);
//发送数据包
Socket Socket=新套接字(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
while(true)
{
SendTo(缓冲区、端点);
}
}
}
}
  • 在C中移植LOIC Android应用程序#
-这是网络调试的最佳工具。您可以按UDP和端口进行过滤,它将为您提供所有数据包和头的详细分类,包括源和目标IP。

-t