服务器与客户端之间的远程图片传输

一、共享类

 

定义一个远程对象类的定义:

接口:   

 public interface IFileServer
    {
        byte[] DownLoad(string path, string user, bool rplace);//实现从服务器上下载图片的方法
        void UpLoad(string path, string user, bool rplace, byte[] bytes););//实现从客户端上传图片的方法
    }

实现的方法:

public class FileClass : MarshalByRefObject, IFileServer
    {
        #region IFileServer 成员

        public byte[] DownLoad(string path, string user, bool rplace)
        {
            string filepath = path+user+".jpg";
            using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
            {
                byte[] bt = new byte[fs.Length];  
                fs.Read(bt, 0,(int) fs.Length);//将文件写为字节数组
                fs.Close();
                return bt;
            }
        }

        public void UpLoad(string path, string user, bool rplace, byte[] bytes)
        {
            string filepath = path + user + ".jpg";
            using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate,FileAccess.Write))
            {
                fs.Write(bytes , 0, (int)bytes.Length);
                fs.Close();
            }
        }

        #endregion
    }

二、服务器端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp ;
using File;
using System.Security.Permissions;
using System.Runtime.Remoting;
namespace FileServer
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TcpServerChannel HPChannel = new TcpServerChannel(9002);
            ChannelServices.RegisterChannel(HPChannel);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(FileClass), "fileserver",
                WellKnownObjectMode.Singleton);
            MessageBox.Show("服务已经启动!");
        }
    }
}

三、客户端程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using File;
using System.IO;

namespace FileClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TcpClientChannel hc = new TcpClientChannel();
            ChannelServices.RegisterChannel(hc);
            IFileServer IFileS = (IFileServer)Activator.GetObject(typeof(IFileServer), "tcp://" + textBox2.Text + ":9002/fileserver");
            string user = textBox1.Text;
            byte[] bytes;

            bytes = IFileS.DownLoad(@"D:/", user, false);

            using (MemoryStream ms = new MemoryStream(bytes))
            {
                Image bp1 = Image.FromStream(ms);
                pictureBox1.Image = bp1;
                ms.Close();
            }
        }

         private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Title = "打开图像文件";
            fd.Filter = "图像文件(*.jpg)|*.JPG|位图(*.bmp)|*.bmp|所有文件(*.*)|*.*";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(fd.FileName);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            IFileServer IFileS = (IFileServer)Activator.GetObject(typeof(IFileServer), "tcp://" + textBox2.Text + ":9002/fileserver");

            string user = textBox1.Text;
            byte[] bytes;
            using (MemoryStream stream = new MemoryStream())
            {
                pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);
                int length = (int)stream.Length;
                bytes = stream.ToArray();
                stream.Close();
            }
            IFileS.UpLoad(@"D:/", user, false, bytes);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值