From db563460ba3904d7553e0ba52fff63d73e9e3d12 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 24 Mar 2023 12:44:37 +0100 Subject: [PATCH] implemented tcp server --- ProtocolTests.sln | 8 ++- TcpTestApp/Program.cs | 112 +++++++++++++++++++++++++++++++++++ TcpTestApp/TcpTestApp.csproj | 10 ++++ UdpTestApp/Program.cs | 2 +- 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 TcpTestApp/Program.cs create mode 100644 TcpTestApp/TcpTestApp.csproj diff --git a/ProtocolTests.sln b/ProtocolTests.sln index 00040f8..a87f23b 100644 --- a/ProtocolTests.sln +++ b/ProtocolTests.sln @@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerialTestApp", "SerialTest EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsbHidTestApp", "UsbHidTestApp\UsbHidTestApp.csproj", "{39995F3A-1305-4053-9A9C-3432C1FF25B6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UdpTestApp", "UdpTestApp\UdpTestApp.csproj", "{97751007-78CC-4356-9F17-F118BA9297FB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UdpTestApp", "UdpTestApp\UdpTestApp.csproj", "{97751007-78CC-4356-9F17-F118BA9297FB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TcpTestApp", "TcpTestApp\TcpTestApp.csproj", "{AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {97751007-78CC-4356-9F17-F118BA9297FB}.Debug|Any CPU.Build.0 = Debug|Any CPU {97751007-78CC-4356-9F17-F118BA9297FB}.Release|Any CPU.ActiveCfg = Release|Any CPU {97751007-78CC-4356-9F17-F118BA9297FB}.Release|Any CPU.Build.0 = Release|Any CPU + {AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AFDCE6F1-D0E8-42DA-B0D9-201958AC0BAD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TcpTestApp/Program.cs b/TcpTestApp/Program.cs new file mode 100644 index 0000000..c6290bd --- /dev/null +++ b/TcpTestApp/Program.cs @@ -0,0 +1,112 @@ +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace TcpTestApp; + +internal class Program +{ + private static Thread? serverThread; + private static CancellationTokenSource? cts; + private static TcpListener? listener; + + public static bool applicationQuitRequested { get; private set; } + + static void Main(string[] args) + { + while (applicationQuitRequested == false) + { + Console.WriteLine($"----- TCP Server for Protocol Testing -----"); + Console.WriteLine($"Command list:"); + Console.WriteLine($"s = start server"); + Console.WriteLine($"e = stop server"); + Console.WriteLine($"q = quit"); + + var key = Console.ReadKey(true); + + switch (key.Key) + { + // start server + case ConsoleKey.S: + Console.Write($"Write port number to listen on: "); + string? portString = Console.ReadLine(); + int port; + + if (portString == null || + portString == "" || + Int32.TryParse(portString, out port) == false) + { + Console.WriteLine($"Invalid port entered."); + break; + } + + var ipEndPoint = new IPEndPoint(IPAddress.Any, port); + listener = new(ipEndPoint); + + try + { + listener.Start(); + } + catch (Exception ex) + { + Console.WriteLine($"Could not start TCP server. \n {ex}"); + break; + } + + cts = new CancellationTokenSource(); // reset token + serverThread = new Thread(() => HandleTcp(cts)); + serverThread.Start(); + + Console.WriteLine("Started TCP server."); + break; + + // stop server + case ConsoleKey.E: + cts?.Cancel(); + serverThread?.Join(); + serverThread = null; + listener?.Stop(); + Console.WriteLine("Stopped TCP server."); + break; + + // quit + case ConsoleKey.Q: + applicationQuitRequested = true; + break; + + default: + Console.WriteLine($"{Environment.NewLine}Did not recognize key.. {Environment.NewLine}"); + break; + } + + + } + } + + static async void HandleTcp(CancellationTokenSource cts) + { + while (!cts.IsCancellationRequested) + { + try + { + using TcpClient handler = await listener!.AcceptTcpClientAsync(cts.Token); + if(handler != null) + { + Console.WriteLine($"Accepted connection from client {handler.Client.RemoteEndPoint}"); + await using NetworkStream stream = handler.GetStream(); + + var message = $"Welcome to the test TCP server.\n Current Time: {DateTime.Now}"; + var dateTimeBytes = Encoding.UTF8.GetBytes(message); + await stream.WriteAsync(dateTimeBytes); + + Console.WriteLine($"Sent message to client: \"{message}\""); + } + } + catch(Exception ex) + { + listener?.Stop(); + Console.WriteLine($"Listener was stopped because of exception: {ex}"); + } + } + } +} \ No newline at end of file diff --git a/TcpTestApp/TcpTestApp.csproj b/TcpTestApp/TcpTestApp.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/TcpTestApp/TcpTestApp.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/UdpTestApp/Program.cs b/UdpTestApp/Program.cs index 80e46db..ae2766d 100644 --- a/UdpTestApp/Program.cs +++ b/UdpTestApp/Program.cs @@ -17,7 +17,7 @@ public class Program { while (applicationQuitRequested == false) { - Console.WriteLine($"----- USB HID PROTOCOL TEST APP -----"); + Console.WriteLine($"----- UDP PROTOCOL TEST APP -----"); Console.WriteLine($"Command list:"); Console.WriteLine($"s = start"); Console.WriteLine($"q = quit");