用C#实现UDP服务器

对UDP服务器的要求

            如同TCP通信一样让UDP服务端可以服务多个客户端
            需要具备的条件:
            1.区分消息类型(不需要处理分包、黏包)
            2.能够接收多个客户端的消息
            3.能够主动给自己发过消息的客户端发消息(记录客户端信息)
            4.主动记录上次收到客户端消息的时间,如果长时间没有收到消息,主动移除记录的客户端信息

            分析:
            1.UDP是无连接的,我们如何记录连入的客户端
            2.UDP收发消息都是通过一个Socket来处理,我们应该如何和处理收发消息
            3.如果不使用心跳消息,如何记录上次收到消息的时间

基本数据类--封装序列化和反序列化等方法

此代码定义了一个抽象基类BaseData,其中包含抽象方法用于获取字节数组容器大小、序列化和反序列化成员变量,还提供了一系列受保护的方法用于在字节数组和不同数据类型(如intshortlong等)及字符串、BaseData子类对象之间进行读写操作。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;public abstract class BaseData
{//用于子类重写的 获取字节数组容器大小的方法public abstract  int GetBytesNum();//把成员变量序列化为对应的字节数组public abstract byte[] Writing();public abstract int Reading(byte[] bytes, int beginIndex=0);//bytes指定的字节数组//value具体的int值//index索引位置的变量protected void WriteInt(byte []bytes,int value,ref int index){BitConverter.GetBytes(value).CopyTo(bytes, index);index += sizeof(int);}protected void WriteShort(byte[]bytes,short value,ref int index){BitConverter.GetBytes(value).CopyTo(bytes, index);index += sizeof(short);}protected void WriteLong(byte[]bytes,long value,ref int index){BitConverter.GetBytes(value).CopyTo(bytes, index);index += sizeof(long);}protected void WriteFloat(byte[] bytes, float value, ref int index){BitConverter.GetBytes(value).CopyTo(bytes, index);index += sizeof(float);}protected void WriteByte(byte[]bytes,byte value,ref int index){bytes[index] = value;index += sizeof(byte);}protected void WriteBool(byte[] bytes, bool value, ref int index){BitConverter.GetBytes(value).CopyTo(bytes, index);index += sizeof(bool);}protected void WriteString(byte[]bytes,string value,ref int index){//先存储string字节数组的长度byte[] strBytes = Encoding.UTF8.GetBytes(value);//BitConverter.GetBytes(strBytes.Length).CopyTo(bytes, index);//index += sizeof(int);WriteInt(bytes, strBytes.Length, ref index);//再存string字节数组strBytes.CopyTo(bytes, index);index += strBytes.Length;}protected void WriteData(byte[]bytes,BaseData data,ref int index){data.Writing().CopyTo(bytes, index);index += data.GetBytesNum();}protected int ReadInt(byte[]bytes,ref int index){int value = BitConverter.ToInt32(bytes, index);index += 4;return value;}protected short ReadShort(byte[] bytes, ref int index){short value = BitConverter.ToInt16(bytes, index);index += 2;return value;}protected long ReadLong(byte[] bytes, ref int index){long value = BitConverter.ToInt64(bytes, index);index += 8;return value;}protected float ReadFloat(byte[] bytes, ref int index){float value = BitConverter.ToSingle(bytes, index);index += sizeof(float);return value;}protected byte ReadByte(byte[] bytes, ref int index){byte value = bytes[index];index += 1;return value;}protected bool ReadBool(byte[] bytes, ref int index){bool value = BitConverter.ToBoolean(bytes, index);index += sizeof(bool);return value;}protected string ReadString(byte[] bytes, ref int index){int length = ReadInt(bytes, ref index);string value = Encoding.UTF8.GetString(bytes, index, length);index += length;return value;}protected T ReadData<T>(byte[] bytes, ref int index) where T : BaseData, new(){T value = new T();index+= value.Reading(bytes,index);return value;}
}

基本消息类

这段代码定义了一个名为BaseMsg的类,它继承自BaseData类。BaseMsg类重写了BaseData的抽象方法GetBytesNumReadingWriting,但这些重写方法只是简单抛出NotImplementedException异常,表明目前未实现具体逻辑。此外,BaseMsg类还定义了一个虚方法GetID,默认返回 0。

BaseMsg类的设计目的主要是作为消息类的基类,为后续具体消息类的实现提供统一的接口和结构框架。

using System.Collections;
using System.Collections.Generic;public class BaseMsg : BaseData
{public override int GetBytesNum(){throw new System.NotImplementedException();}public override int Reading(byte[] bytes, int beginIndex = 0){throw new System.NotImplementedException();}public override byte[] Writing(){throw new System.NotImplementedException();}public virtual int GetID(){return 0;}
}

玩家信息类

这段代码定义了一个名为PlayerMsg的类,它继承自BaseMsg类。PlayerMsg类代表了与玩家相关的消息,并且实现了消息的序列化和反序列化功能。

using System.Collections;
using System.Collections.Generic;public class PlayerMsg : BaseMsg
{public int playerID;public PlayerData playerData;public override int GetBytesNum(){return 4 +//消息ID4 +//playerID长度playerData.GetBytesNum();//消息的长度}public override int GetID(){return 1001;}public override int Reading(byte[] bytes, int beginIndex = 0){//反序列化不需要去解析ID,因为在这一步之前,就应该将ID反序列化出来//用来判断到底使用哪一个自定义类来反序列化int index = beginIndex;playerID = ReadInt(bytes, ref index);playerData = ReadData<PlayerData>(bytes, ref index);return index - beginIndex;}public override byte[] Writing(){int index = 0;byte[] playerBytes = new byte[GetBytesNum()];//先写消息IDWriteInt(playerBytes, GetID(), ref index);WriteInt(playerBytes, playerID, ref index);WriteData(playerBytes, playerData, ref index);return playerBytes;}
}
using System.Collections;
using System.Collections.Generic;
using System.Text;public class PlayerData : BaseData 
{public string name; public int lev;public int atk;public override int GetBytesNum(){return 4 + 4 + 4 + Encoding.UTF8.GetBytes(name).Length;}public override int Reading(byte[] bytes, int beginIndex = 0){int index = beginIndex;name=ReadString(bytes, ref index);lev=ReadInt(bytes, ref index);atk=ReadInt(bytes, ref index);return index - beginIndex;}public override byte[] Writing(){int index = 0;byte[] bytes = new byte[GetBytesNum()];WriteString(bytes, name, ref index);WriteInt(bytes, lev, ref index);WriteInt(bytes, atk, ref index);return bytes;}
}

这段代码定义了一个名为PlayerData的类,它继承自BaseData类。PlayerData类的作用是用来表示玩家的相关数据,并且实现了这些数据的序列化与反序列化功能。

服务端类

这段代码定义了一个名为ServerSocket的类,用于构建基于 UDP 协议的服务器,它能通过绑定指定 IP 和端口启动服务,利用线程池实现消息接收与客户端超时检查,将客户端信息存储在字典中,可处理新客户端连接,接收客户端消息并交予对应客户端对象处理,支持向指定客户端发送消息、向所有客户端广播消息,还能移除超时或指定的客户端。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace UDPServerExerise
{class ServerSocket{public Socket socket;private bool IsClose;//我们可以通过记录谁给我们发了消息 把它的IP和端口记录下来 这样就认为他是我的客户端了private Dictionary<string, Client> clientDic = new Dictionary<string, Client>();public void Start(string ip,int port){socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(ip), port);try{socket.Bind(ipPoint);IsClose = false;}catch (Exception e){Console.WriteLine("UDP开启错误" + e.Message);}//接收消息,使用线程池ThreadPool.QueueUserWorkItem(ReceiveMsg);//检测超时的线程ThreadPool.QueueUserWorkItem(CheakTimeOut);}private void CheakTimeOut(object obj){long nowTime=0;List<string> delClient = new List<string>();while (true){//30秒检查一次Thread.Sleep(30000);//得到当前系统时间nowTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;foreach (Client c in clientDic .Values){//超过十秒没有 收到消息的客户端需要被移除if(nowTime -c.frontTime >=10){delClient.Add(c.clientID);}}//从待删除列表中删除超时客户端for (int i = 0; i < delClient.Count; i++)RemoveClient(delClient[i]);delClient.Clear();}}private void ReceiveMsg(object obj){byte[] bytes = new byte[512];//记录谁发的string strID = "";string ip;int port;EndPoint ipPoint = new IPEndPoint(IPAddress.Any, 0);while (!IsClose){if(socket.Available >0){lock(socket)socket.ReceiveFrom(bytes, ref ipPoint);//处理消息 最好不要直接在这里处理,而是交给客户端对象处理//收到消息时,我们要判断 是不是记录了这个客户端的信息(ip和端口)//出去发送消息给我的IP和端口ip = (ipPoint as IPEndPoint).Address.ToString();port = (ipPoint as IPEndPoint).Port;strID = ip + port;//拼接成唯一一个ID这是我们自定义的规则//判断有没有记录这个客户端的信息,如果有直接用它处理信息if(clientDic .ContainsKey (strID )){clientDic[strID].ReceiveMsg(bytes);}else//如果没有 直接添加并处理消息{clientDic.Add(strID, new Client(ip, port));clientDic[strID].ReceiveMsg(bytes);}}}}public void SendTo(BaseMsg msg,IPEndPoint ipPoint){try{lock (socket)socket.SendTo(msg.Writing(), ipPoint);}catch (SocketException s){Console.WriteLine("发消息出现问题" + s.SocketErrorCode + s.Message);}catch (Exception e){Console.WriteLine("发消息出现问题(可能是序列化的问题)" + e.Message);}}private void Close(){if(socket!=null){socket.Shutdown(SocketShutdown.Both);socket.Close();IsClose = true;socket = null;}}public void BoardCast(BaseMsg msg){//广播给谁foreach (Client c in clientDic .Values){SendTo(msg,c.ipAndPoint);}}public void RemoveClient(string clientID){if(clientDic .ContainsKey (clientID)){Console.WriteLine("客户端{0}被移除了", clientID);clientDic.Remove(clientID);}}}
}

客户端类

这段代码定义了Client类,用于处理 UDP 服务器端接收到的来自客户端的消息。Client类的构造函数通过传入的 IP 和端口创建IPEndPoint对象并生成唯一的客户端 ID;ReceiveMsg方法接收消息字节数组,拷贝消息到新数组,记录消息接收时间,并将消息处理任务放入线程池;ReceiceHandleMsg方法从消息字节数组中解析消息类型、长度和消息体,针对不同消息 ID(如 1001 对应PlayerMsg消息,1003 对应quitMsg消息)进行相应处理,如反序列化PlayerMsg并输出相关信息,处理quitMsg时移除对应客户端,若处理消息出错也会移除该客户端。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace UDPServerExerise
{class Client{public IPEndPoint ipAndPoint;public string clientID;public float frontTime = -1;public Client (string ip,int port){//规则和外边一样 记录唯一ID 通过ip和port拼接的形式clientID = ip + port;//把客户端的信息记录下来ipAndPoint = new IPEndPoint(IPAddress.Parse(ip), port);}public void ReceiveMsg(byte[]bytes){//为了避免处理消息时又接收到了新的消息 所以我们需要在处理消息前 先把消息拷贝出来//处理消息和接收消息用不同容器 避免发生冲突byte[] cacheBytes = new byte[512];bytes.CopyTo(cacheBytes, 0);//记录发消息的系统时间frontTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;ThreadPool.QueueUserWorkItem(ReceiceHandleMsg, cacheBytes);}private void ReceiceHandleMsg(object obj){try{byte[] bytes = obj as byte[];int nowIndex = 0;//解析消息类型int msgID = BitConverter.ToInt32(bytes, nowIndex);nowIndex += 4;//解析消息长度int length = BitConverter.ToInt32(bytes, nowIndex);nowIndex += 4;//解析消息体switch (msgID){case 1001:PlayerMsg playerMsg = new PlayerMsg();playerMsg.Reading(bytes, nowIndex);Console.WriteLine(playerMsg.playerID);Console.WriteLine(playerMsg.playerData.lev);Console.WriteLine(playerMsg.playerData.atk);Console.WriteLine(playerMsg.playerData.name);break;case 1003:quitMsg quitMsg = new quitMsg();//由于它没有消息体 所以不用反序列化//quitMsg.Reading(bytes, nowIndex);//处理退出Program.serverSocket.RemoveClient(clientID);break;}}catch (Exception e){Console.WriteLine("处理消息出错" + e.Message);//如果出错了,就不用记录客户端的信息了Program.serverSocket.RemoveClient(clientID);}}}
}

退出消息类

这段代码定义了一个名为quitMsg的类,它继承自BaseMsg类,用于表示退出消息,重写了GetBytesNum方法指定消息字节数为 8,重写GetID方法返回消息唯一标识符 1003,重写Reading方法调用基类方法进行反序列化,重写Writing方法将消息 ID 和消息体长度(这里设为 0)序列化为字节数组。

using System.Collections;
using System.Collections.Generic;public class quitMsg : BaseMsg
{public override int GetBytesNum(){return 8;}public override int GetID(){return 1003;}public override int Reading(byte[] bytes, int beginIndex = 0){return base.Reading(bytes, beginIndex);}public override byte[] Writing(){int index = 0;byte[] bytes = new byte[GetBytesNum()];WriteInt(bytes, GetID(), ref index);WriteInt(bytes, 0, ref index);return bytes;}
}

主函数启动服务器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace UDPServerExerise
{class Program{public static ServerSocket serverSocket;static void Main(string[] args){serverSocket = new ServerSocket();serverSocket.Start("127.0.0.1", 8080);Console.WriteLine("UDP服务器启动了");string input = Console.ReadLine();if(input.Substring (0,2)=="B:"){PlayerMsg msg = new PlayerMsg();msg.playerData = new PlayerData();msg.playerID = 1001;msg.playerData.atk = 999;msg.playerData.lev = 88;msg.playerData.name ="DamnF的服务器";serverSocket.BoardCast(msg);}}}
}

成功运行程序--等待客户端通信

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/73504.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何在 Postman 中发送 PUT 请求?

在 Postman 中发送 PUT 请求的步骤相对简单&#xff0c;包括新建接口、选择 PUT 方法、填写 URL 和参数等几个主要步骤。 Postman 发送 put 请求教程

charles抓包软件免费使用教程

本文将给大家介绍Charles破解教程&#xff0c;支持Windows和Mac系统&#xff0c;操作简单&#xff0c;永久免费使用。同时&#xff0c;我们也会提到另一款强大的抓包工具——SniffMaster&#xff08;抓包大师&#xff09;&#xff0c;它在网络调试和数据包分析方面同样表现出色…

卷积神经网络 - 参数学习

本文我们通过两个简化的例子&#xff0c;展示如何从前向传播、损失计算&#xff0c;到反向传播推导梯度&#xff0c;再到参数更新&#xff0c;完整地描述卷积层的参数学习过程。 一、例子一 我们构造一个非常简单的卷积神经网络&#xff0c;其结构仅包含一个卷积层和一个输出…

.NET三层架构详解

.NET三层架构详解 文章目录 .NET三层架构详解引言什么是三层架构表示层&#xff08;Presentation Layer&#xff09;业务逻辑层&#xff08;Business Logic Layer&#xff0c;BLL&#xff09;数据访问层&#xff08;Data Access Layer&#xff0c;DAL&#xff09; .NET三层架构…

Redis实战常用二、缓存的使用

一、什么是缓存 在实际开发中,系统需要"避震器"&#xff0c;防止过高的数据访问猛冲系统,导致其操作线程无法及时处理信息而瘫痪. 这在实际开发中对企业讲,对产品口碑,用户评价都是致命的。所以企业非常重视缓存技术; 缓存(Cache)&#xff1a;就是数据交换的缓冲区&…

STM32八股【2】-----ARM架构

1、架构包含哪几部分内容 寄存器处理模式流水线MMU指令集中断FPU总线架构 2、以STM32为例进行介绍 2.1 寄存器 寄存器名称作用R0-R3通用寄存器用于数据传递、计算及函数参数传递&#xff1b;R0 也用于存储函数返回值。R4-R12通用寄存器用于存储局部变量&#xff0c;减少频繁…

effective Java 学习笔记(第二弹)

effective Java 学习笔记&#xff08;第一弹&#xff09; 整理自《effective Java 中文第3版》 本篇笔记整理第3&#xff0c;4章的内容。 重写equals方法需要注意的地方 自反性&#xff1a;对于任何非空引用 x&#xff0c;x.equals(x) 必须返回 true。对称性&#xff1a;对于…

mac命令行快捷键

光标移动 Ctrl A: 将光标移动到行首。Ctrl E: 将光标移动到行尾。Option 左箭头: 向左移动一个单词。Option 右箭头: 向右移动一个单词。 删除和修改 Ctrl K: 删除从光标到行尾的所有内容。Ctrl U: 删除从光标到行首的所有内容。Ctrl W: 删除光标前的一个单词。Ctrl …

CentOS 7部署主域名服务器 DNS

1. 安装 BIND 服务和工具 yum install -y bind bind-utils 2. 配置 BIND 服务 vim /etc/named.conf 修改以下配置项: listen-on port 53 { any; }; # 监听所有接口allow-query { any; }; # 允许所有设备查询 3 . 添加你的域名区域配置 …

优化 SQL 语句方向和提升性能技巧

优化 SQL 语句是提升 MySQL 性能的关键步骤之一。通过优化 SQL 语句,可以减少查询时间、降低服务器负载、提高系统吞吐量。以下是优化 SQL 语句的方法、策略和技巧: 一、优化 SQL 语句的方法 1. 使用 EXPLAIN 分析查询 作用:查看 SQL 语句的执行计划,了解查询是如何执行的…

C++ 多线程简要讲解

std::thread是 C11 标准库中用于多线程编程的核心类&#xff0c;提供线程的创建、管理和同步功能。下面我们一一讲解。 一.构造函数 官网的构造函数如下&#xff1a; 1.默认构造函数和线程创建 thread() noexcept; 作用&#xff1a;创建一个 std::thread 对象&#xff0c;但…

Vscode HTML5新增元素及属性

一、‌HTML5 语义化标签 HTML5 语义化标签&#xff08;Semantic Elements&#xff09;是一组 ‌具有明确含义的 HTML 元素‌&#xff0c;通过标签名称直接描述其内容或结构的功能&#xff0c;而非仅作为样式容器&#xff08;如 <div> 或 <span>&#xff09;。它们旨…

【PostgreSQL教程】PostgreSQL 特别篇之 语言接口Python

博主介绍:✌全网粉丝22W+,CSDN博客专家、Java领域优质创作者,掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域✌ 技术范围:SpringBoot、SpringCloud、Vue、SSM、HTML、Nodejs、Python、MySQL、PostgreSQL、大数据、物联网、机器学习等设计与开发。 感兴趣的可…

Three学习入门(四)

9-Three.js 贴图与材质学习指南 环境准备 <!DOCTYPE html> <html> <head><title>Three.js Texture Demo</title><style> body { margin: 0; } </style> </head> <body><script src"https://cdnjs.cloudflare.…

前端NVM安装

https://v0.dev/chat/settings 本地启动环境 1安装 nvm 2安装node nvm install v18.19.0 nvm install v20.9.0 nvm use 18 node -v 3安装 pnpm npm install -g pnpm 或者 npm i -g pnpm 4启动 代码 目录下 执行 pnpm i pnpm run dev 4.1到代码目录下 4.2直接cmd…

蓝桥杯算法精讲:二分查找实战与变种解析

适合人群&#xff1a;蓝桥杯备考生 | 算法竞赛入门者 | 二分查找进阶学习者 目录 一、二分查找核心要点 1. 算法思想 2. 适用条件 3. 算法模板 二、蓝桥杯真题实战 例题&#xff1a;分巧克力&#xff08;蓝桥杯2017省赛&#xff09; 三、二分查找变种与技巧 1. 查找左边…

cmd命令查看电脑的CPU、内存、存储量

目录 获取计算机硬件的相关信息的命令分别的功能结果展示结果说明获取计算机硬件的相关信息的命令 wmic cpu get name wmic memorychip get capacity wmic diskdrive get model,size,mediaType分别的功能 获取计算机中央处理器(CPU)的名称 获取计算机内存(RAM)芯片的容量…

SCI论文阅读指令(特征工程)

下面是一个SCI论文阅读特征工程V3.0&#xff0c;把指令输入大模型中&#xff0c;并上传PDF论文&#xff0c;就可以帮你快速阅读论文。 优先推荐kimi&#xff0c;当然DeepSeek、QwQ-32B等大语言模型也可以。测试了一下总结的还不错&#xff0c;很详细。 请仔细并深入地阅读所提…

如何监控 SQL Server

监控 SQL Server 对于维护数据库性能、确保数据可用性和最大限度地减少停机时间至关重要。随着企业越来越依赖数据驱动的决策&#xff0c;高效的SQL Server监控策略能显著提升组织生产力和用户满意度。 为什么要监控 SQL Server SQL Server 是许多关键应用程序的支柱&#xf…

python脚本处理excel文件

1.对比perl和python 分别尝试用perl和python处理excel文件&#xff0c;发现perl的比较复杂&#xff0c;比如说read excel就有很多方式 Spreadsheet::Read use Spreadsheet::ParseExcel 不同的method&#xff0c;对应的取sheet的cell方式也不一样。更复杂的是处理含有中文内…