added second client for receiving udp messages

master
Jonas Arnold 3 years ago
parent 3b0f0d08f4
commit 257b086495
  1. 13
      UdpTestApp/Program.cs

@ -9,7 +9,7 @@ public class Program
private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
private static bool _continue; private static bool _continue;
private static Thread readThread; private static Thread readThread;
private static UdpClient udpClient = null; private static UdpClient udpClient = null, receivingUdpClient = null;
public static bool applicationQuitRequested { get; private set; } public static bool applicationQuitRequested { get; private set; }
@ -52,12 +52,13 @@ public class Program
try try
{ {
udpClient = new UdpClient(11000); udpClient = new UdpClient();
receivingUdpClient = new UdpClient(11001);
udpClient.Connect(hostname, port); 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; break;
} }
@ -97,6 +98,8 @@ public class Program
readThread.Join(TimeSpan.FromSeconds(2)); readThread.Join(TimeSpan.FromSeconds(2));
//readThread.Abort(); //readThread.Abort();
readThread = null; readThread = null;
receivingUdpClient.Close();
receivingUdpClient = null;
udpClient.Close(); udpClient.Close();
udpClient = null; udpClient = null;
Console.WriteLine("Closed connection."); Console.WriteLine("Closed connection.");
@ -128,7 +131,7 @@ public class Program
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks until a message returns on this socket from a remote host. // 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); string returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which of these two hosts responded. // Uses the IPEndPoint object to determine which of these two hosts responded.

Loading…
Cancel
Save