using System.Net; using System.Net.Sockets; using System.Text; namespace UdpTestApp; public class Program { private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; private static bool _continue; private static Thread readThread; private static UdpClient udpClient = null, receivingUdpClient = null; public static bool applicationQuitRequested { get; private set; } static void Main(string[] args) { while (applicationQuitRequested == false) { Console.WriteLine($"----- UDP PROTOCOL TEST APP -----"); Console.WriteLine($"Command list:"); Console.WriteLine($"s = start"); Console.WriteLine($"q = quit"); var key = Console.ReadKey(); switch (key.Key) { // start case ConsoleKey.S: Console.Write($"Write hostname: "); string? hostname = Console.ReadLine(); if (hostname == null || String.IsNullOrEmpty(hostname)) { Console.WriteLine($"Invalid hostname entered."); break; } Console.Write($"Write port number: "); string? portString = Console.ReadLine(); int port; if (portString == null || portString == "" || Int32.TryParse(portString, out port) == false) { Console.WriteLine($"Invalid port entered."); break; } try { udpClient = new UdpClient(); receivingUdpClient = new UdpClient(11001); udpClient.Connect(hostname, port); } catch (Exception ex) { Console.WriteLine($"Could not create UDP client. \n {ex}"); break; } _continue = true; readThread = new Thread(Read); readThread.Start(); Console.WriteLine("Connected to device. Write 'quit' to end communication."); while (_continue) { var message = Console.ReadLine(); if (stringComparer.Equals("quit", message)) { _continue = false; } else if (message != null && String.IsNullOrEmpty(message) == false) { try { byte[] msgBytes = Encoding.ASCII.GetBytes(message); List listBytes = new List(); listBytes.AddRange(msgBytes); listBytes.Add((byte)'\n'); var array = listBytes.ToArray(); udpClient.Send(array, array.Length); } catch (Exception) { Console.WriteLine("Message could not be sent."); } } } readThread.Join(TimeSpan.FromSeconds(2)); //readThread.Abort(); readThread = null; receivingUdpClient.Close(); receivingUdpClient = null; udpClient.Close(); udpClient = null; Console.WriteLine("Closed connection."); break; // quit case ConsoleKey.Q: applicationQuitRequested = true; break; default: Console.WriteLine($"{Environment.NewLine}Did not recognize key.. {Environment.NewLine}"); break; } } } public static void Read() { while (_continue) { try { //IPEndPoint object will allow us to read datagrams sent from any source. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); // Uses the IPEndPoint object to determine which of these two hosts responded. Console.WriteLine("Message received: " + returnData.ToString()); Console.WriteLine("Was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()); } catch { } } } }