From 257b0864954bf03c8b38c4a01ad6846e7b0c80b2 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 24 Mar 2023 12:03:20 +0100 Subject: [PATCH] added second client for receiving udp messages --- UdpTestApp/Program.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/UdpTestApp/Program.cs b/UdpTestApp/Program.cs index 32e3610..80e46db 100644 --- a/UdpTestApp/Program.cs +++ b/UdpTestApp/Program.cs @@ -9,7 +9,7 @@ public class Program private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; private static bool _continue; private static Thread readThread; - private static UdpClient udpClient = null; + private static UdpClient udpClient = null, receivingUdpClient = null; public static bool applicationQuitRequested { get; private set; } @@ -52,12 +52,13 @@ public class Program try { - udpClient = new UdpClient(11000); + udpClient = new UdpClient(); + receivingUdpClient = new UdpClient(11001); udpClient.Connect(hostname, port); } - catch (Exception) + catch (Exception ex) { - Console.WriteLine($"Could not connect to endpoint."); + Console.WriteLine($"Could not create UDP client. \n {ex}"); break; } @@ -97,6 +98,8 @@ public class Program readThread.Join(TimeSpan.FromSeconds(2)); //readThread.Abort(); readThread = null; + receivingUdpClient.Close(); + receivingUdpClient = null; udpClient.Close(); udpClient = null; Console.WriteLine("Closed connection."); @@ -128,7 +131,7 @@ public class Program IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); // Blocks until a message returns on this socket from a remote host. - Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); + Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); // Uses the IPEndPoint object to determine which of these two hosts responded.