为了传送文件,用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;
}
}

}

}
本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2005/04/30/148284.html,如需转载请自行联系原作者

用Remoting 实现一个文件传输组件相关推荐

  1. 写了一个文件传输软件

    大家好,我是涛哥. 最近遇到这样一件事情,有一个大文件,用微信从电脑A传输到电脑B,结果微信提示文件过大.这点小事情,略微有点郁闷. 解决办法肯定是有的,比如下载一个文件传输软件,然后传送大文件.不过 ...

  2. 基于小程序+云开发制作一个文件传输助手小程序

    微信文件传输助手是真人?基于云开发制作一个文件传输助手小程序,你发给ta的小秘密,只有你自己知道. 开发步骤 一.创建小程序 二.云开发配置 环境配置 绑定云环境 三.页面设计 首页 详情页 底部弹窗 ...

  3. Android 一直往文件写数据_对标苹果 AirDrop,Google 为安卓开发了一个文件传输利器...

    在今年 5 月的 Google I/O 大会上,Google 透露了更多 Android Q 搭载的令人期待的新功能:同时, 关于 Android Beam 在 Android Q 中的缺席, 官方并 ...

  4. python或c++编写一个文件传输工具

    C++实现 python实现 方法一:搭建web server 1. 用python -m http.server搭一个简易的本地局域网 已经验证有效,good!!! 2. 参考文章<用Pyth ...

  5. 一步一步做一个linux文件传输软件(一)

    曾经在linux上实现过一个文件传输软件,客户端可以向服务器一次传输多个文件或者图片,并且在客户端可以看到文件传输的进度. 功能非常简单,但是涉及到的知识挺多的:GDB的调试:socket编程:多线程 ...

  6. 写了一个没啥用的文件传输工具

    这是在干嘛? 起因 之前看到有开发者使用二维码+tun/tap, 实现了使用二维码扫码作为物理传输的IP网络.大家感兴趣的话传送门在这里 https://hackaday.com/2016/11/22 ...

  7. ecs云服务器上传文件,ECS文件传输

    ECS文件传输 内容精选 换一换 通过Web浏览器登录主机,提供协同分享.文件传输.文件管理和预置命令等功能.用户在主机上执行的所有操作,被云堡垒机记录并生成审计数据.协同分享指会话创建者将当前会话链 ...

  8. linux 传输大文件大小,Linux大文件传输(转)

    我们经常需要在机器之间传输文件.比如备份,复制数据等等.这个是很常见,也是很简单的.用scp或者rsync就能很好的完成任务.但是如果文件很大,需要占用一些传输时间的时候,怎样又快又好地完成任务就很重 ...

  9. linux ssh 推送文件_WinSCP软件双系统(Win-Linux)文件传输教程

    WinSCP软件是windows下的一款使用ssh协议的开源图形化SFTP客户端,也就是一个文件传输的软件,它有什么优点吗,咱们嵌入式开发中经常会将windows中的文件复制到linux系统当中,比较 ...

最新文章

  1. 使用livereload实现自动刷新
  2. NHIBERNATE
  3. 分布式缓存使用介绍MemCache
  4. 为Jersey 2.19创建共享库以与Weblogic 12.1.3一起使用
  5. 获取js里添加的css文件,用JS添加一个css文件
  6. C/C++网络编程工作笔记0004---socket()函数详解
  7. 通过JDBC和Hibernate对Clob和Blob的操作
  8. 带SN切换流程_贴片电阻生产工艺流程简介
  9. 上位机plc编程入门_零基础自学plc编程怎么入门?
  10. MATLAB里根号打印,里根号
  11. MIPI-DSI学习笔记(一)
  12. Vue项目设置浏览器小图标
  13. python面向对象学习
  14. uni-app 基础之常用组件(2)基础内容
  15. odoo13-14电商插件
  16. linux ps查看完整时间,Linux ps 命令查看进程启动及运行时间
  17. CGdiObject::DeleteObject的说法
  18. CSS实现超级炫酷的流光按钮效果
  19. 免费分享一个SpringBoot鲜花商城管理系统,很漂亮的
  20. Oracle EBS Concurrent Request:Gather Schema Statistics

热门文章

  1. SpringMVC上传文件解析request请求为空获取不到数据问题
  2. Python基础编程——字典的创建
  3. 允许使用抽象类类型 isearchboxinfo 的对象_Java新手必学:面向对象的特性都有哪些?...
  4. 介绍一下Linux 误删文件恢复命令及方法
  5. 微软宣布 Power Fx 开源
  6. MySQL 中的共享表空间与独立表空间如何选择
  7. sqlalchemy中的first_or_404()和get_or_404()使用(前端页面可视化操作——查询和添加)
  8. react 让滚动条一直在下面_Ink 2.0 发布:命令行应用程序的 React
  9. 电脑c盘满了怎么办_电脑用久了C盘空间不够用怎么办?教你如何无损扩展C盘空间大小...
  10. android路由器 设备数,手机查看wifi连接人数_手机查看wifi连接设备数量-192路由网...