高性能TcpServer(C#) - 4.文件通道(處理:文件分包,支持斷點續傳)

高性能TcpServer(C#) - 1.網絡通訊協議html

高性能TcpServer(C#) - 2.建立高性能Socket服務器SocketAsyncEventArgs的實現(IOCP)服務器

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

高性能TcpServer(C#) - 4.文件通道(處理:文件分包,支持斷點續傳)性能

高性能TcpServer(C#) - 5.客戶端管理測試

高性能TcpServer(C#) - 6.代碼下載spa

 

應用場景
htm

 

升級程序blog

流程:終端->查詢服務器版本比較->升級程序(獲取包數,獲取各包數據)->數據拼裝生成文件->最後更新服務器上該設備的版本信息get

分包代碼段:string

        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;

        }

測試:

 

相關文章
相關標籤/搜索