From f97220d7c2b591ac843e39da0b5b59b7eabb26db Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 7 Feb 2023 18:15:24 +0900 Subject: [PATCH] base chat module --- Chat/Chat.sln | 37 ++++++++++ Chat/Chat/Chat.csproj | 10 +++ Chat/Chat/Program.cs | 10 +++ Chat/Core/Core.csproj | 9 +++ Chat/Core/PClient.cs | 35 ++++++++++ Chat/Core/PServer.cs | 69 +++++++++++++++++++ Chat/Server/Program.cs | 12 ++++ Chat/Server/Server.csproj | 14 ++++ Chat/TestConsoleClient/Program.cs | 13 ++++ .../TestConsoleClient.csproj | 14 ++++ 10 files changed, 223 insertions(+) create mode 100644 Chat/Chat.sln create mode 100644 Chat/Chat/Chat.csproj create mode 100644 Chat/Chat/Program.cs create mode 100644 Chat/Core/Core.csproj create mode 100644 Chat/Core/PClient.cs create mode 100644 Chat/Core/PServer.cs create mode 100644 Chat/Server/Program.cs create mode 100644 Chat/Server/Server.csproj create mode 100644 Chat/TestConsoleClient/Program.cs create mode 100644 Chat/TestConsoleClient/TestConsoleClient.csproj diff --git a/Chat/Chat.sln b/Chat/Chat.sln new file mode 100644 index 0000000..b8af116 --- /dev/null +++ b/Chat/Chat.sln @@ -0,0 +1,37 @@ + +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", "{44FE944D-8D60-4A9C-B739-32F53A033880}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Core", "Core\Core.csproj", "{0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestConsoleClient", "TestConsoleClient\TestConsoleClient.csproj", "{E8D23703-2D2E-43C2-9B77-16EA04CD7331}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {44FE944D-8D60-4A9C-B739-32F53A033880}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {44FE944D-8D60-4A9C-B739-32F53A033880}.Debug|Any CPU.Build.0 = Debug|Any CPU + {44FE944D-8D60-4A9C-B739-32F53A033880}.Release|Any CPU.ActiveCfg = Release|Any CPU + {44FE944D-8D60-4A9C-B739-32F53A033880}.Release|Any CPU.Build.0 = Release|Any CPU + {0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0BF351F8-CA96-446D-83DE-00CAE7B4CC5D}.Release|Any CPU.Build.0 = Release|Any CPU + {E8D23703-2D2E-43C2-9B77-16EA04CD7331}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E8D23703-2D2E-43C2-9B77-16EA04CD7331}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E8D23703-2D2E-43C2-9B77-16EA04CD7331}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E8D23703-2D2E-43C2-9B77-16EA04CD7331}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {812F7F1E-3CAD-4992-A7E2-D53C6FB1D1AA} + EndGlobalSection +EndGlobal diff --git a/Chat/Chat/Chat.csproj b/Chat/Chat/Chat.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Chat/Chat/Chat.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Chat/Chat/Program.cs b/Chat/Chat/Program.cs new file mode 100644 index 0000000..606d9e0 --- /dev/null +++ b/Chat/Chat/Program.cs @@ -0,0 +1,10 @@ +namespace Chat +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} \ No newline at end of file diff --git a/Chat/Core/Core.csproj b/Chat/Core/Core.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/Chat/Core/Core.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/Chat/Core/PClient.cs b/Chat/Core/PClient.cs new file mode 100644 index 0000000..22c948f --- /dev/null +++ b/Chat/Core/PClient.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Core +{ + public class PClient + { + private Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + private EndPoint endPoint; + + public PClient(string ip, int port) + { + endPoint = new IPEndPoint(IPAddress.Parse(ip), port); + } + + public async Task StartAsync() + { + await socket.ConnectAsync(endPoint); + while (true) + { + string str = Console.ReadLine() ?? string.Empty; + byte[] dataBuffer = Encoding.UTF8.GetBytes(str); + byte[] headerBuffer = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)dataBuffer.Length)); + + await socket.SendAsync(headerBuffer, SocketFlags.None); + await socket.SendAsync(dataBuffer, SocketFlags.None); + } + } + } +} diff --git a/Chat/Core/PServer.cs b/Chat/Core/PServer.cs new file mode 100644 index 0000000..81b4cc1 --- /dev/null +++ b/Chat/Core/PServer.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Core +{ + public class PServer + { + private Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + public PServer(string ip, int port, int backlog) + { + IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port); + serverSocket.Bind(endPoint); + serverSocket.Listen(backlog); + } + + public async Task StartAsync() + { + while (true) + { + Socket clientSocket = await serverSocket.AcceptAsync(); + Console.WriteLine($"[{DateTime.Now}] Accept client: {clientSocket.RemoteEndPoint}"); + ThreadPool.QueueUserWorkItem(RunAsync, clientSocket); + } + } + + private async void RunAsync(object? sender) + { + if (sender == null) + return; + + Socket clientSocket = (Socket)sender; + byte[] headerBuffer = new byte[2]; + + while (true) + { + // header buffer + int n1 = await clientSocket.ReceiveAsync(headerBuffer, SocketFlags.None); + if (n1 < 1) + { + Console.WriteLine($"[{DateTime.Now}] Disconnect client: {clientSocket.RemoteEndPoint}"); + clientSocket.Dispose(); + return; + } + else if (n1 == 1) + { + await clientSocket.ReceiveAsync(new ArraySegment(headerBuffer, 1, 1), SocketFlags.None); + } + + // data buffer + short dataSize = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(headerBuffer)); + byte[] dataBuffer = new byte[dataSize]; + + int totalRecv = 0; + while (totalRecv < dataSize) + { + int n2 = await clientSocket.ReceiveAsync(new ArraySegment(dataBuffer, totalRecv, dataSize - totalRecv), SocketFlags.None); + totalRecv += n2; + } + Console.WriteLine($"[{DateTime.Now}] {clientSocket.RemoteEndPoint} client: {Encoding.UTF8.GetString(dataBuffer)}"); + } + } + } +} diff --git a/Chat/Server/Program.cs b/Chat/Server/Program.cs new file mode 100644 index 0000000..dbe24d2 --- /dev/null +++ b/Chat/Server/Program.cs @@ -0,0 +1,12 @@ +using Core; + +namespace Server; + +internal class Program +{ + static async Task Main(string[] args) + { + PServer server = new PServer("127.0.0.1", 20000, 1000); + await server.StartAsync(); + } +} \ No newline at end of file diff --git a/Chat/Server/Server.csproj b/Chat/Server/Server.csproj new file mode 100644 index 0000000..09a2cd7 --- /dev/null +++ b/Chat/Server/Server.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + diff --git a/Chat/TestConsoleClient/Program.cs b/Chat/TestConsoleClient/Program.cs new file mode 100644 index 0000000..87a338b --- /dev/null +++ b/Chat/TestConsoleClient/Program.cs @@ -0,0 +1,13 @@ +using Core; + +namespace TestConsoleClient +{ + internal class Program + { + static async Task Main(string[] args) + { + PClient client = new PClient("127.0.0.1", 20000); + await client.StartAsync(); + } + } +} \ No newline at end of file diff --git a/Chat/TestConsoleClient/TestConsoleClient.csproj b/Chat/TestConsoleClient/TestConsoleClient.csproj new file mode 100644 index 0000000..09a2cd7 --- /dev/null +++ b/Chat/TestConsoleClient/TestConsoleClient.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + +