我们来自五湖四海,不为别的,只因有共同的爱好,为中国互联网发展出一分力!

udp实现两台电脑桌面同步的方法

2012年02月13日11:57 阅读: 22780 次

标签: C#, udp, 编程技巧, .net, 编程实例, 电脑桌面同步

dp实现两台电脑桌面同步的方法 用socket 实现的   上图和代码吧

简单分端:

1.组播端:

 

 

2.听众端:

 

 

3.同步窗口:

 

 

核心代码:
// 1. 组播端
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;

namespace WindForms
{
    public partial class Form1 : Form
    {
        string path = Application.StartupPath
            //+"/2011.bmp";
            + "/boy.jpg";    

        private ScreenCapture screen = new ScreenCapture();

        public Form1()
        {
            InitializeComponent();

            pictureBox1.Image = Image.FromFile(path);

            //Init();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ToggleButton();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            sendScreen();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ToggleButton();
        }

      

        #region  Test Two 方案二

        public void sendScreen()
        {
            byte[] data = screenData();

            IPEndPoint iep1 = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);

            //IPEndPoint iep1 = new IPEndPoint(IPAddress.Parse("192.168.1.109"), 2000);
            //IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("192.168.1.107"), 2000);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

            socket.SendTo(data, iep1);
            //socket.SendTo(data, iep2);

            socket.Close();
            textBox1.Text += "发送完毕!!\r\n";
        }

        private byte[] screenData()
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                byte[] by;
                Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                Graphics gp = Graphics.FromImage(bmp);

                gp.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));

                Image im = bmp.GetThumbnailImage(320, 256, null, IntPtr.Zero);
                bmp = new Bitmap(im);
                bmp.Save(ms, ImageFormat.Jpeg);
                by = ms.GetBuffer();

                pictureBox2.Image = Image.FromStream(ms);

                ms.Close();
                ms.Dispose();
                return Compress.CompressBytes(by);
            }
            catch
            {
                ms.Close();
                ms.Dispose();
                return null;
            }
        }

        #endregion

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            //this.MaximizeBox = true;
            //pictureBox2.Dock = DockStyle.Fill;
        }

        public void ToggleButton( )
        {
            timer1.Enabled = !timer1.Enabled;
            button1.Enabled = !button1.Enabled;
            button2.Enabled = !button2.Enabled;
        }
    }
}
// 2.听众端
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace WindForms_Client
{
    public partial class Form1 : Form
    {
        string br = "\r\n";

        public Form1()
        {
            InitializeComponent();
        }

        public void getPic()
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000);
            s.Bind(point);
            byte[] data = new byte[1024];
            s.Receive(data);
            MemoryStream ms = new MemoryStream(data);

            pictureBox1.Image = Image.FromStream(ms);

            ms.Dispose();
            s.Close();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Listen();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = true;

            timer1.Enabled = true;
        }

        public void Listen()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try
            {
                IPEndPoint iep = new IPEndPoint(IPAddress.Any, 2000);
                sock.Bind(iep);
                EndPoint ep = (EndPoint)iep;

                textBox1.Text += "收到数据!!\r\n";

                byte[] data = new byte[1024 * 64];

                int recv = sock.ReceiveFrom(data, ref ep);

                MemoryStream ms = new MemoryStream(Compress.DecompressBytes(data));

                pictureBox1.Image = Image.FromStream(ms);

                ms.Dispose();

                sock.Close();

            }

            catch (SocketException)
            {
                sock.Close();
                textBox1.Text = "\r\n 出现异常,线程终止!!";
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
            timer1.Enabled = false;
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            if (pictureBox1.Dock != DockStyle.Fill)
            {
                pictureBox1.Dock = DockStyle.Fill;
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                pictureBox1.Dock = DockStyle.None;
                this.FormBorderStyle = FormBorderStyle.Fixed3D;
                this.WindowState = FormWindowState.Normal;
            }
        }
    }
}
分享到: 更多
蓝客门户
©2001-2017 中国蓝客联盟 版权所有.
关于蓝客联盟历史宗旨章程技术服务联系我们蓝客社区