From f74090a49ec78dd01b578ff3ad3c543565881165 Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 7 Feb 2023 14:44:58 +0900 Subject: [PATCH] tap (task-based asynchronous pattern) --- TAP/Client/Client.cs | 24 ++++++++++++++++++++ TAP/Client/Client.csproj | 10 +++++++++ TAP/Server/Server.cs | 47 ++++++++++++++++++++++++++++++++++++++++ TAP/Server/Server.csproj | 10 +++++++++ TAP/TAP.sln | 31 ++++++++++++++++++++++++++ TAP/TAP/Program.cs | 10 +++++++++ TAP/TAP/TAP.csproj | 10 +++++++++ 7 files changed, 142 insertions(+) create mode 100644 TAP/Client/Client.cs create mode 100644 TAP/Client/Client.csproj create mode 100644 TAP/Server/Server.cs create mode 100644 TAP/Server/Server.csproj create mode 100644 TAP/TAP.sln create mode 100644 TAP/TAP/Program.cs create mode 100644 TAP/TAP/TAP.csproj diff --git a/TAP/Client/Client.cs b/TAP/Client/Client.cs new file mode 100644 index 0000000..ea71dbf --- /dev/null +++ b/TAP/Client/Client.cs @@ -0,0 +1,24 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Client +{ + internal class Client + { + static async Task Main(string[] args) + { + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000); + + await socket.ConnectAsync(endPoint); + + while (true) + { + string str = Console.ReadLine() ?? string.Empty; + byte[] buffer = Encoding.UTF8.GetBytes(str); + await socket.SendAsync(buffer, SocketFlags.None); + } + } + } +} \ No newline at end of file diff --git a/TAP/Client/Client.csproj b/TAP/Client/Client.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/TAP/Client/Client.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/TAP/Server/Server.cs b/TAP/Server/Server.cs new file mode 100644 index 0000000..0c81bc5 --- /dev/null +++ b/TAP/Server/Server.cs @@ -0,0 +1,47 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Server +{ + internal class Server + { + private static readonly int BUFFER_SIZE = 256; + + static async Task Main(string[] args) + { + Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000); + serverSocket.Bind(endPoint); + serverSocket.Listen(1000); + + while (true) + { + Socket clientSocket = await serverSocket.AcceptAsync(); + Console.WriteLine(clientSocket.RemoteEndPoint); + ThreadPool.QueueUserWorkItem(ReadAsync, clientSocket); + } + } + + private static async void ReadAsync(object? state) + { + if (state == null) + return; + + Socket clientSocket = (Socket)state; + while (true) + { + byte[] buffer = new byte[BUFFER_SIZE]; + int n1 = await clientSocket.ReceiveAsync(buffer, SocketFlags.None); + if (n1 < 1) + { + Console.WriteLine("Client is disconnected..."); + clientSocket.Dispose(); + return; + } + + Console.WriteLine(Encoding.UTF8.GetString(buffer)); + } + } + } +} \ No newline at end of file diff --git a/TAP/Server/Server.csproj b/TAP/Server/Server.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/TAP/Server/Server.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/TAP/TAP.sln b/TAP/TAP.sln new file mode 100644 index 0000000..7a2b05f --- /dev/null +++ b/TAP/TAP.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{F632989E-390D-4098-9114-F1BCE43B8B82}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}.Release|Any CPU.Build.0 = Release|Any CPU + {F632989E-390D-4098-9114-F1BCE43B8B82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F632989E-390D-4098-9114-F1BCE43B8B82}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F632989E-390D-4098-9114-F1BCE43B8B82}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F632989E-390D-4098-9114-F1BCE43B8B82}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9E9FD3BE-FDB5-4256-AC8D-B528D1B5A9BE} + EndGlobalSection +EndGlobal diff --git a/TAP/TAP/Program.cs b/TAP/TAP/Program.cs new file mode 100644 index 0000000..75403db --- /dev/null +++ b/TAP/TAP/Program.cs @@ -0,0 +1,10 @@ +namespace TAP +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} \ No newline at end of file diff --git a/TAP/TAP/TAP.csproj b/TAP/TAP/TAP.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/TAP/TAP/TAP.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +