2023年8月3日发(作者:)
C#Socket通信通过XML⽂件(序列化与反序列化)传输数据今天帮基友做了⼀个⼤作业,题⽬是这样的:
多数据采集设计与实现(TCP)有⼀个数据采集点(服务器)需要采集多个数据(温度、湿度、光照和P2.5数值)。该数值格式类似XML形式:
static byte[] FileTOByte(String path) { FileStream fs = new FileStream(path, , ); byte[] infbytes = new byte[(int)]; (infbytes, 0, ); (); return infbytes; } }}服务器端:using System;using c;using ;namespace 服务器端{ public class Data { public string collectid { get; set; } public string temperature { get; set; } public string humidity { get; set; } public string Lumen { get; set; } public string aqi { get; set; } }}using System;using ;using ;using s;using ;using ization;namespace 服务器端{ class ServerSocket { private Socket serverSocket; private object serializer; public ServerSocket(String ip,int port) { serverSocket = new Socket(etwork, , ); (new IPEndPoint((ip),port)); (0); ine("开始监听。。。"); } public void ReceiveMsg() { byte[] msgByte = new byte[1024]; Socket clientSocket = (); ine("有⼀台设备连接。。。"); e(msgByte); FileStream fs = new FileStream("C:/_E/", , ); (msgByte, 0, ); (); Data data; XmlSerializer serializer = new XmlSerializer(typeof(Data)); using (StreamReader streamReader = xt( "C:/_E/")) { data = alize(streamReader) as Data; } ine("collectid:"+tid); } }}using System;namespace 服务器端{ class Program { private static String IP = "127.0.0.1"; private static int Port = 5588; static void Main(string[] args) { ServerSocket socket = new ServerSocket(IP,Port); eMsg(); y(); } }}额,简单来说,就是这样了。。。我简单的写了下服务器端的接收字节流的⽅法,标准的应该还有防⽌粘包什么的,并且数据包的前四个字节要存着字节数组的长度,防⽌出现乱码,但是傲娇如我,啥都不写,哈哈,因为我估计了下,最长的数据包不会⼤于1024字节,就省去了什么循环接收什么的。。。主要涉及到的知识点也不多,简单总结点:类的使⽤⽂件的序列化与反序列化(没错,,,没了)。。
2023年8月3日发(作者:)
C#Socket通信通过XML⽂件(序列化与反序列化)传输数据今天帮基友做了⼀个⼤作业,题⽬是这样的:
多数据采集设计与实现(TCP)有⼀个数据采集点(服务器)需要采集多个数据(温度、湿度、光照和P2.5数值)。该数值格式类似XML形式:
static byte[] FileTOByte(String path) { FileStream fs = new FileStream(path, , ); byte[] infbytes = new byte[(int)]; (infbytes, 0, ); (); return infbytes; } }}服务器端:using System;using c;using ;namespace 服务器端{ public class Data { public string collectid { get; set; } public string temperature { get; set; } public string humidity { get; set; } public string Lumen { get; set; } public string aqi { get; set; } }}using System;using ;using ;using s;using ;using ization;namespace 服务器端{ class ServerSocket { private Socket serverSocket; private object serializer; public ServerSocket(String ip,int port) { serverSocket = new Socket(etwork, , ); (new IPEndPoint((ip),port)); (0); ine("开始监听。。。"); } public void ReceiveMsg() { byte[] msgByte = new byte[1024]; Socket clientSocket = (); ine("有⼀台设备连接。。。"); e(msgByte); FileStream fs = new FileStream("C:/_E/", , ); (msgByte, 0, ); (); Data data; XmlSerializer serializer = new XmlSerializer(typeof(Data)); using (StreamReader streamReader = xt( "C:/_E/")) { data = alize(streamReader) as Data; } ine("collectid:"+tid); } }}using System;namespace 服务器端{ class Program { private static String IP = "127.0.0.1"; private static int Port = 5588; static void Main(string[] args) { ServerSocket socket = new ServerSocket(IP,Port); eMsg(); y(); } }}额,简单来说,就是这样了。。。我简单的写了下服务器端的接收字节流的⽅法,标准的应该还有防⽌粘包什么的,并且数据包的前四个字节要存着字节数组的长度,防⽌出现乱码,但是傲娇如我,啥都不写,哈哈,因为我估计了下,最长的数据包不会⼤于1024字节,就省去了什么循环接收什么的。。。主要涉及到的知识点也不多,简单总结点:类的使⽤⽂件的序列化与反序列化(没错,,,没了)。。
发布评论