c#中UdpClient 通信

因为在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;
                    }));
                }
            });
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值