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

基于Winform的UDP通信

1、文件结构
在这里插入图片描述
2、UdpReceiver.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;namespace UDPTest.Udp
{public class UdpStateEventArgs : EventArgs{public IPEndPoint remoteEndPoint;public byte[] buffer = null;}public delegate void UdpReceivedEventHandler(UdpStateEventArgs args);public class UdpReceiver{private UdpClient udpClient;public event UdpReceivedEventHandler UdpReceived;public UdpReceiver(string localIp,int localPort, string remoteIp, int remotePort){IPEndPoint localPoint = new IPEndPoint(IPAddress.Parse(localIp), localPort);udpClient = new UdpClient(localPoint);Task.Run(() =>{while (true){UdpStateEventArgs args = new UdpStateEventArgs();if (udpClient != null){IPEndPoint remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), remotePort);var received = udpClient.Receive(ref remotePoint);args.remoteEndPoint = remotePoint;args.buffer = received;UdpReceived?.Invoke(args);}else{break;}}});}}
}

3、ControlCross.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace UDPTest.Utils
{internal class ControlCross{public static void LableSetText(Label label, string text){if (label.InvokeRequired){MethodInvoker mi = delegate (){label.Text = text;};label.Invoke(mi);}else{label.Text = text;}}}
}

4、MainForm窗口
在这里插入图片描述
5、MainForm.cs

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;
using UDPTest.Udp;
using UDPTest.Utils;namespace UDPTest
{public partial class MainForm : Form{public MainForm(){InitializeComponent();}string localIp="127.0.0.1";int localPort = 7777;string remoteIp = "127.0.0.1";int remotePort = 8081;string remotePointInfo = "";string receiveBuffer = "";private void btStartTest_Click(object sender, EventArgs e){UdpReceiver udpReceiver=new UdpReceiver(localIp, localPort, remoteIp, remotePort);udpReceiver.UdpReceived += UdpReceiverProcess;}private void UdpReceiverProcess(UdpStateEventArgs args){var remotePoint = args.remoteEndPoint;remotePointInfo = $"{remotePoint.Address}:{remotePoint.Port}";receiveBuffer = Encoding.UTF8.GetString(args.buffer);ControlCross.LableSetText(lbTest1, remotePointInfo);ControlCross.LableSetText(lbTest2, receiveBuffer);}private UdpClient udpClient = null;private void btSendSet_Click(object sender, EventArgs e){IPEndPoint localPoint = new IPEndPoint(IPAddress.Parse(localIp), localPort);udpClient = new UdpClient(localPoint);}private void btSendTest_Click(object sender, EventArgs e){string str=this.tbSendData.Text;byte[] buffer=Encoding.UTF8.GetBytes(str);if(udpClient != null){IPEndPoint remotePoint = new IPEndPoint(IPAddress.Parse(remoteIp), remotePort);udpClient.Send(buffer,buffer.Length,remotePoint);}}}
}

6、运行代码
在这里插入图片描述

http://www.lryc.cn/news/186816.html

相关文章:

  • 掌握 BERT:自然语言处理 (NLP) 从初级到高级的综合指南(1)
  • Linux Ftrace介绍
  • Go语言进阶------>init()函数
  • 云计算:常用微服务框架
  • jmeter添加断言(详细图解)
  • few shot object detection via feature reweight笔记
  • 工会排队模式:电商新营销模式吸引消费者,提升销售!
  • 定档通知2024中国(北京)国际红外技术及设备展览会
  • 自助建站系统,一建建站系统api版,自动建站
  • 算法框架-LLM-1-Prompt设计(一)
  • 一个rar压缩包如何分成三个?
  • 批量获取拼多多商品详情数据,拼多多商品详情API接口
  • Redis Cluster Gossip Protocol: 目录
  • HarmonyOS/OpenHarmony原生应用-ArkTS万能卡片组件Span
  • 这些负载均衡都解决哪些问题?服务、网关、NGINX
  • Lambda表达式在C++中的定义
  • sheng的学习笔记-【中文】【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第二周测验
  • 前端代码格式化规范总结
  • Windows10打开应用总是会弹出提示窗口的解决方法
  • 易点易动固定资产管理系统: 帮助您应对2023年年终固定资产大盘点
  • OpenGLES:绘制一个混色旋转的3D立方体
  • Maven(4)-利用intellij idea创建maven 多模块项目
  • 8年测试老鸟,性能测试-数据库连接池问题定位/分析,一篇打通...
  • 【Sentinel】Sentinel原码分析
  • 计算机竞赛 题目:基于深度学习的人脸表情识别 - 卷积神经网络 竞赛项目 代码
  • 基于aarch64分析kernel源码 五:idle进程(0号进程)
  • 【Linux】 vi / vim 使用
  • Leetcode hot 100之双指针(快慢指针、滑动窗口)
  • Bridge Champ助力我国桥牌阔步亚运, Web3游戏为传统项目注入创新活力
  • 云原生微服务 第六章 Spring Cloud中使用OpenFeign