From cb8fba64b1700be6f1d44cff50eec44868348ab5 Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 7 Feb 2023 13:54:42 +0900 Subject: [PATCH] apm (asynchronous programming model) --- APM/APM.sln | 31 ++++++++++++++++++++ APM/APM/APM.csproj | 10 +++++++ APM/APM/Program.cs | 10 +++++++ APM/Client/Client.cs | 50 ++++++++++++++++++++++++++++++++ APM/Client/Client.csproj | 10 +++++++ APM/Server/Server.cs | 61 ++++++++++++++++++++++++++++++++++++++++ APM/Server/Server.csproj | 10 +++++++ 7 files changed, 182 insertions(+) create mode 100644 APM/APM.sln create mode 100644 APM/APM/APM.csproj create mode 100644 APM/APM/Program.cs create mode 100644 APM/Client/Client.cs create mode 100644 APM/Client/Client.csproj create mode 100644 APM/Server/Server.cs create mode 100644 APM/Server/Server.csproj diff --git a/APM/APM.sln b/APM/APM.sln new file mode 100644 index 0000000..821c6cb --- /dev/null +++ b/APM/APM.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", "{D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{79E1A445-695E-4CBA-98D4-EE80DCD0D892}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D04D1A29-83AA-4E60-8CEA-51C4FECBF54A}.Release|Any CPU.Build.0 = Release|Any CPU + {79E1A445-695E-4CBA-98D4-EE80DCD0D892}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {79E1A445-695E-4CBA-98D4-EE80DCD0D892}.Debug|Any CPU.Build.0 = Debug|Any CPU + {79E1A445-695E-4CBA-98D4-EE80DCD0D892}.Release|Any CPU.ActiveCfg = Release|Any CPU + {79E1A445-695E-4CBA-98D4-EE80DCD0D892}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7E0DE83C-5101-4E7B-AB41-42B7DC8A4F98} + EndGlobalSection +EndGlobal diff --git a/APM/APM/APM.csproj b/APM/APM/APM.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/APM/APM/APM.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/APM/APM/Program.cs b/APM/APM/Program.cs new file mode 100644 index 0000000..6850007 --- /dev/null +++ b/APM/APM/Program.cs @@ -0,0 +1,10 @@ +namespace APM +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} \ No newline at end of file diff --git a/APM/Client/Client.cs b/APM/Client/Client.cs new file mode 100644 index 0000000..25b51fa --- /dev/null +++ b/APM/Client/Client.cs @@ -0,0 +1,50 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Client; + +internal class Client +{ + static void Main(string[] args) + { + Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000); + + socket.BeginConnect(endPoint, ConnectCompleted, socket); + + while (true) + { + string str = Console.ReadLine() ?? string.Empty; + socket.Send(Encoding.UTF8.GetBytes(str)); + } + } + + private static void ConnectCompleted(IAsyncResult ar) + { + if (ar.AsyncState == null) + return; + + Socket socket = (Socket)ar.AsyncState; + socket.EndConnect(ar); + + string str = Console.ReadLine() ?? string.Empty; + byte[] buffer = Encoding.UTF8.GetBytes(str); + + socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, SendCompleted, socket); + } + + private static void SendCompleted(IAsyncResult ar) + { + if (ar.AsyncState == null) + return; + + Socket socket = (Socket)ar.AsyncState; + socket.EndSend(ar); + + string str = Console.ReadLine() ?? string.Empty; + byte[] buffer = Encoding.UTF8.GetBytes(str); + + socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, SendCompleted, socket); + } +} \ No newline at end of file diff --git a/APM/Client/Client.csproj b/APM/Client/Client.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/APM/Client/Client.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/APM/Server/Server.cs b/APM/Server/Server.cs new file mode 100644 index 0000000..166b6b1 --- /dev/null +++ b/APM/Server/Server.cs @@ -0,0 +1,61 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Server; + +internal class Server +{ + private static readonly int BUFFER_SIZE = 256; + + static void 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); + + serverSocket.BeginAccept(AcceptCompleted, serverSocket); + + while (true) + { + Thread.Sleep(1000); + } + } + + private static void AcceptCompleted(IAsyncResult ar) + { + if (ar.AsyncState == null) + return; + + Socket serverSocket = (Socket)ar.AsyncState; + Socket clientSocket = serverSocket.EndAccept(ar); + + Console.WriteLine(clientSocket.RemoteEndPoint); + + serverSocket.BeginAccept(AcceptCompleted, serverSocket); + + byte[] buffer = new byte[BUFFER_SIZE]; + clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCompleted, (clientSocket, buffer)); // state: tuple + } + + private static void ReceiveCompleted(IAsyncResult ar) + { + if (ar.AsyncState == null) + return; + + (Socket clientSocket, byte[] buffer) = ((Socket, byte[]))ar.AsyncState; + int n1 = clientSocket.EndReceive(ar); + if (n1 < 1) + { + Console.WriteLine("Client is disconnected"); + clientSocket.Dispose(); + return; + } + + Console.WriteLine(Encoding.UTF8.GetString(buffer)); + + byte[] newBuffer = new byte[BUFFER_SIZE]; + clientSocket.BeginReceive(newBuffer, 0, newBuffer.Length, SocketFlags.None, ReceiveCompleted, (clientSocket, newBuffer)); // state: tuple + } +} \ No newline at end of file diff --git a/APM/Server/Server.csproj b/APM/Server/Server.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/APM/Server/Server.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +