当前位置: 首页 > news >正文

UdpClient

Socket实现Udp的发送和接收

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.Tasks;
using System.Windows.Forms;

namespace _1Socket实现udp的发送
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Socket soc;
        //打开
        private void button1_Click(object sender, EventArgs e)
        {
            soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            soc.Bind(new IPEndPoint(IPAddress.Parse("192.168.107.14"), 3030));

            Task.Run(() =>
            {
                while (true)
                {
                    byte[] buffer = new byte[1024];
                    EndPoint ip = new IPEndPoint(IPAddress.None, 0);// 初始化远程终端,没ip ,没端口
                    soc.ReceiveFrom(buffer, 0, ref ip); //接收了消息,获取远程终端
                    IPEndPoint ip1 = ip as IPEndPoint; //EndPoint的ip类型 转成IPEndPoint类型
                    BeginInvoke( new Action(() =>
                    {
                        richTextBox1.AppendText($"接收到了来自{ip1.ToString()}的消息:{Encoding.UTF8.GetString(buffer)}\r\n");
                    }));

                }
            });
        }
        //关闭
        private void button2_Click(object sender, EventArgs e)
        {
            soc.Close();
        }
        //发送
        private void button3_Click(object sender, EventArgs e)
        {
            soc.SendTo(Encoding.UTF8.GetBytes("9福星还有一个大铲子输了"), new IPEndPoint(IPAddress.Parse("192.168.107.46"), 3000));
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

 

UdpClient

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.Tasks;
using System.Windows.Forms;

namespace _2UDPClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        UdpClient udp;

        private void button1_Click(object sender, EventArgs e)
        {
            udp = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.107.14"), 3000));

            Task.Run(() =>
            {
                try
                {

                    while (true)
                    {
                        IPEndPoint ip = null;
                        byte[] bs = udp.Receive(ref ip);//接收数据 传递远程终端
                        string s = Encoding.UTF8.GetString(bs);
                        BeginInvoke(new Action(() =>
                        {
                            richTextBox1.AppendText(ip.ToString() + ":" + s + "\r\n");
                        }));
                    }

                }
                catch (Exception ex)
                {

                    MessageBox.Show("UDP已经关闭");
                }
            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            udp.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            byte[] bs = Encoding.UTF8.GetBytes("今天又周五了,明天可以休息了");
            udp.Send(bs,bs.Length,"192.168.107.210",8000);
        }
    }
}

 

相关文章:

  • Chat-TTS-UI:文字转语音 - 本地部署方案
  • 【K8s】 K8s Pod如何通过HostAliases字段配置Host
  • Jmeter下载及环境配置
  • LinuX---Shell脚本创建和执行
  • fastapi+angular实现Tcp在线聊天室功能
  • 大白话阐述react和vue高阶组件的概念、优势及应用场景,以及区别,给出简单高阶组件的实现代码
  • Django-ORM-select_related
  • 如何手动使用下载并且运行 QwQ-32B-GGUF
  • 文件上传安全
  • 大数据-spark3.5安装部署之standalone模式
  • 数据结构:栈的应用举例——进制转换
  • ACwing—跳台阶(bfs+dp+递归+记忆化搜索算法)
  • SpringBoot中使用AJ-Captcha实现行为验证码(滑动拼图、点选文字)
  • C语言零基础入门:嵌入式系统开发之旅
  • windows下安装docker desktop并指定安装目录及汉化
  • 系统可观测性(5)OpenTelemetry基础使用
  • Kotlin 继承
  • 银河麒麟V10ServerSP3中Redis7源码编译与安装详细教程
  • SQL basic commands
  • 【go】函数类型的作用
  • 市场监管总局等五部门约谈外卖平台企业
  • 智能手表眼镜等存泄密隐患,国安部提醒:严禁在涉密场所使用
  • 最美西游、三星堆遗址等入选“2025十大年度IP”
  • “无锡景・江南韵”:中国评弹艺术在尼日利亚收获众多粉丝
  • 佩斯科夫:俄会考虑30天停火提议,但试图对俄施压无用
  • 家电维修担心遇“李鬼”?上海推动“物业+专业服务”进社区