CTCP发送消息和传输文件

上传人:re****.1 文档编号:501250668 上传时间:2023-07-01 格式:DOC 页数:17 大小:190KB
返回 下载 相关 举报
CTCP发送消息和传输文件_第1页
第1页 / 共17页
CTCP发送消息和传输文件_第2页
第2页 / 共17页
CTCP发送消息和传输文件_第3页
第3页 / 共17页
CTCP发送消息和传输文件_第4页
第4页 / 共17页
CTCP发送消息和传输文件_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《CTCP发送消息和传输文件》由会员分享,可在线阅读,更多相关《CTCP发送消息和传输文件(17页珍藏版)》请在金锄头文库上搜索。

1、【背景】最近做了一个双机备份,就是服务器上有个文件夹,会接收客户端传来的文件,而我们要 做的就是同步这台服务器和另一台备用服务器上的文件为了实现这个功能我们使用的tcp点对点传输【开发环境】VS2005【实现原理】要实现同步要解决两个问题,一个是获取本地服务器上上传上来的文件,二是实现两台机器间的文件传输第一个问题我们用的 FileSystemWatcher这个可以监视指定文件夹下的文件变动,然后我们把变动的文件信息记录到数据库,在指定的时间间隔后同步两台机器的文件第二个问题我们用的tcp文件传输,我们按照一定的原则通过传输消息来告知备份服务 器的要传输的文件名称和大小,然后传输文件【代码】1

2、:FileSystemWatcher监视文件变动的就不介绍了,很简单的winform 控件应用.2:为了完成文件传输,我做了一个TcpHelper类库,其中包括TcpCommon,TcpClientHelper,TcpListenerHelper三个类,TcpCommon主要实现了文件传输时用的一些公共的方法比如发送接收文件,发送接收消息,和文件hash的计算TcpCom monusing System;usingSystem.Collecti on s.Ge neric;using System.Text;using System.Security.Cryptography;usingSys

3、te m.IO;using System.Net.Sockets;n amespace Xpwy.Backup.PTcpHelperinternal class TcpCom monprivate static readonly int _blockLength =500 *1024 ;/ /计算文件的hash值/ in ternal stri ng CalcFileHash( string FilePath)MD5CryptoServiceProvider md5 =new MD5CryptoServiceProvider();byte hash;using (FileStream fs =

4、new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read,4096 )hash = md5.ComputeHash(fs);returnBitC on verter.ToStri ng(hash);/ /发送文件/ /in ternalbool Sen dFile(str ingfilePath, NetworkStream stream)FileStream fs = File.Open(filePath, FileMode.Open);int readLe ngth =0;byte data = new

5、byte _blockLength;new byte 8;new FileI nfo(filePath).Le ngth).Copy0, 8);/发送大小 byte length =BitCo nverter.GetBytes(To(le ngth, 0);stream.Write(le ngth,/发送文件0)while (readLe ngth = fs.Read(data,0, _blockLe ngth) stream.Write(data,0, readLe ngth);fs.Close();return true ;/ /接收文件/ /in ternalbool ReceiveFi

6、le(stri ngfilePath, NetworkStream stream)trylong cou nt = GetSize(stream);if (cou nt =0)return false;long in dex =0;byte clientData =new byte _blockLength;if (File.Exists(filePath)File.Delete(filePath);str ingpath= new FileI nfo(filePath).Directory.FullName;if (!Directory.Exists(path)Directory.Creat

7、eDirectory(path);FileStream fs = File.Open(filePath, FileMode.OpenOrCreate);try/计算当前要读取的块的大小int curre ntBlockLe ngth =0;if (_blockLe ngth 0 & in dex cou nt)clientData =new byte _blockLength;0;receivedBytesLe n =if (_blockLe ngth cou nt - in dex)curre ntBlockLe ngth = _blockLe ngth;elsecurre ntBlockL

8、e ngth = (int )(cou nt -in dex);receivedBytesLe n = stream.Read(clie ntData, 0, curre ntBlockLe ngth);in dex += receivedBytesLe n;fs.Write(clie ntData,0, receivedBytesLe n);catch(Excepti on ex)return false ;fin allyfs.Close();catch(Excepti on ex)return false ;return true ;/ /发送消息/ /vparam n ame=mess

9、age/vparam n ame=stream/vretur nsin ternal bool Sen dMessage(stri ng message, Networkstream stream)byte data = Encoding.UTF8.GetBytes(message);byte resultData =new byte 8 + data.Length;0);BitC on verter.GetBytes(data.Le ngth).CopyTo(resultData,data.CopyTo(resultData,8);data.CopyTo(resultData,8);0, r

10、esultData.Le ngth);returntruestream.Write(resultData,data.CopyTo(resultData,8);data.CopyTo(resultData,8);/ /读取消息/ / /in ternal stri ng ReadMessage(NetworkStream stream)stringresult =;int messageLe ngth =0;byte resultbyte =/读取数据大小intin dex =0;new byte 500 *1024 ;int cou nt = GetSize(stream);byte data

11、 = new byte count;while (in dex cou nt & (messageLe ngth = stream.Read(data, 0, cou nt - in dex) !=0)data.CopyTo(resultbyte, in dex);in dex += messageLe ngth;0, in dex);result = En cod in g.UTF8.GetStri ng(resultbyte,return result;/ /获取要读取的数据的大小/ /private int GetSize(NetworkStream stream)int cou nt =0;byte countBytes =new byte 8;tryif (stream.Read(cou ntBytes,0, 8) =8)0);cou nt = BitC on verter.To In t32(cou ntBytes,elsereturn 0;catch (Excepti on ex)retur n cou nt;TcpClie ntHelper using System;usingSystem.Collecti on s.Ge neric;usin

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号