• UDP简单桌面同步


    最近公司接到 视频会议的项目,顺便找了一些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(
    0000new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));

                    Image im 
    = bmp.GetThumbnailImage(320256null, 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;
                }
            }
        }
    }
  • 相关阅读:
    Lc40_组合总和II
    Spring整合ZooKeeper基础使用介绍
    常见Bean拷贝框架下划线驼峰互转扩展支持
    ElastchSearch 基本使用姿势
    Java中两种分页遍历的使用姿势
    【SpringBoot DB系列】Mybatis多数据源配置与使用
    【SpringBoot DB 系列】Mybatis-Plus 多数据源配置
    【SpringBoot DB 系列】Mybatis 基于 AbstractRoutingDataSource 与 AOP 实现多数据源切换
    【基础系列】ConfigurationProperties 配置绑定中那些你不知道的事情
    Spring 工具类之基本元素判断
  • 原文地址:https://www.cnblogs.com/dzone/p/2072541.html
Copyright © 2020-2023  润新知