From 9bbe10e13094ea651853d382b58154299d8e5944 Mon Sep 17 00:00:00 2001 From: syneffort Date: Thu, 27 Oct 2022 17:54:19 +0900 Subject: [PATCH] screen send via tcp --- ScreenTCP/Client/App.config | 6 + ScreenTCP/Client/Client.cs | 77 +++++++++++ ScreenTCP/Client/Client.csproj | 84 ++++++++++++ ScreenTCP/Client/MainForm.Designer.cs | 63 +++++++++ ScreenTCP/Client/MainForm.cs | 35 +++++ ScreenTCP/Client/MainForm.resx | 120 ++++++++++++++++++ ScreenTCP/Client/Program.cs | 22 ++++ ScreenTCP/Client/Properties/AssemblyInfo.cs | 36 ++++++ .../Client/Properties/Resources.Designer.cs | 70 ++++++++++ ScreenTCP/Client/Properties/Resources.resx | 117 +++++++++++++++++ .../Client/Properties/Settings.Designer.cs | 29 +++++ ScreenTCP/Client/Properties/Settings.settings | 7 + ScreenTCP/ScreenTCP.sln | 31 +++++ ScreenTCP/ScreenTCP/App.config | 6 + ScreenTCP/ScreenTCP/ScreenTCP.csproj | 39 ++++++ ScreenTCP/Server/App.config | 6 + ScreenTCP/Server/Program.cs | 20 +++ ScreenTCP/Server/Properties/AssemblyInfo.cs | 36 ++++++ ScreenTCP/Server/Server.cs | 68 ++++++++++ ScreenTCP/Server/Server.csproj | 54 ++++++++ 20 files changed, 926 insertions(+) create mode 100644 ScreenTCP/Client/App.config create mode 100644 ScreenTCP/Client/Client.cs create mode 100644 ScreenTCP/Client/Client.csproj create mode 100644 ScreenTCP/Client/MainForm.Designer.cs create mode 100644 ScreenTCP/Client/MainForm.cs create mode 100644 ScreenTCP/Client/MainForm.resx create mode 100644 ScreenTCP/Client/Program.cs create mode 100644 ScreenTCP/Client/Properties/AssemblyInfo.cs create mode 100644 ScreenTCP/Client/Properties/Resources.Designer.cs create mode 100644 ScreenTCP/Client/Properties/Resources.resx create mode 100644 ScreenTCP/Client/Properties/Settings.Designer.cs create mode 100644 ScreenTCP/Client/Properties/Settings.settings create mode 100644 ScreenTCP/ScreenTCP.sln create mode 100644 ScreenTCP/ScreenTCP/App.config create mode 100644 ScreenTCP/ScreenTCP/ScreenTCP.csproj create mode 100644 ScreenTCP/Server/App.config create mode 100644 ScreenTCP/Server/Program.cs create mode 100644 ScreenTCP/Server/Properties/AssemblyInfo.cs create mode 100644 ScreenTCP/Server/Server.cs create mode 100644 ScreenTCP/Server/Server.csproj diff --git a/ScreenTCP/Client/App.config b/ScreenTCP/Client/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/ScreenTCP/Client/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ScreenTCP/Client/Client.cs b/ScreenTCP/Client/Client.cs new file mode 100644 index 0000000..97a681e --- /dev/null +++ b/ScreenTCP/Client/Client.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Client +{ + class Client + { + private readonly int BUFF_SIZE = 1024; + private string IP { get; set; } = "127.0.0.1"; + private int Port { get; set; } = 7000; + + public Client(string ip = "127.0.0.1", int port = 7000) + { + this.IP = ip; + this.Port = port; + } + + public byte Connect() + { + TcpClient client = new TcpClient(this.IP, this.Port); + + Bitmap bmp = CaptureScreen(); + ImageConverter imgConverter = new ImageConverter(); + byte[] imgBytes = (byte[])imgConverter.ConvertTo(bmp, typeof(byte[])); + byte[] nBytes = BitConverter.GetBytes(imgBytes.Length); + + byte[] result = new byte[1]; + using (NetworkStream stream = client.GetStream()) + { + // Send data size + stream.Write(nBytes, 0, nBytes.Length); + + // Send image + int end = imgBytes.Length; + int start = 0; + while (start < end) + { + int size = end - start >= BUFF_SIZE ? BUFF_SIZE : end - start; + stream.Write(imgBytes, start, size); + + start += size; + } + + // Receive result + result = new byte[1]; + stream.Read(result, 0, result.Length); + + Console.WriteLine(result[0]); + + } + + client.Close(); + + return result[0]; + } + + private Bitmap CaptureScreen() + { + Rectangle rect = Screen.PrimaryScreen.Bounds; + Bitmap img = new Bitmap(rect.Width, rect.Height); + + using (Graphics g = Graphics.FromImage(img)) + { + g.CopyFromScreen(rect.X, rect.Y, 0, 0, img.Size, CopyPixelOperation.SourceCopy); + } + + return img; + } + + } +} diff --git a/ScreenTCP/Client/Client.csproj b/ScreenTCP/Client/Client.csproj new file mode 100644 index 0000000..92d7c08 --- /dev/null +++ b/ScreenTCP/Client/Client.csproj @@ -0,0 +1,84 @@ + + + + + Debug + AnyCPU + {25D8FBD0-15A8-4DE5-B96D-972981393133} + WinExe + Client + Client + v4.8.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + Form + + + MainForm.cs + + + + + MainForm.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git a/ScreenTCP/Client/MainForm.Designer.cs b/ScreenTCP/Client/MainForm.Designer.cs new file mode 100644 index 0000000..08bf214 --- /dev/null +++ b/ScreenTCP/Client/MainForm.Designer.cs @@ -0,0 +1,63 @@ + +namespace Client +{ + partial class MainForm + { + /// + /// 필수 디자이너 변수입니다. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 사용 중인 모든 리소스를 정리합니다. + /// + /// 관리되는 리소스를 삭제해야 하면 true이고, 그렇지 않으면 false입니다. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form 디자이너에서 생성한 코드 + + /// + /// 디자이너 지원에 필요한 메서드입니다. + /// 이 메서드의 내용을 코드 편집기로 수정하지 마세요. + /// + private void InitializeComponent() + { + this.sendButton = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // sendButton + // + this.sendButton.Location = new System.Drawing.Point(13, 13); + this.sendButton.Name = "sendButton"; + this.sendButton.Size = new System.Drawing.Size(87, 53); + this.sendButton.TabIndex = 0; + this.sendButton.Text = "Send"; + this.sendButton.UseVisualStyleBackColor = true; + this.sendButton.Click += new System.EventHandler(this.sendButton_Click); + // + // Form1 + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(109, 78); + this.Controls.Add(this.sendButton); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; + this.Name = "Form1"; + this.Text = "Form1"; + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.Button sendButton; + } +} + diff --git a/ScreenTCP/Client/MainForm.cs b/ScreenTCP/Client/MainForm.cs new file mode 100644 index 0000000..0e407b0 --- /dev/null +++ b/ScreenTCP/Client/MainForm.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Client +{ + public partial class MainForm : Form + { + public MainForm() + { + InitializeComponent(); + } + + private void sendButton_Click(object sender, EventArgs e) + { + + for (int i = 0; i < 10; i++) + { + Client client = new Client(); + byte result = client.Connect(); + } + + + + + // MessageBox.Show(result.ToString()); + } + } +} diff --git a/ScreenTCP/Client/MainForm.resx b/ScreenTCP/Client/MainForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ScreenTCP/Client/MainForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/ScreenTCP/Client/Program.cs b/ScreenTCP/Client/Program.cs new file mode 100644 index 0000000..b13634d --- /dev/null +++ b/ScreenTCP/Client/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Client +{ + static class Program + { + /// + /// 해당 애플리케이션의 주 진입점입니다. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new MainForm()); + } + } +} diff --git a/ScreenTCP/Client/Properties/AssemblyInfo.cs b/ScreenTCP/Client/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e3976bb --- /dev/null +++ b/ScreenTCP/Client/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("Client")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Client")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("25d8fbd0-15a8-4de5-b96d-972981393133")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ScreenTCP/Client/Properties/Resources.Designer.cs b/ScreenTCP/Client/Properties/Resources.Designer.cs new file mode 100644 index 0000000..c0e0969 --- /dev/null +++ b/ScreenTCP/Client/Properties/Resources.Designer.cs @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// 이 코드는 도구를 사용하여 생성되었습니다. +// 런타임 버전:4.0.30319.42000 +// +// 파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면 +// 이러한 변경 내용이 손실됩니다. +// +//------------------------------------------------------------------------------ + + +namespace Client.Properties +{ + /// + /// 지역화된 문자열 등을 찾기 위한 강력한 형식의 리소스 클래스입니다. + /// + // 이 클래스는 ResGen 또는 Visual Studio와 같은 도구를 통해 StronglyTypedResourceBuilder + // 클래스에서 자동으로 생성되었습니다. + // 멤버를 추가하거나 제거하려면 .ResX 파일을 편집한 다음 /str 옵션을 사용하여 + // ResGen을 다시 실행하거나 VS 프로젝트를 다시 빌드하십시오. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 이 클래스에서 사용하는 캐시된 ResourceManager 인스턴스를 반환합니다. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Client.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 이 강력한 형식의 리소스 클래스를 사용하여 모든 리소스 조회에 대해 현재 스레드의 CurrentUICulture 속성을 + /// 재정의합니다. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/ScreenTCP/Client/Properties/Resources.resx b/ScreenTCP/Client/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/ScreenTCP/Client/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ScreenTCP/Client/Properties/Settings.Designer.cs b/ScreenTCP/Client/Properties/Settings.Designer.cs new file mode 100644 index 0000000..de5fe1e --- /dev/null +++ b/ScreenTCP/Client/Properties/Settings.Designer.cs @@ -0,0 +1,29 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +namespace Client.Properties +{ + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/ScreenTCP/Client/Properties/Settings.settings b/ScreenTCP/Client/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/ScreenTCP/Client/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/ScreenTCP/ScreenTCP.sln b/ScreenTCP/ScreenTCP.sln new file mode 100644 index 0000000..5683e0f --- /dev/null +++ b/ScreenTCP/ScreenTCP.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.32630.194 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{25D8FBD0-15A8-4DE5-B96D-972981393133}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{FB13CF8A-7A24-4E3C-BB41-04B55BD03A6E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {25D8FBD0-15A8-4DE5-B96D-972981393133}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {25D8FBD0-15A8-4DE5-B96D-972981393133}.Debug|Any CPU.Build.0 = Debug|Any CPU + {25D8FBD0-15A8-4DE5-B96D-972981393133}.Release|Any CPU.ActiveCfg = Release|Any CPU + {25D8FBD0-15A8-4DE5-B96D-972981393133}.Release|Any CPU.Build.0 = Release|Any CPU + {FB13CF8A-7A24-4E3C-BB41-04B55BD03A6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB13CF8A-7A24-4E3C-BB41-04B55BD03A6E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB13CF8A-7A24-4E3C-BB41-04B55BD03A6E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB13CF8A-7A24-4E3C-BB41-04B55BD03A6E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4E75ADF2-7A82-49A1-B5EE-E55481790BA4} + EndGlobalSection +EndGlobal diff --git a/ScreenTCP/ScreenTCP/App.config b/ScreenTCP/ScreenTCP/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/ScreenTCP/ScreenTCP/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ScreenTCP/ScreenTCP/ScreenTCP.csproj b/ScreenTCP/ScreenTCP/ScreenTCP.csproj new file mode 100644 index 0000000..d70110d --- /dev/null +++ b/ScreenTCP/ScreenTCP/ScreenTCP.csproj @@ -0,0 +1,39 @@ + + + + + Debug + AnyCPU + {17D59AAC-755B-4ADB-A5B3-DB698B1B354F} + Exe + ScreenTCP + ScreenTCP + v4.8.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + \ No newline at end of file diff --git a/ScreenTCP/Server/App.config b/ScreenTCP/Server/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/ScreenTCP/Server/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ScreenTCP/Server/Program.cs b/ScreenTCP/Server/Program.cs new file mode 100644 index 0000000..7aa46e5 --- /dev/null +++ b/ScreenTCP/Server/Program.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Server +{ + class Program + { + static void Main(string[] args) + { + Server server = new Server(); + server.Start(); + + Console.WriteLine("Press any key to exit."); + Console.ReadKey(); + } + } +} diff --git a/ScreenTCP/Server/Properties/AssemblyInfo.cs b/ScreenTCP/Server/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..381e288 --- /dev/null +++ b/ScreenTCP/Server/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("Server")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Server")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("fb13cf8a-7a24-4e3c-bb41-04b55bd03a6e")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ScreenTCP/Server/Server.cs b/ScreenTCP/Server/Server.cs new file mode 100644 index 0000000..477fba6 --- /dev/null +++ b/ScreenTCP/Server/Server.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Server +{ + class Server + { + private readonly int BUFF_SIZE = 1024; + + private TcpListener listener; + + public async void Start() + { + listener = new TcpListener(IPAddress.Any, 7000); + listener.Start(); + + while (true) + { + TcpClient client = await listener.AcceptTcpClientAsync().ConfigureAwait(false); + string clientEndPoint = client.Client.RemoteEndPoint.ToString(); + NetworkStream stream = client.GetStream(); + + // Receive data size + byte[] bytes = new byte[4]; + int readSize = await stream.ReadAsync(bytes, 0, bytes.Length).ConfigureAwait(false); + if (readSize != 4) + throw new ApplicationException("Invalid size"); + + int totalSize = BitConverter.ToInt32(bytes, 0); + Console.WriteLine($"[{clientEndPoint}] Data size: {totalSize} byte"); + + // Receive data + string fileName = Guid.NewGuid().ToString("N") + ".png"; + using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) + { + byte[] buff = new byte[BUFF_SIZE]; + int received = 0; + while (received < totalSize) + { + int size = totalSize - received >= BUFF_SIZE ? BUFF_SIZE : totalSize - received; + readSize = await stream.ReadAsync(buff, 0, size).ConfigureAwait(false); + received += readSize; + + await fs.WriteAsync(buff, 0, readSize); + + Console.WriteLine($"[{clientEndPoint}] Receive: {received}/{totalSize}"); + } + } + + // Send result + byte[] result = new byte[1]; + result[0] = 1; + await stream.WriteAsync(result, 0, result.Length).ConfigureAwait(false); + + Console.WriteLine($"[{clientEndPoint}] Result: {(result[0] == 1 ? "success" : "fail")}"); + + stream.Close(); + client.Close(); + } + } + } +} diff --git a/ScreenTCP/Server/Server.csproj b/ScreenTCP/Server/Server.csproj new file mode 100644 index 0000000..9524f35 --- /dev/null +++ b/ScreenTCP/Server/Server.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {FB13CF8A-7A24-4E3C-BB41-04B55BD03A6E} + Exe + Server + Server + v4.8.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file