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

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);
        }
    }
}

 


http://www.kler.cn/a/585203.html

相关文章:

  • 什么是 MyBatis?
  • 基于卡尔曼滤波的雷达光电多目标航迹融合算法matlab仿真
  • 基于ssm的宠物医院信息管理系统(全套)
  • 线程池参数调优
  • 游戏引擎学习第152天
  • AI时代研究卷积神经网络(CNN)工具与方法
  • 【即插即用涨点模块】CAA上下文锚点注意力机制:有效捕捉全局信息,助力高效涨点【附源码+注释】
  • 21天 - 说说 TCP 的四次挥手?TCP 的粘包和拆包能说说吗?说说 TCP 拥塞控制的步骤?
  • 谷歌 Gemini 2.0 Flash实测:1条指令自动出图+配故事!
  • el-table 插槽踩过的坑 :slot-scope 和#default的区别
  • 代码随想录-回溯
  • 如何优雅地将Collection转为Map?
  • 平安养老险广西分公司2025年“3∙15”金融消费者权益教育宣传活动暨南湖公园健步行活动
  • 【C语言】编译和链接详解
  • Redis的缓存雪崩、缓存击穿、缓存穿透与缓存预热、缓存降级
  • 2025-03-15 学习记录--C/C++-PTA 练习3-4 统计字符
  • 【3D视觉学习笔记2】摄像机的标定、畸变的建模、2D/3D变换
  • python如何获取三个小时之前的时间并输出
  • MATLAB 控制系统设计与仿真 - 26
  • python画图文字显示不全+VScode新建jupyter文件