From 6e1f0b237ff6fe220ea824a0828c28ba70a71987 Mon Sep 17 00:00:00 2001
From: renzaijianghu4 <867189791@qq.com>
Date: Thu, 28 Jul 2022 17:08:08 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EUDP=E5=B1=8F=E5=B9=95?=
=?UTF-8?q?=E8=8E=B7=E5=8F=96demo?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ScreenUdpReceiver/Form1.Designer.cs" | 63 +++++++++
.../ScreenUdpReceiver/Form1.cs" | 40 ++++++
.../ScreenUdpReceiver/Form1.resx" | 60 +++++++++
.../ScreenUdpReceiver/Program.cs" | 23 ++++
.../ScreenUdpReceiver.csproj" | 13 ++
.../ScreenUdpSender/Form1.Designer.cs" | 55 ++++++++
.../ScreenUdpSender/Form1.cs" | 123 ++++++++++++++++++
.../ScreenUdpSender/Form1.resx" | 63 +++++++++
.../ScreenUdpSender/Program.cs" | 23 ++++
.../ScreenUdpSender/ScreenUdpSender.csproj" | 13 ++
10 files changed, 476 insertions(+)
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.Designer.cs"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.cs"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.resx"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Program.cs"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/ScreenUdpReceiver.csproj"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.Designer.cs"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.cs"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.resx"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Program.cs"
create mode 100644 "examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/ScreenUdpSender.csproj"
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.Designer.cs" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.Designer.cs"
new file mode 100644
index 00000000..a1a4d721
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.Designer.cs"
@@ -0,0 +1,63 @@
+namespace ScreenUdpReceiver
+{
+ partial class Form1
+ {
+ ///
+ /// Required designer variable.
+ ///
+ private System.ComponentModel.IContainer components = null;
+
+ ///
+ /// Clean up any resources being used.
+ ///
+ /// true if managed resources should be disposed; otherwise, false.
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && (components != null))
+ {
+ components.Dispose();
+ }
+ base.Dispose(disposing);
+ }
+
+ #region Windows Form Designer generated code
+
+ ///
+ /// Required method for Designer support - do not modify
+ /// the contents of this method with the code editor.
+ ///
+ private void InitializeComponent()
+ {
+ this.pictureBox1 = new System.Windows.Forms.PictureBox();
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
+ this.SuspendLayout();
+ //
+ // pictureBox1
+ //
+ this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
+ this.pictureBox1.Location = new System.Drawing.Point(0, 0);
+ this.pictureBox1.Name = "pictureBox1";
+ this.pictureBox1.Size = new System.Drawing.Size(800, 450);
+ this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
+ this.pictureBox1.TabIndex = 0;
+ this.pictureBox1.TabStop = false;
+ //
+ // Form1
+ //
+ this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+ this.ClientSize = new System.Drawing.Size(800, 450);
+ this.Controls.Add(this.pictureBox1);
+ this.Name = "Form1";
+ this.Text = "ScreenReceiver 感谢若汝棋茗大佬的无私奉献 -xfdl 监听地址127.0.0.1:7790";
+ this.Load += new System.EventHandler(this.Form1_Load);
+ ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
+ this.ResumeLayout(false);
+
+ }
+
+ #endregion
+
+ private System.Windows.Forms.PictureBox pictureBox1;
+ }
+}
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.cs" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.cs"
new file mode 100644
index 00000000..95f37f35
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.cs"
@@ -0,0 +1,40 @@
+using System;
+using System.Drawing;
+using System.Windows.Forms;
+using TouchSocket.Core.Config;
+using TouchSocket.Sockets;
+
+namespace ScreenUdpReceiver
+{
+ public partial class Form1 : Form
+ {
+ UdpSession udpSession;
+ public Form1()
+ {
+ InitializeComponent();
+ }
+
+ private void Form1_Load(object sender, EventArgs e)
+ {
+ try
+ {
+ udpSession = new UdpSession();
+
+ udpSession.Received += (endpoint, byteBlock, requestInfo) =>
+ {
+ pictureBox1.Image = Image.FromStream(byteBlock);
+ };
+ udpSession.Setup(new TouchSocketConfig()
+ .SetBindIPHost(new IPHost("127.0.0.1:7790"))
+ .SetBufferLength(1024 * 1024)
+ .SetUdpDataHandlingAdapter(() => { return new UdpPackageAdapter() { MaxPackageSize = 1024 * 1024 }; })
+ ).Start();
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show($"错误:{ex.Message},程序将退出");
+ Environment.Exit(0);
+ }
+ }
+ }
+}
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.resx" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.resx"
new file mode 100644
index 00000000..f298a7be
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Form1.resx"
@@ -0,0 +1,60 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
\ No newline at end of file
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Program.cs" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Program.cs"
new file mode 100644
index 00000000..e2849896
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/Program.cs"
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace ScreenUdpReceiver
+{
+ internal static class Program
+ {
+ ///
+ /// The main entry point for the application.
+ ///
+ [STAThread]
+ static void Main()
+ {
+ Application.SetHighDpiMode(HighDpiMode.SystemAware);
+ Application.EnableVisualStyles();
+ Application.SetCompatibleTextRenderingDefault(false);
+ Application.Run(new Form1());
+ }
+ }
+}
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/ScreenUdpReceiver.csproj" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/ScreenUdpReceiver.csproj"
new file mode 100644
index 00000000..08a55206
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpReceiver/ScreenUdpReceiver.csproj"
@@ -0,0 +1,13 @@
+
+
+
+ WinExe
+ netcoreapp3.1
+ true
+
+
+
+
+
+
+
\ No newline at end of file
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.Designer.cs" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.Designer.cs"
new file mode 100644
index 00000000..985a25cd
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.Designer.cs"
@@ -0,0 +1,55 @@
+namespace ScreenUdpSender
+{
+ partial class Form1
+ {
+ ///
+ /// Required designer variable.
+ ///
+ private System.ComponentModel.IContainer components = null;
+
+ ///
+ /// Clean up any resources being used.
+ ///
+ /// true if managed resources should be disposed; otherwise, false.
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && (components != null))
+ {
+ components.Dispose();
+ }
+ base.Dispose(disposing);
+ }
+
+ #region Windows Form Designer generated code
+
+ ///
+ /// Required method for Designer support - do not modify
+ /// the contents of this method with the code editor.
+ ///
+ private void InitializeComponent()
+ {
+ this.components = new System.ComponentModel.Container();
+ this.timer1 = new System.Windows.Forms.Timer(this.components);
+ this.SuspendLayout();
+ //
+ // timer1
+ //
+ this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
+ //
+ // Form1
+ //
+ this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+ this.ClientSize = new System.Drawing.Size(800, 450);
+ this.Name = "Form1";
+ this.Text = "Sender 感谢若汝棋茗大佬的无私奉献 -xfdl 监听地址127.0.0.1:7789,发送地址127.0.0.1:7790";
+ this.Load += new System.EventHandler(this.Form1_Load);
+ this.ResumeLayout(false);
+
+ }
+
+ #endregion
+
+ private System.Windows.Forms.Timer timer1;
+ }
+}
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.cs" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.cs"
new file mode 100644
index 00000000..6c6609ac
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.cs"
@@ -0,0 +1,123 @@
+using System;
+using System.Drawing;
+using System.IO;
+using System.Net;
+using System.Runtime.InteropServices;
+using System.Windows.Forms;
+using TouchSocket.Core.ByteManager;
+using TouchSocket.Core.Config;
+using TouchSocket.Sockets;
+
+namespace ScreenUdpSender
+{
+ public partial class Form1 : Form
+ {
+ UdpSession udpSession;
+ public Form1()
+ {
+ InitializeComponent();
+ }
+
+ private void Form1_Load(object sender, EventArgs e)
+ {
+ timer1.Enabled = true;
+ try
+ {
+ udpSession = new UdpSession();
+
+ udpSession.Setup(
+ new TouchSocketConfig()
+ .SetBindIPHost(new IPHost(7789))
+ .SetBufferLength(1024 * 1024)
+ .SetUdpDataHandlingAdapter(() => { return new UdpPackageAdapter() { MaxPackageSize = 1024 * 1024 }; })
+ ).Start();
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show($"错误:{ex.Message},程序将退出");
+ Environment.Exit(0);
+ }
+ }
+
+ private void timer1_Tick(object sender, EventArgs e)
+ {
+ byte[] byteArray = ImageToByte(getScreen());
+ ByteBlock bb = new ByteBlock(byteArray);
+ udpSession.Send(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7790), bb);
+ }
+
+
+ #region 屏幕和光标获取
+ [DllImport("user32.dll")]
+ static extern bool GetCursorInfo(out CURSORINFO pci);
+
+ private const Int32 CURSOR_SHOWING = 0x00000001;
+ [StructLayout(LayoutKind.Sequential)]
+ struct POINT
+ {
+ public Int32 x;
+ public Int32 y;
+ }
+
+ [StructLayout(LayoutKind.Sequential)]
+ struct CURSORINFO
+ {
+ public Int32 cbSize;
+ public Int32 flags;
+ public IntPtr hCursor;
+ public POINT ptScreenPos;
+ }
+
+ public Image getScreen(int x = 0, int y = 0, int width = -1, int height = -1, String savePath = "", bool haveCursor = true)
+ {
+ if (width == -1) width = SystemInformation.VirtualScreen.Width;
+ if (height == -1) height = SystemInformation.VirtualScreen.Height;
+
+ Bitmap tmp = new Bitmap(width, height); //按指定大小创建位图
+ Graphics g = Graphics.FromImage(tmp); //从位图创建Graphics对象
+ g.CopyFromScreen(x, y, 0, 0, new Size(width, height)); //绘制
+
+ // 绘制鼠标
+ if (haveCursor)
+ {
+ try
+ {
+ CURSORINFO pci;
+ pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
+ GetCursorInfo(out pci);
+ System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor);
+ cur.Draw(g, new Rectangle(pci.ptScreenPos.x, pci.ptScreenPos.y, cur.Size.Width, cur.Size.Height));
+ }
+ catch (Exception ex) { } // 若获取鼠标异常则不显示
+ }
+
+ //Size halfSize = new Size((int)(tmp.Size.Width * 0.8), (int)(tmp.Size.Height * 0.8)); // 按一半尺寸存储图像
+ //if (!savePath.Equals("")) saveImage(tmp, tmp.Size, savePath); // 保存到指定的路径下
+
+ return tmp; //返回构建的新图像
+ }
+ #endregion
+
+ #region 格式转换
+ private byte[] ImageToByte(Image Picture)
+ {
+ MemoryStream ms = new MemoryStream();
+ if (Picture == null)
+ return new byte[ms.Length];
+ Picture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
+ byte[] BPicture = new byte[ms.Length];
+ BPicture = ms.GetBuffer();
+ return BPicture;
+ }
+
+ private Image ByteToImage(byte[] btImage)
+ {
+ if (btImage.Length == 0)
+ return null;
+ System.IO.MemoryStream ms = new System.IO.MemoryStream(btImage);
+ System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
+ return image;
+ }
+ #endregion
+ }
+}
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.resx" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.resx"
new file mode 100644
index 00000000..d7310886
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Form1.resx"
@@ -0,0 +1,63 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ 17, 17
+
+
\ No newline at end of file
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Program.cs" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Program.cs"
new file mode 100644
index 00000000..dd1a6999
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/Program.cs"
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace ScreenUdpSender
+{
+ internal static class Program
+ {
+ ///
+ /// The main entry point for the application.
+ ///
+ [STAThread]
+ static void Main()
+ {
+ Application.SetHighDpiMode(HighDpiMode.SystemAware);
+ Application.EnableVisualStyles();
+ Application.SetCompatibleTextRenderingDefault(false);
+ Application.Run(new Form1());
+ }
+ }
+}
diff --git "a/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/ScreenUdpSender.csproj" "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/ScreenUdpSender.csproj"
new file mode 100644
index 00000000..08a55206
--- /dev/null
+++ "b/examples/Udp\347\256\200\345\215\225\347\244\272\344\276\213/ScreenUdpSender/ScreenUdpSender.csproj"
@@ -0,0 +1,13 @@
+
+
+
+ WinExe
+ netcoreapp3.1
+ true
+
+
+
+
+
+
+
\ No newline at end of file
--
GitLab