Skip to content

Commit 0ac2ace

Browse files
committed
* More efficient checking of multicast addresses
* Allows default multicast address and port to be used (with empty input) * Better logging on error
1 parent 1aad00d commit 0ac2ace

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

multicast-test/Program.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,39 @@ public static void Main(string[] args)
5757
Console.WriteLine();
5858
while (true)
5959
{
60-
Console.Write("Enter multicast address to use: ");
61-
string? enteredMc = Console.ReadLine();
62-
63-
if(enteredMc == null || enteredMc == string.Empty) continue;
64-
List<int> enteredMcSplit = new List<int>();
65-
enteredMc.Split(".").ToList().ForEach(x => enteredMcSplit.Add(Convert.ToInt32(x)));
66-
if(enteredMcSplit.Count != 4) continue; // Not enough parts
67-
if(enteredMcSplit[0] < 224 || enteredMcSplit[0] > 239) continue; // Check first part
68-
for(int part = 1; part < 4; part++) if(enteredMcSplit[part] < 0 || enteredMcSplit[part] > 255) continue; // Check other parts
69-
MulticastAddress = IPAddress.Parse(enteredMc);
70-
break;
60+
Console.Write($"Enter multicast address to use [{MulticastAddress}]: ");
61+
string enteredMc = Console.ReadLine();
62+
if(enteredMc == null || enteredMc == string.Empty) break; // Use defult multicast address
63+
if(IPAddress.TryParse(enteredMc, out IPAddress multicastAddress))
64+
{
65+
if(IsMulticast(multicastAddress))
66+
{
67+
MulticastAddress = multicastAddress;
68+
break;
69+
}
70+
Console.WriteLine("A multicast IP addresses must be between 224.0.0.0 to 239.255.255.255.");
71+
continue;
72+
}
73+
Console.WriteLine("Not a valid IP address");
7174
}
7275

7376
// prompt to select a multicast port
7477
Console.WriteLine();
7578
while (true)
7679
{
77-
Console.Write("Enter multicast port to use: ");
78-
string? enteredPortString = Console.ReadLine();
79-
if(enteredPortString == null || enteredPortString == string.Empty) continue;
80-
if(!int.TryParse(enteredPortString, out int enteredPort)) continue;
81-
if(enteredPort < 0 || enteredPort > 65535) continue;
80+
Console.Write($"Enter multicast port to use [{MulticastPort}]: ");
81+
string enteredPortString = Console.ReadLine();
82+
if(string.IsNullOrEmpty(enteredPortString)) break; // Use default port
83+
if(!int.TryParse(enteredPortString, out int enteredPort))
84+
{
85+
Console.WriteLine("Not a valid number");
86+
continue;
87+
}
88+
if(enteredPort < 0 || enteredPort > 65535)
89+
{
90+
Console.WriteLine("Port must be between 1 and 65535");
91+
continue;
92+
}
8293
MulticastPort = enteredPort;
8394
break;
8495
}
@@ -160,7 +171,7 @@ public static void Listen()
160171
var receiveThread = new Thread(Receive);
161172
receiveThread.Start();
162173

163-
Console.WriteLine($"\nBound udp listener on {_bindingAddress}. Joined multicast group {MulticastAddress}. Waiting to receive data...\n");
174+
Console.WriteLine($"\nBound udp listener on {_bindingAddress}. Joined multicast group {MulticastAddress}. Port {MulticastPort}. Waiting to receive data...\n");
164175
}
165176

166177
public static void Receive()
@@ -185,6 +196,12 @@ public static void SendMessage(UdpClient client, string message)
185196
client.Send(data, data.Length, ipEndPoint);
186197
}
187198

199+
private static bool IsMulticast(IPAddress ipAddress)
200+
{
201+
byte addressFirstOctet = ipAddress.GetAddressBytes()[0];
202+
return addressFirstOctet >= 224 && addressFirstOctet <= 239;
203+
}
204+
188205
private static IPAddress _bindingAddress;
189206

190207
private static IPAddress MulticastAddress = IPAddress.Parse("239.0.1.2");

multicast-test/multicast-test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<RootNamespace>multicast_test</RootNamespace>
77
<ImplicitUsings>enable</ImplicitUsings>
8-
<Nullable>enable</Nullable>
8+
<Nullable>disable</Nullable>
99
</PropertyGroup>
1010

1111
</Project>

0 commit comments

Comments
 (0)