C#正在发送UDP包
本文关键字:UDP | 更新日期: 2023-09-27 18:28:47
我想知道,他们有没有任何方法可以检查您的程序是否正在将UDP
包发送到所需的IP
?我是一个初学者套接字程序员。所以,如果你决定帮助我,请详细解释一下。我才15岁,学习c#
才2个月。
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应用程序#
https://www.wireshark.org/-这是有史以来用于网络调试的最佳工具。您可以通过UDP&端口,它会给你所有数据包的详细分类&标头,包括源&目的地IP。