Remote Copy

公司里挪VDI,两台主机之间无法复制粘贴共享数据,特写此程序用来实现在两机之间实现拷贝

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace RemoteCopy
{
    abstract class SafeThread
    {
        bool _running = false;
        ManualResetEvent _exitEvent = null;
        long _exitFlag = 0;

        public enum ExitFlag
        {
            Exit,
            Continue
        }

        public abstract ExitFlag ThreadTick();

        void ThreadProc()
        {
            while (true)
            {
                if (Interlocked.Read(ref _exitFlag) == 1) break;
                if (ThreadTick() == ExitFlag.Exit)
                    break;
            }
            _exitEvent.Set();
        }

        public void Run()
        {
            if (_running) return;
            _exitEvent = new ManualResetEvent(false);
            _exitFlag = 0;
            new Thread(ThreadProc).Start();
            _running = true;
        }

        public void Stop()
        {
            if (!_running) return;
            Interlocked.Increment(ref _exitFlag);
            _exitEvent.WaitOne();
            _running = false;
        }
    }
}

SafeThread.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using RemoteCopy.Commands;

namespace RemoteCopy
{
    static class Program
    {
        static object _clipboardLock = new object();
        static IClipboard _clipboard = null;

        public static void Copy(IClipboard clipboard)
        {
            lock (_clipboardLock)
            {
                _clipboard = clipboard;
            }
        }

        static void SendDataProc(object text)
        {
            CommandHelper.SendText("127.0.0.1", 8088, text.ToString());
        }

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            WebServer server = new WebServer(8088);
            server.Run();

            while (true)
            {
                string text = ClipboardChecker.GetInstance().CheckText();
                if (text != null)
                {// 发送
                    new Thread(new ParameterizedThreadStart(SendDataProc)).Start(text);
                }
                // 复制
                lock (_clipboardLock)
                {
                    if (_clipboard != null)
                    {
                        _clipboard.Copy();
                        _clipboard = null;
                    }
                }
                Thread.Sleep(1000);
            }
        }
    }
}
WebServer.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace RemoteCopy
{
    class WebServer : SafeThread
    {
        TcpListener _listener = null;

        public WebServer()
        {
            _listener = new TcpListener(IPAddress.Any, 80);
            _listener.Start();
        }

        public WebServer(int port)
        {
            _listener = new TcpListener(IPAddress.Any, port);
            _listener.Start();
        }

        public override SafeThread.ExitFlag ThreadTick()
        {
            if (_listener.Pending())
            {
                TcpClient client = _listener.AcceptTcpClient();
                if (client != null)
                    new ClientConnection(client);
            }
            Thread.Sleep(10);
            return ExitFlag.Continue;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace RemoteCopy
{
    class ClipboardChecker
    {
        static ClipboardChecker _instance = new ClipboardChecker();
        string _lastValue = null;

        private ClipboardChecker()
        {
        }

        public static ClipboardChecker GetInstance()
        {
            return _instance;
        }

        public string CheckText()
        {
            try
            {
                IDataObject iData = Clipboard.GetDataObject();
                // 将数据与指定的格式进行匹配,返回bool
                if (iData != null && iData.GetDataPresent(DataFormats.Text))
                {
                    string text = (string)iData.GetData(DataFormats.Text);
                    if (text != null && text != _lastValue)
                    {
                        _lastValue = text;
                        return _lastValue;
                    }
                }
            }
            catch
            {
            }
            return null;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using RemoteCopy.Commands;

namespace RemoteCopy
{
    static class CommandHelper
    {
        static void WriteLength(NetworkStream ns, int length)
        {
            ns.WriteByte((byte)((length & 0xff000000) >> 24));
            ns.WriteByte((byte)((length & 0x00ff0000) >> 16));
            ns.WriteByte((byte)((length & 0x0000ff00) >> 8));
            ns.WriteByte((byte)((length & 0x000000ff) >> 0));
        }

        public static void SendText(string ip, int port, string text)
        {
            if (string.IsNullOrEmpty(text)) return;
            TcpClient client = null;
            try
            {
                client = new TcpClient(ip, port);
                using (NetworkStream ns = client.GetStream())
                {
                    byte[] textBuf = Encoding.UTF8.GetBytes(text);
                    ns.WriteByte(TextCommand.ID);
                    WriteLength(ns, textBuf.Length);
                    ns.Write(textBuf, 0, textBuf.Length);
                    ns.Flush();
                }
            }
            catch
            {
            }
            finally
            {
                if (client != null)
                    client.Close();
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Web;
using RemoteCopy.Commands;

namespace RemoteCopy
{
    class ClientConnection
    {
        TcpClient _client = null;
        static Dictionary<int, ICommand> _commands = new Dictionary<int, ICommand>();

        static ClientConnection()
        {
            _commands.Add(TextCommand.ID, new TextCommand());
        }

        public ClientConnection(TcpClient client)
        {
            _client = client;
            new Thread(HandleProc).Start();
        }

        int ReadLength(NetworkStream ns)
        {
            int len1 = ns.ReadByte();
            int len2 = ns.ReadByte();
            int len3 = ns.ReadByte();
            int len4 = ns.ReadByte();
            return len1 << 24 | len2 << 16 | len3 << 8 | len4;
        }

        void HandleProc()
        {
            try
            {
                using (NetworkStream ns = _client.GetStream())
                {
                    int type = ns.ReadByte();
                    if (!_commands.ContainsKey(type)) return;
                    int len = ReadLength(ns);
                    if (len == 0) return;
                    byte[] bs = new byte[len];
                    int size = ns.Read(bs, 0, bs.Length);
                    if (size == 0) return;
                    ICommand cmd = _commands[type];
                    if (cmd == null) return;
                    ICommand clone = cmd.Clone();
                    clone.Handle(bs, size);
                    Program.Copy((IClipboard)clone);
                }
            }
            finally
            {
                _client.Close();
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace RemoteCopy.Commands
{
    interface ICommand
    {
        ICommand Clone();
        void Handle(byte[] data, int length);
    }
}

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace RemoteCopy.Commands
{
    interface IClipboard
    {
        void Copy();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RemoteCopy.Commands
{
    class TextCommand : ICommand, IClipboard
    {
        public const int ID = 0x01;

        string _text = "";

        public ICommand Clone()
        {
            return new TextCommand();
        }

        public void Handle(byte[] data, int length)
        {
            _text = Encoding.UTF8.GetString(data);
        }

        public void Copy()
        {
            try
            {
                Clipboard.Clear();
                Clipboard.SetText(_text);
            }
            catch
            {
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值