Skip to content

Commit 04b4806

Browse files
committed
multicast
1 parent e3cbcdc commit 04b4806

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

NetDebug/MainWindow.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
<RadioButton Name="UDP_type_multicast" GroupName="UDP_type" Grid.Row="2" Checked="UDP_type_multicast_Checked" Unchecked="UDP_type_multicast_Unchecked">Multicast</RadioButton>
6060
<ComboBox Grid.Row="3" IsEditable="True" Name="UDP_multicast_ip" SelectedIndex="0" Loaded="UDP_Target_ip_Loaded" Margin="20,0,0,0">
6161
<Label>224.0.0.0</Label>
62-
<Label>224.0.0.1</Label>
6362
</ComboBox>
6463
</Grid>
6564
<Button Name="UDP_button" Grid.Row="3" Margin="20,5" Click="Open_Button_Click" FontSize="16">Open</Button>

NetDebug/MainWindow.xaml.cs

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
5+
using System.Net.NetworkInformation;
56
using System.Net.Sockets;
67
using System.Text;
78
using System.Threading;
@@ -74,22 +75,33 @@ IPAddress ParseIp(string str)
7475
{
7576
if (!IPAddress.TryParse(str, out var ip))
7677
{
77-
switch (str)
78+
switch (str.ToLower())
7879
{
7980
case "loopback":
8081
case "localhost": return IPAddress.Loopback;
8182
case "broadcast": return IPAddress.Broadcast;
82-
default: return IPAddress.IPv6Any;
83+
84+
case "ipv6any":
85+
case "v6any":
86+
case "anyv6":
87+
case "anyipv6": return IPAddress.IPv6Any;
88+
89+
case "ipv4any":
90+
case "v4any":
91+
case "anyv4":
92+
case "anyipv4":
93+
case "any":
94+
default: return IPAddress.Any;
8395
}
8496
}
8597
return ip;
8698
}
8799

88-
void LogSystem(string msg)
100+
void LogSystem(params string[] msgs)
89101
{
90102
Dispatcher.Invoke(() =>
91103
{
92-
MsgBox.Document.Blocks.Add(new Paragraph()
104+
var p = new Paragraph()
93105
{
94106
Inlines = {
95107
new Run($"[ {DateTime.Now.ToString("zzz yyyy.MM.dd tt hh:mm:ss:fff")} ] [Info]"){
@@ -99,13 +111,6 @@ void LogSystem(string msg)
99111
StylisticAlternates = 1,
100112
}
101113
},
102-
new LineBreak(),
103-
new Run(msg){
104-
Typography =
105-
{
106-
StylisticAlternates = 1,
107-
}
108-
},
109114
},
110115
Typography =
111116
{
@@ -115,7 +120,15 @@ void LogSystem(string msg)
115120
HistoricalLigatures = true,
116121
ContextualAlternates = true,
117122
},
118-
});
123+
};
124+
p.Inlines.AddRange(msgs.SelectMany(msg =>
125+
new Inline[]{
126+
new LineBreak(),
127+
new Run(msg) { Typography = { StylisticAlternates = 1, }
128+
}
129+
}));
130+
if (msgs.Length == 0) p.Inlines.Add(new LineBreak());
131+
MsgBox.Document.Blocks.Add(p);
119132

120133
MsgBox.ScrollToEnd();
121134
});
@@ -227,6 +240,7 @@ enum NowType
227240
NowType nowType = NowType.Udp;
228241

229242
object socket;
243+
List<UdpClient> sendClients = new List<UdpClient>();
230244
CancellationTokenSource loop;
231245
List<Task> loops = new List<Task>();
232246
private void Open_Button_Click(object sender, RoutedEventArgs e)
@@ -237,6 +251,15 @@ private void Open_Button_Click(object sender, RoutedEventArgs e)
237251
{
238252
uc.Close();
239253
uc.Dispose();
254+
if (sendClients != null)
255+
{
256+
sendClients.ForEach(send =>
257+
{
258+
send.Close();
259+
send.Dispose();
260+
});
261+
sendClients = null;
262+
}
240263
}
241264
else if (socket is Socket sk)
242265
{
@@ -270,7 +293,8 @@ private void Open_Button_Click(object sender, RoutedEventArgs e)
270293
}
271294
return;
272295
}
273-
var ip = ParseIp(UDP_Local_ip.Text);
296+
297+
var ip = UDP_type_multicast.IsChecked ?? false ? IPAddress.Any :ParseIp(UDP_Local_ip.Text);
274298
int port;
275299
try { port = Convert.ToInt32(UDP_Local_port.Text); }
276300
catch
@@ -288,8 +312,6 @@ private void Open_Button_Click(object sender, RoutedEventArgs e)
288312
{
289313
var uc = new UdpClient(new IPEndPoint(ip, port));
290314
socket = uc;
291-
//socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
292-
//socket.Bind(new IPEndPoint(ip, port));
293315

294316
LogSystem($"Open on {{ {ip} :{port} }}");
295317

@@ -306,11 +328,26 @@ private void Open_Button_Click(object sender, RoutedEventArgs e)
306328
if (UDP_type_multicast.IsChecked ?? false)
307329
{
308330
var mip = ParseIp(UDP_multicast_ip.Text);
309-
//var opt = new MulticastOption(mip, ip);
310-
uc.JoinMulticastGroup(mip);
311-
//socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, opt);
312331

313-
LogSystem($"Join Multicast Group {{ {mip} }}");
332+
sendClients = new List<UdpClient>();
333+
334+
var msgs = (from networkInterface in NetworkInterface.GetAllNetworkInterfaces()
335+
where networkInterface.OperationalStatus == OperationalStatus.Up
336+
select networkInterface.GetIPProperties()
337+
.UnicastAddresses.First(addr =>
338+
addr.Address.AddressFamily == AddressFamily.InterNetwork)?.Address)
339+
.Where(address => address != null)
340+
.Select(addr =>
341+
{
342+
uc.JoinMulticastGroup(mip, addr);
343+
sendClients.Add(new UdpClient(new IPEndPoint(addr, port)));
344+
return $"Join Multicast Group {{ {mip} on {addr} }}";
345+
});
346+
347+
LogSystem(msgs.ToArray());
348+
349+
//uc.JoinMulticastGroup(mip);
350+
//LogSystem($"Join Multicast Group {{ {mip} }}");
314351
}
315352

316353
loop = new CancellationTokenSource();
@@ -354,7 +391,7 @@ private void Send_Button_Click(object sender, RoutedEventArgs e)
354391
{
355392
if(nowType == NowType.Udp)
356393
{
357-
var ip = ParseIp(UDP_Target_ip.Text);
394+
var ip = UDP_type_multicast.IsChecked ?? false ? ParseIp(UDP_multicast_ip.Text) : ParseIp(UDP_Target_ip.Text);
358395
int port;
359396
try { port = Convert.ToInt32(UDP_Target_port.Text); }
360397
catch
@@ -370,15 +407,20 @@ private void Send_Button_Click(object sender, RoutedEventArgs e)
370407
try
371408
{
372409
var @byte = Encoding.Default.GetBytes(new TextRange(Send_Msg.Document.ContentStart, Send_Msg.Document.ContentEnd).Text);
373-
if(socket is UdpClient uc)
410+
if(UDP_type_multicast.IsChecked ?? false)
411+
{
412+
sendClients.ForEach(send =>
413+
{
414+
send.Send(@byte, @byte.Length, new IPEndPoint(ip, port));
415+
});
416+
} else if (socket is UdpClient uc)
374417
{
375418
uc.Send(@byte, @byte.Length, new IPEndPoint(ip, port));
376419
}
377420
else
378421
{
379422
throw new SocketException();
380423
}
381-
//socket.SendTo(@byte, new IPEndPoint(ip, port));
382424
Log(new IPEndPoint(ip, port), new TextRange(Send_Msg.Document.ContentStart, Send_Msg.Document.ContentEnd).Text);
383425
}
384426
catch (Exception ex)
@@ -416,11 +458,13 @@ private void Send_Msg_KeyUp(object sender, KeyEventArgs e)
416458
private void UDP_type_multicast_Checked(object sender, RoutedEventArgs e)
417459
{
418460
UDP_Target_ip.IsEnabled = false;
461+
UDP_Local_ip.IsEnabled = false;
419462
}
420463

421464
private void UDP_type_multicast_Unchecked(object sender, RoutedEventArgs e)
422465
{
423466
UDP_Target_ip.IsEnabled = true;
467+
UDP_Local_ip.IsEnabled = true;
424468
}
425469

426470
private void Clear_Button_Click(object sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)