您的位置:首页 > 其它

用Remoting 实现一个文件传输组件

2008-10-08 10:38 399 查看
为了传送文件,用remoting 实现很简单容易,有工程源码和演示程序下载,是从我写的一个网络库的一个子模块;有注解,不加以文字说明了。




/**//*


作者:S.F.


blog:www.cnblogs.com/chinasf


*/


using System;


using System.ComponentModel;


using System.Runtime.Remoting;


using System.Runtime.Remoting.Channels;


using System.Runtime.Remoting.Channels.Tcp;


using System.IO;


using System.Text;


using System.Net;




namespace SocketLibrary






{




/**//// <summary>


/// NetFileTransfer 的摘要说明。


/// 用Remoting 实现文件传输管理


/// </summary>


public class NetFileTransfer: MarshalByRefObject






{


public NetFileTransfer() : base()






{


}






/**//// <summary>


/// 获取文件的数组


/// </summary>


/// <param name="filePath">文件路径</param>


/// <returns>数组(默认null)</returns>




public byte[] GetFileBytes(string filePath)

{




if(File.Exists(filePath))

{




try

{


FileStream fs =new FileStream(filePath,FileMode.Open,FileAccess.Read,FileShare.Read);


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


fs.Read(buffer,0,buffer.Length);


fs.Close();


return buffer;




}catch

{


return null;


}




}else

{


return null;


}


}






/**//// <summary>


/// 发送数据


/// </summary>


/// <param name="savePath">保存路径</param>


/// <returns>状态</returns>




public bool SendFileBytes(byte[] fileBytes,string savePath)

{




if(fileBytes==null)return false;






try

{


FileStream fs = new FileStream(savePath,FileMode.OpenOrCreate,FileAccess.Write,FileShare.Write);


fs.Write(fileBytes,0,fileBytes.Length);


fs.Close();


return true;




}catch

{


return false;


}




}


}






public class NetFileTransferServer : Component

{




private TcpChannel chan =null;


private int _Port = 8085;


private string _RegisterMethod = "FileService";


private bool _Active =false;






/**//// <summary>


/// 构造


/// </summary>




public NetFileTransferServer() : base()

{




}






/**//// <summary>


/// 绑定端口


/// </summary>




public int Port

{




get

{


return this._Port;


}




set

{


this._Port = value;


}


}






/**//// <summary>


/// 绑定注册方法名


/// </summary>




public string RegisterMethod

{




get

{


return this._RegisterMethod;


}




set

{


this._RegisterMethod = value;


}


}






/**//// <summary>


/// 获取激活状态


/// </summary>




public bool Active

{




get

{


return this._Active;


}


}






/**//// <summary>


/// 启动服务


/// </summary>




public void Start()

{




try

{


chan = new TcpChannel(this._Port);


ChannelServices.RegisterChannel(chan);


RemotingConfiguration.RegisterWellKnownServiceType(typeof(NetFileTransfer),this._RegisterMethod,WellKnownObjectMode.SingleCall);


this._Active = true;




}catch(Exception e)

{


Console.WriteLine(e.Message);


this._Active = false;


}


}






/**//// <summary>


/// 停止服务


/// </summary>




public void Stop()

{


this._Active = false;




if(chan!=null)

{


ChannelServices.UnregisterChannel(chan);


}


}






/**//// <summary>


/// 获取注册的方法协议全称


/// </summary>


/// <param name="IpAddressIndex">IP地址索引号(支持多IP)</param>


/// <returns>协议全称</returns>




public string NoticeRegisterMethodName(int IpAddressIndex)

{


//tcp://localhost:8085/FileService




try

{


IPHostEntry heserver = Dns.Resolve("localhost");


IPAddress currAddr = heserver.AddressList[IpAddressIndex];


return String.Format("tcp://{0}:{1}/{2}",currAddr.ToString(),this._Port.ToString(),this._RegisterMethod);




}catch(Exception e)

{


return e.Message;


}


}


}






public class NetFileTransferClient : Component

{




private NetFileTransfer nft = null;


private bool _Active =false;


private string _NoticeRegisterMethodName = "tcp://localhost:8085/FileService";


private int _Port = 8085;






public NetFileTransferClient() : base()

{


}






/**//// <summary>


/// 绑定端口


/// </summary>




public int Port

{




get

{


return this._Port;


}




set

{


this._Port = value;


}


}






/**//// <summary>


/// 绑定注册方法名


/// </summary>




public string NoticeRegisterMethodName

{




get

{


return this._NoticeRegisterMethodName;


}




set

{


this._NoticeRegisterMethodName = value;


}


}






/**//// <summary>


/// 获取激活状态


/// </summary>




public bool Active

{




get

{


return this._Active;


}


}






/**//// <summary>


/// 连接器


/// </summary>




public NetFileTransfer NetFileTransferObject

{




get

{


return this.nft;


}


}






/**//// <summary>


/// 连接到服务器


/// </summary>


/// <returns>状态</returns>




public bool Connect()

{




try

{


nft = (NetFileTransfer)Activator.GetObject(typeof(SocketLibrary.NetFileTransfer),_NoticeRegisterMethodName);




if(nft!=null && nft.ToString().Length>1)

{


this._Active = true;


return true;




}else

{


this._Active =false;


return false;


}




}catch

{


this._Active =false;


return false;


}


}






/**//// <summary>


/// 停止连接


/// </summary>




public void Disconnection()

{


nft = null;


_Active =false;


}






/**//// <summary>


/// 获取文件


/// </summary>


/// <param name="RemoteFilePath">文件路径</param>


/// <returns>文件数组</returns>




public byte[] GetFileBytes(string RemoteFilePath)

{


if(!_Active)return null;




try

{


return nft.GetFileBytes(RemoteFilePath);




}catch

{


return null;


}


}






/**//// <summary>


/// 获取文件,并保存


/// </summary>


/// <param name="RemoteFilePath">远程文件路径</param>


/// <param name="LocalSavePath">本地保存路径</param>


/// <returns>状态</returns>




public bool GetFile(string RemoteFilePath,string LocalSavePath)

{


if(!_Active)return true;




try

{


byte[] filebytes = nft.GetFileBytes(RemoteFilePath);




if(filebytes!=null)

{


FileStream fs = new FileStream(LocalSavePath,FileMode.CreateNew,FileAccess.Write,FileShare.Write);


fs.Write(filebytes,0,filebytes.Length);


fs.Close();


return true;




}else

{


return false;


}




}catch

{


return false;


}


}






/**//// <summary>


/// 发送文件


/// </summary>


/// <param name="fileBytes">文件数组</param>


/// <param name="RemoteSavePath">保存路径</param>


/// <returns>保存状态</returns>




public bool SendFileBytes(byte[] fileBytes,string RemoteSavePath)

{


if(!_Active)return false;




try

{


return nft.SendFileBytes(fileBytes,RemoteSavePath);




}catch

{


return false;


}


}






/**//// <summary>


/// 发送文件,并保存到主机


/// </summary>


/// <param name="LocalFilePath">本地文件</param>


/// <param name="RemoteSavePath">远端保存路径</param>


/// <returns>是否成功</returns>




public bool SendFile(string LocalFilePath,string RemoteSavePath)

{


if(!_Active)return false;




try

{


if(!File.Exists(LocalFilePath))return false;




FileStream fs = new FileStream(LocalFilePath,FileMode.Open,FileAccess.Read,FileShare.Read);


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


fs.Read(buffer,0,buffer.Length);


fs.Close();


return nft.SendFileBytes(buffer,RemoteSavePath);




}catch

{


return false;


}


}




}




}



点击这里下载工程代码

来自

http://www.cnblogs.com/Chinasf/archive/2008/08/23/148284.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: