C#udpClient组播

一、0udpClient

控件:

button(打开,关闭,发送),textbox,richTextBox

打开UDP:

UdpClient udp:

cs 复制代码
namespace _01udpClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //打开
        UdpClient udp;
        private void button1_Click(object sender, EventArgs e)
        {
           //1创建udp对象 指定ip地址和端口号
            udp = new UdpClient(new IPEndPoint(IPAddress.Any, 8080));

            //2 接收数据
            startReceive();

        }
        void startReceive()
        {
            new Thread(() =>
            {
                try
                {
                    while (true)
                    {
                        //创建ip接受客户端的ip地址
                        IPEndPoint ip = null;
                        //接收数据 返回字节数组
                        byte[] body =  udp.Receive(ref ip);
                        string s = Encoding.UTF8.GetString(body);
                        BeginInvoke((Action)(() =>
                        {
                            richTextBox1.AppendText(ip.ToString() + ":" + s + "\t\n");
                        }));
                    }
                }
                catch
                {

                }

            }).Start();
        }
        //关闭
        private void button2_Click(object sender, EventArgs e)
        {
            udp.Close();//关闭
            udp = null;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            byte[] bs = Encoding.UTF8.GetBytes(this.textBox1.Text);
            //发数据
            //参数1 字节数组
            //参数2 字节长度
            //参数3 目标主机地址
            //参数4 端口号
            udp.Send(bs, bs.Length, "192.168.107.71", 8080);

        }
    }
}

二、udpClient组播

cs 复制代码
namespace _02udpClinet组播
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //打开服务器
        private void button3_Click(object sender, EventArgs e)
        {
           
            udp = new UdpClient(new IPEndPoint(IPAddress.Any, 8080));
            strartReceive();
          
        }
        UdpClient udp;
        //异步的方式
        //1 new Thread() 分线程
        //2 Task.Run() 异步任务
        //3 async(异步)和await (等待)
        async void strartReceive()
        {
            while (true)
            {
                //await 跟一个异步的任务
                // 等待异步结束之后 再去执行
                //receiveAsync() 异步接收数据
                UdpReceiveResult body = await udp.ReceiveAsync();
                // body.RemoteEndPoint 远程终端
                //body.Buffer 数据字节数组
                BeginInvoke((Action)(() =>
                {
                    richTextBox1.AppendText(body.RemoteEndPoint.ToString() + ":" + Encoding.UTF8.GetString(body.Buffer)+"\t\n");
                }));
            }

        }

        // 加入组播
        private void button1_Click(object sender, EventArgs e)
        {
            //Join 加入
             udp.JoinMulticastGroup(IPAddress.Parse(this.textBox1.Text));//加入组播地址
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //发送消息
           byte[] bs =  Encoding.UTF8.GetBytes(this.textBox2.Text);
            udp.Send(bs, bs.Length, this.textBox1.Text, 8080);
        }
    }
}
相关推荐
前端小巷子2 分钟前
Web 实时通信:从短轮询到 WebSocket
前端·javascript·面试
神仙别闹5 分钟前
基于C#+SQL Server实现(Web)学生选课管理系统
前端·数据库·c#
web前端神器12 分钟前
指定阿里镜像原理
前端
枷锁—sha16 分钟前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
枷锁—sha18 分钟前
跨站请求伪造漏洞(CSRF)详解
运维·服务器·前端·web安全·网络安全·csrf
群联云防护小杜34 分钟前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
汉得数字平台1 小时前
【鲲苍提效】全面洞察用户体验,助力打造高性能前端应用
前端·前端监控
向宇it1 小时前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
花海如潮淹1 小时前
前端性能追踪工具:用户体验的毫秒战争
前端·笔记·ux
2301_780789665 小时前
UDP和TCP的主要区别是什么
服务器·网络协议·web安全·网络安全·udp