C# Socket通信通过XML文件(序列化与反序列化)传输数据

今天帮基友做了一个大作业,题目是这样的:

 

多数据采集设计与实现(TCP)有一个数据采集点(服务器)需要采集多个数据(温度、湿度、光照和P2.5数值)。该数值格式类似XML形式:<data><collectid>123456</collectid><temperature>19</temperature><humidity>50.1</humidity><Lumen>2501</Lumen><aqi>85</aqi></data>客户端把数据提交给数据中心(客户端)服务器接到后把数据解析后打印出来

想了下,就产生了如下的两段代码(有注释哦):

客户端:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

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 System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Xml;
using System.Linq;
using System.Xml.Serialization;
using System.IO;

namespace 客户端
{
    class ClientSocket
    {
        private Socket clientSocket;
        public ClientSocket()
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }
        /// <summary>
        /// 连接服务器端
        /// </summary>
        public void ConnectToServer(String ip,int port)
        {
            clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
        }
        /// <summary>
        /// 向服务器端发送消息
        /// </summary>
        public void SendMSGToServer(byte[] msg)
        {
            clientSocket.Send(msg);
        }
    }
}
using System;
using System.IO;
using System.Net.Sockets;
using System.Xml;
using System.Xml.Serialization;

namespace 客户端
{
    class Program
    {
        private static String IP = "127.0.0.1";
        private static int Port = 5588;
        private static String xmlFilePath = "test.xml";
        static void Main(string[] args)
        {
            Data data = new Data();
            Console.WriteLine("please input the value of collectid:");
            data.collectid = Console.ReadLine();
            Console.WriteLine("please input the value of temperature:");
            data.temperature = Console.ReadLine();
            Console.WriteLine("please input the value of humidity:");
            data.humidity = Console.ReadLine();
            Console.WriteLine("please input the value of Lumen:");
            data.Lumen = Console.ReadLine();
            Console.WriteLine("please input the value of aqi:");
            data.aqi = Console.ReadLine();

            //序列化
            XmlSerializer serializer = new XmlSerializer(typeof(Data));
            using (StreamWriter streamWriter = File.CreateText(xmlFilePath))
            {
                serializer.Serialize(streamWriter, data);
            }
            ClientSocket clientSocket =new ClientSocket();
            clientSocket.ConnectToServer(IP,Port);
            clientSocket.SendMSGToServer(FileTOByte(xmlFilePath));
            Console.WriteLine("数据已发送。。。");
            Console.ReadKey();
        }
        
        static byte[] FileTOByte(String path)
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] infbytes = new byte[(int)fs.Length];
            fs.Read(infbytes, 0, infbytes.Length);
            fs.Close();
            return infbytes;
        }



    }
}

服务器端:

using System;
using System.Collections.Generic;
using System.Text;

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 System.IO;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.Xml.Serialization;


namespace 服务器端
{
    class ServerSocket
    {

        private Socket serverSocket;
        private object serializer;

        public ServerSocket(String ip,int port)
        {
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(new IPEndPoint(IPAddress.Parse(ip),port));
            serverSocket.Listen(0);
            Console.WriteLine("开始监听。。。");
        }
        public void ReceiveMsg()
        {
            byte[] msgByte = new byte[1024];
            Socket clientSocket = serverSocket.Accept();
            Console.WriteLine("有一台设备连接。。。");
            clientSocket.Receive(msgByte);
            FileStream fs = new FileStream("C:/_E/XMLFile2.xml", FileMode.Create, FileAccess.Write);
            fs.Write(msgByte, 0, msgByte.Length);
            fs.Close();
            Data data;
            XmlSerializer serializer = new XmlSerializer(typeof(Data));
            using (StreamReader streamReader = File.OpenText( "C:/_E/XMLFile2.xml"))
            {
                data = serializer.Deserialize(streamReader) as Data;
            }

            Console.WriteLine("collectid:"+data.collectid);
        }
    }
}
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);
            socket.ReceiveMsg();
            Console.ReadKey();
        }
    }
}

额,简单来说,就是这样了。。。

我简单的写了下服务器端的接收字节流的方法,标准的应该还有防止粘包什么的,并且数据包的前四个字节要存着字节数组的长度,防止出现乱码,但是傲娇如我,啥都不写,哈哈,因为我估计了下,最长的数据包不会大于1024字节,就省去了什么循环接收什么的。。。

主要涉及到的知识点也不多,简单总结点:

1.socket类的使用

2.xml文件的序列化与反序列化

(没错,,,没了)。。

 

 

  • 2
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值