因为在c#中udp服务端和客户端区别不大,详细的可以去看看udp和tcp的区别
winform界面
其中一个代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace udp客户端
{
public partial class Form1 : Form
{
// 用于 UDP 通信的客户端对象
UdpClient udpClient;
// 用于取消任务的令牌源
CancellationTokenSource cts;
public Form1()
{
InitializeComponent();
// 在构造函数中初始化 UdpClient,绑定到指定的 IP 地址和端口(这里是 192.168.205.238 的 9999 端口)
udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.205.238"), 9999));
}
// 发送按钮的点击事件处理方法
private void button1_Click(object sender, EventArgs e)
{
// 创建新的取消令牌源
cts = new CancellationTokenSource();
// 在后台线程中执行发送任务
Task.Run(() =>
{
// 获取文本框中的输入内容
string str = textBox1.Text;
// 将输入内容转换为字节数组
byte[] data = Encoding.UTF8.GetBytes(str);
// 向指定的 IP 地址和端口(192.168.205.238 的 8888 端口)发送字节数组数据
udpClient.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("192.168.205.238"), 8888));
});
}
// 接收按钮的点击事件处理方法
private void button2_Click(object sender, EventArgs e)
{
// 创建新的取消令牌源
cts = new CancellationTokenSource();
// 在后台线程中执行接收任务
Task.Run(() =>
{
// 循环接收数据,直到取消令牌被触发
while (!cts.IsCancellationRequested)
{
// 创建一个 IP 端点对象,用于接收数据时存储发送方的 IP 地址和端口信息(这里初始化为 127.0.0.1 的 8888 端口,会被实际接收的数据覆盖)
IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
// 调用 UdpClient 的 Receive 方法接收数据,并将发送方的 IP 端点信息存储在 point 中
byte[] data = udpClient.Receive(ref point);
// 将接收到的字节数组转换为字符串
string str = Encoding.UTF8.GetString(data);
// 在 UI 线程中更新标签的文本内容为接收到的字符串
Invoke(new Action(() =>
{
label1.Text = str;
}));
}
});
}
}
}
另一个winform界面:
另一个代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace udp服务器
{
public partial class Form1 : Form
{
// 用于 UDP 通信的客户端对象,在服务器端这里也用 UdpClient 接收和发送数据
UdpClient udpClient;
// 用于取消任务的令牌源
CancellationTokenSource cts;
public Form1()
{
InitializeComponent();
// 在构造函数中初始化 UdpClient,绑定到指定的 IP 地址和端口(这里是 192.168.205.238 的 8888 端口)
udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.205.238"), 8888));
}
// 发送按钮的点击事件处理方法
private void button1_Click(object sender, EventArgs e)
{
// 创建新的取消令牌源
cts = new CancellationTokenSource();
// 在后台线程中执行发送任务
Task.Run(() =>
{
// 获取文本框中的输入内容
string str = textBox1.Text;
// 将输入内容转换为字节数组
byte[] data = Encoding.UTF8.GetBytes(str);
// 向指定的 IP 地址和端口(192.168.205.238 的 9999 端口)发送字节数组数据
udpClient.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("192.168.205.238"), 9999));
});
}
// 接收按钮的点击事件处理方法
private void button2_Click(object sender, EventArgs e)
{
// 创建新的取消令牌源
cts = new CancellationTokenSource();
// 在后台线程中执行接收任务
Task.Run(() =>
{
// 循环接收数据,直到取消令牌被触发
while (!cts.IsCancellationRequested)
{
// 创建一个 IP 端点对象,用于接收数据时存储发送方的 IP 地址和端口信息(这里初始化为 127.0.0.1 的 9999 端口,会被实际接收的数据覆盖)
IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
// 调用 UdpClient 的 Receive 方法接收数据,并将发送方的 IP 端点信息存储在 point 中
byte[] data = udpClient.Receive(ref point);
// 将接收到的字节数组转换为字符串
string str = Encoding.UTF8.GetString(data);
// 在 UI 线程中更新标签的文本内容为接收到的字符串
Invoke(new Action(() =>
{
label1.Text = str;
}));
}
});
}
}
}