“文件同步精灵”的介绍请查看《 文件同步精灵(初版) 》
概念解析:
* 协议包:客户端与服务器的进行功能交互时传递的数据包。
* 协议头:协议包的前4个字节则称为“协议头”。
* 协议码:用数值代表的操作码,主要是用于区分不同的操作,比如上传文件是601,而下载文件则是602等。
一、协议包格式:
协议包是由“协议头”与“数据块”组成,其中在一个协议包中“协议头”是必须存在的!
黄色区域中的数据块的大小N将由浅绿色区域的值决定,也就是N的值范围是0...65535,比如浅绿色区域的值是0x64时,则黄色区域的数据是100Byte,而如果值为0则黄色区域的数据块为空!
二、协议码:
协议码是用于区分每种功能操作,目前共有以下几种定义:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
三、密码验证:
客户端与服务端在交互时,必须先通过密码验证才能进行其它操作(比如上传文件)。而为了确保密码在传输过程中不被窃取所以必须先对密码进行加密再传输。为方便客户端与服务端的加解密,采用的是类似于“证书”方式,也即客户端需要加密密码时,必须先向服务端请求加密密匙!
大概的流程如下:
也取是客户端首先发送“协议码”为501的协议包到服务端,服务端收到后将返回一个“协议码”为505的协议包给客户端,客户端再从协议包中取得加密密匙,并使用加密密匙对验证密码进行加密并发送“协议码”为502的协议包,服务端接收到协议包后对其中的加密数据进行解密并判断验证密码的正确性,并将结果(协议包)返回给客户端,从而完成密码验证过程。
四、文件操作:
为方便说明,对目录或文件的操作都统称为“文件操作”。
1)文件消息包:由文件消息包头和文件数据组成。
主要是用于存放要操作的文件信息,格式定义如下:
注:虚线框内的则是“文件消息包头”,每个文件消息包中必须存在文件消息包头!
文件类型是采用以下的定义:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
#region 文件类型
/// <summary>
/// 文件类型
/// </summary>
[Flags]
private enum FileMessageType
: byte
{
/// <summary>
/// 未知
/// </summary>
Unknown = 0,
/// <summary>
/// 文件
/// </summary>
File = 0x1,
/// <summary>
/// 目录
/// </summary>
Directory = 0x2,
/// <summary>
/// 多分发的
/// </summary>
IsMulriple = 0x4
}
#endregion
其中IsMulriple是服务器传送多个文件数据到客户端时使用的标记,用于区分当前文件消息包后是否还有下一个文件消息包。黄色区域的文件数据多少则由灰色块的文件大小决定,而如果文件消息包是针对目录,则文件大小的值为0且文件数据块为空!
2) 文件协议包:
将文件消息包数据放入协议包中的黄色区域块,协议包的数据大小值为文件消息包头的大小,则构成文件协议包。如下图:
3) 上传文件流程:
客户端发送一个“协议码”为601的文件协议包,服务器收到协议包后进行处理并返回结果。
4)下载文件流程:
客户端发送一个“协议码”为602的文件协议包(空文件数据),服务器收到协议包根据情况进行处理,并返回一个或多个“协议码”为605的文件协议包给客户端。
5)删除文件流程:
客户端发送一个“协议码”为603的文件协议包,服务器收到协议包后进行处理并返回结果。
五、断开会话:
当双方还处于连接状态并且某一方需要断开会话时则发送“协议码”为886的协议包给对方。
源码下载:http://files.cnblogs.com/kingthy/FileSynchronizer_src.rar
注:以上的协议定义并不是稳健的,如果哪位有需要的可以直接使用,但出现问题本人概不负责!如果你对此协议进行了扩展修改并且方便的望能发一份源码给我。谢谢!!
![1298240.html?type=1](https://i-blog.csdnimg.cn/blog_migrate/86733371c9e2e10ada96b6e57a602bbd.jpeg)
[新闻]Google美国9月搜索份额升至71% 同比增长7.6%
博客园首页 社区 新闻频道 小组 博问 网摘 闪存
文章来源: http://www.cnblogs.com/kingthy/archive/2008/09/24/1298240.html