高性能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;

        }

測試:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM