![]()

作者: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
{
![]()
/// NetFileTransfer 的摘要说明。
/// 用Remoting 实现文件传输管理
/// </summary>
public class NetFileTransfer: MarshalByRefObject
{
public NetFileTransfer() : base()
{
}

![]()
/// 获取文件的数组
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns>数组(默认null)</returns>
![]()
{
![]()
{
![]()
{

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;
{
return null;
}
{
return null;
}
}

![]()
/// 发送数据
/// </summary>
/// <param name="savePath">保存路径</param>
/// <returns>状态</returns>
![]()
{

if(fileBytes==null)return false;

{
FileStream fs = new FileStream(savePath,FileMode.OpenOrCreate,FileAccess.Write,FileShare.Write);
fs.Write(fileBytes,0,fileBytes.Length);
fs.Close();
return true;
{
return false;
}

}
}

{

private TcpChannel chan =null;
private int _Port = 8085;
private string _RegisterMethod = "FileService";
private bool _Active =false;

![]()
/// 构造
/// </summary>
![]()
{


}

![]()
/// 绑定端口
/// </summary>
![]()
{
![]()
{
return this._Port;
}
{
this._Port = value;
}
}

![]()
/// 绑定注册方法名
/// </summary>
![]()
{
![]()
{
return this._RegisterMethod;
}
{
this._RegisterMethod = value;
}
}

![]()
/// 获取激活状态
/// </summary>
![]()
{
![]()
{
return this._Active;
}
}

![]()
/// 启动服务
/// </summary>
![]()
{
![]()
{

chan
= new TcpChannel(this._Port);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(NetFileTransfer),this._RegisterMethod,WellKnownObjectMode.SingleCall);
this._Active = true;
{
Console.WriteLine(e.Message);
this._Active = false;
}
}

![]()
/// 停止服务
/// </summary>
![]()
{
this._Active = false;
{
ChannelServices.UnregisterChannel(chan);
}
}

![]()
/// 获取注册的方法协议全称
/// </summary>
/// <param name="IpAddressIndex">IP地址索引号(支持多IP)</param>
/// <returns>协议全称</returns>
![]()
{
//tcp://localhost:8085/FileService
{
IPHostEntry heserver = Dns.Resolve("localhost");
IPAddress currAddr = heserver.AddressList[IpAddressIndex];
return String.Format("tcp://{0}:{1}/{2}",currAddr.ToString(),this._Port.ToString(),this._RegisterMethod);
{
return e.Message;
}
}
}

{

private NetFileTransfer nft = null;
private bool _Active =false;
private string _NoticeRegisterMethodName = "tcp://localhost:8085/FileService";
private int _Port = 8085;

{
}

![]()
/// 绑定端口
/// </summary>
![]()
{
![]()
{
return this._Port;
}
{
this._Port = value;
}
}

![]()
/// 绑定注册方法名
/// </summary>
![]()
{
![]()
{
return this._NoticeRegisterMethodName;
}
{
this._NoticeRegisterMethodName = value;
}
}

![]()
/// 获取激活状态
/// </summary>
![]()
{
![]()
{
return this._Active;
}
}

![]()
/// 连接器
/// </summary>
![]()
{
![]()
{
return this.nft;
}
}

![]()
/// 连接到服务器
/// </summary>
/// <returns>状态</returns>
![]()
{
![]()
{

nft
= (NetFileTransfer)Activator.GetObject(typeof(SocketLibrary.NetFileTransfer),_NoticeRegisterMethodName);
{
this._Active = true;
return true;
{
this._Active =false;
return false;
}
{
this._Active =false;
return false;
}
}

![]()
/// 停止连接
/// </summary>
![]()
{

nft
= null;
_Active =false;
}

![]()
/// 获取文件
/// </summary>
/// <param name="RemoteFilePath">文件路径</param>
/// <returns>文件数组</returns>
![]()
{
if(!_Active)return null;
{
return nft.GetFileBytes(RemoteFilePath);
{
return null;
}
}

![]()
/// 获取文件,并保存
/// </summary>
/// <param name="RemoteFilePath">远程文件路径</param>
/// <param name="LocalSavePath">本地保存路径</param>
/// <returns>状态</returns>
![]()
{
if(!_Active)return true;
{
byte[] filebytes = nft.GetFileBytes(RemoteFilePath);
{
FileStream fs = new FileStream(LocalSavePath,FileMode.CreateNew,FileAccess.Write,FileShare.Write);
fs.Write(filebytes,0,filebytes.Length);
fs.Close();
return true;
{
return false;
}
{
return false;
}
}
![]()
/// 发送文件
/// </summary>
/// <param name="fileBytes">文件数组</param>
/// <param name="RemoteSavePath">保存路径</param>
/// <returns>保存状态</returns>
![]()
{
if(!_Active)return false;
{
return nft.SendFileBytes(fileBytes,RemoteSavePath);
{
return false;
}
}

![]()
/// 发送文件,并保存到主机
/// </summary>
/// <param name="LocalFilePath">本地文件</param>
/// <param name="RemoteSavePath">远端保存路径</param>
/// <returns>是否成功</returns>
![]()
{
if(!_Active)return false;
{
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);
{
return false;
}
}

}

}