服務器與客戶端之間的遠程圖片傳輸

一、共享類

 

定義一個遠程對象類的定義:

接口:   

 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);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章