From 8e51b3d0462652c888f5b92b0be61a22549f272a Mon Sep 17 00:00:00 2001 From: syneffort Date: Mon, 6 Feb 2023 13:11:18 +0900 Subject: [PATCH] echo server --- Echo/Client/Client.cs | 38 ++++++++++++++++++++++++++++++++++++++ Echo/Client/Client.csproj | 10 ++++++++++ Echo/Echo.sln | 31 +++++++++++++++++++++++++++++++ Echo/Echo/Echo.csproj | 10 ++++++++++ Echo/Echo/Program.cs | 10 ++++++++++ Echo/Server/Server.cs | 39 +++++++++++++++++++++++++++++++++++++++ Echo/Server/Server.csproj | 10 ++++++++++ 7 files changed, 148 insertions(+) create mode 100644 Echo/Client/Client.cs create mode 100644 Echo/Client/Client.csproj create mode 100644 Echo/Echo.sln create mode 100644 Echo/Echo/Echo.csproj create mode 100644 Echo/Echo/Program.cs create mode 100644 Echo/Server/Server.cs create mode 100644 Echo/Server/Server.csproj diff --git a/Echo/Client/Client.cs b/Echo/Client/Client.cs new file mode 100644 index 0000000..d2e52de --- /dev/null +++ b/Echo/Client/Client.cs @@ -0,0 +1,38 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Client; + +internal class Client +{ + static void Main(string[] args) + { + using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + { + IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000); + socket.Connect(endPoint); + + while (true) + { + string str = Console.ReadLine(); + if (str == "exit") + return; + + byte[] buffer = Encoding.UTF8.GetBytes(str); + socket.Send(buffer); + + byte[] buffer2 = new byte[256]; + int byteRead = socket.Receive(buffer2); + if (byteRead < 1) + { + Console.WriteLine("Disconnecting server..."); + return; + } + + string str2 = Encoding.UTF8.GetString(buffer2); + Console.WriteLine($"[Receive] {str2}"); + } + } + } +} \ No newline at end of file diff --git a/Echo/Client/Client.csproj b/Echo/Client/Client.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Echo/Client/Client.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Echo/Echo.sln b/Echo/Echo.sln new file mode 100644 index 0000000..3cb2965 --- /dev/null +++ b/Echo/Echo.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", "{E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E8C28E9B-3C0F-4A89-9F95-FB078E7B1D81}.Release|Any CPU.Build.0 = Release|Any CPU + {224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}.Debug|Any CPU.Build.0 = Debug|Any CPU + {224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}.Release|Any CPU.ActiveCfg = Release|Any CPU + {224CAEA2-AA8A-4DA7-AE3C-F9BE71938D63}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {1B391772-9687-4A60-8A3B-A725F424A383} + EndGlobalSection +EndGlobal diff --git a/Echo/Echo/Echo.csproj b/Echo/Echo/Echo.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Echo/Echo/Echo.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Echo/Echo/Program.cs b/Echo/Echo/Program.cs new file mode 100644 index 0000000..ed4b97f --- /dev/null +++ b/Echo/Echo/Program.cs @@ -0,0 +1,10 @@ +namespace Echo +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} \ No newline at end of file diff --git a/Echo/Server/Server.cs b/Echo/Server/Server.cs new file mode 100644 index 0000000..1308f1f --- /dev/null +++ b/Echo/Server/Server.cs @@ -0,0 +1,39 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Server; + +internal class Server +{ + static void Main(string[] args) + { + using (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(20); + + using (Socket clientSocket = serverSocket.Accept()) + { + Console.WriteLine(clientSocket.RemoteEndPoint); + + while (true) + { + byte[] buffer = new byte[256]; + int totalBytes = clientSocket.Receive(buffer); + if (totalBytes < 1) + { + Console.WriteLine("Disconnecting client..."); + return; + } + + string str = Encoding.UTF8.GetString(buffer); + Console.WriteLine(str); + + clientSocket.Send(buffer); + } + } + } + } +} \ No newline at end of file diff --git a/Echo/Server/Server.csproj b/Echo/Server/Server.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Echo/Server/Server.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + +