2023年8月3日发(作者:)

C#Socket通信通过XML⽂件(序列化与反序列化)传输数据今天帮基友做了⼀个⼤作业,题⽬是这样的:

多数据采集设计与实现(TCP)有⼀个数据采集点(服务器)需要采集多个数据(温度、湿度、光照和P2.5数值)。该数值格式类似XML形式:1234561950.1250185客户端把数据提交给数据中⼼(客户端)服务器接到后把数据解析后打印出来想了下,就产⽣了如下的两段代码(有注释哦):客户端:using System;using c;using ;using ;using ization;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 c;using ;using s;using ;using ;using ;using ization;using ;namespace 客户端{ class ClientSocket { private Socket clientSocket; public ClientSocket() { clientSocket = new Socket(etwork, , ); } ///

/// 连接服务器端 /// public void ConnectToServer(String ip,int port) { t(new IPEndPoint((ip), port)); } /// /// 向服务器端发送消息 /// public void SendMSGToServer(byte[] msg) { (msg); } }}using System;using ;using s;using ;using ization;namespace 客户端{ class Program { private static String IP = "127.0.0.1"; private static int Port = 5588; private static String xmlFilePath = ""; static void Main(string[] args) { Data data = new Data(); ine("please input the value of collectid:"); tid = ne(); ine("please input the value of temperature:"); ature = ne(); ine("please input the value of humidity:"); ty = ne(); ine("please input the value of Lumen:"); = ne(); ine("please input the value of aqi:"); = ne(); //序列化 XmlSerializer serializer = new XmlSerializer(typeof(Data)); using (StreamWriter streamWriter = Text(xmlFilePath)) { ize(streamWriter, data); } ClientSocket clientSocket =new ClientSocket(); tToServer(IP,Port); GToServer(FileTOByte(xmlFilePath)); ine("数据已发送。。。"); y(); }

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形式:1234561950.1250185客户端把数据提交给数据中⼼(客户端)服务器接到后把数据解析后打印出来想了下,就产⽣了如下的两段代码(有注释哦):客户端:using System;using c;using ;using ;using ization;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 c;using ;using s;using ;using ;using ;using ization;using ;namespace 客户端{ class ClientSocket { private Socket clientSocket; public ClientSocket() { clientSocket = new Socket(etwork, , ); } ///

/// 连接服务器端 /// public void ConnectToServer(String ip,int port) { t(new IPEndPoint((ip), port)); } /// /// 向服务器端发送消息 /// public void SendMSGToServer(byte[] msg) { (msg); } }}using System;using ;using s;using ;using ization;namespace 客户端{ class Program { private static String IP = "127.0.0.1"; private static int Port = 5588; private static String xmlFilePath = ""; static void Main(string[] args) { Data data = new Data(); ine("please input the value of collectid:"); tid = ne(); ine("please input the value of temperature:"); ature = ne(); ine("please input the value of humidity:"); ty = ne(); ine("please input the value of Lumen:"); = ne(); ine("please input the value of aqi:"); = ne(); //序列化 XmlSerializer serializer = new XmlSerializer(typeof(Data)); using (StreamWriter streamWriter = Text(xmlFilePath)) { ize(streamWriter, data); } ClientSocket clientSocket =new ClientSocket(); tToServer(IP,Port); GToServer(FileTOByte(xmlFilePath)); ine("数据已发送。。。"); y(); }

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字节,就省去了什么循环接收什么的。。。主要涉及到的知识点也不多,简单总结点:类的使⽤⽂件的序列化与反序列化(没错,,,没了)。。