高性能TcpServer(C#) - 4.文件通道(处理:文件分包,支持断点续传)

高性能TcpServer(C#) - 1.网络通信协议

高性能TcpServer(C#) - 2.创建高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)

高性能TcpServer(C#) - 3.命令通道(处理:掉包,粘包,垃圾包)

高性能TcpServer(C#) - 4.文件通道(处理:文件分包,支持断点续传)

高性能TcpServer(C#) - 5.客户端管理

高性能TcpServer(C#) - 6.代码下载

 

应用场景

 

升级程序

流程:终端->查询服务器版本比较->升级程序(获取包数,获取各包数据)->数据拼装生成文件->最后更新服务器上该设备的版本信息

分包代码段:

        static Dictionary<int, string> ReadFile(string path)

        {

            Dictionary<int, string> dicFileData = new Dictionary<int, string>();

            FileStream fs = new FileStream(path, FileMode.Open);

            BinaryReader binReader = new BinaryReader(fs);

            int bagindex = 1;

            int dataindex = 0;

            byte[] bBuffer = new byte[fs.Length];

            int bagsize = 235;// 一包数据大小

            byte[] temp = new byte[bagsize];

            binReader.Read(bBuffer, 0, (int)fs.Length);

 

            for (int i = 0; i < bBuffer.Length; i++)

            {

                if ((bagsize - 1) == dataindex || (bBuffer.Length - 1) == i)

                {

                    if ((bBuffer.Length - 1) == i) temp[dataindex++] = bBuffer[i];

                    string data = CCommonFunc.ToHexString(temp, dataindex, false);

                    dicFileData.Add(bagindex, data);

                    bagindex++;

                    dataindex = 0;

                    temp = new byte[bagsize];

                }

                temp[dataindex++] = bBuffer[i];

            }

            binReader.Close();

            fs.Close();

 

            return dicFileData;

        }

测试:

 

 

posted @ 2019-07-24 16:09  CHHC  阅读(1271)  评论(0编辑  收藏  举报