forked from richardschneider/net-mdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
65 lines (57 loc) · 2.02 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Common.Logging;
using Common.Logging.Simple;
using Makaretu.Dns;
using System;
using System.Linq;
namespace Spike
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Multicast DNS spike");
// set logger factory
var properties = new Common.Logging.Configuration.NameValueCollection
{
["level"] = "TRACE",
["showLogName"] = "true",
["showDateTime"] = "true",
["dateTimeFormat"] = "HH:mm:ss.fff"
};
LogManager.Adapter = new ConsoleOutLoggerFactoryAdapter(properties);
var mdns = new MulticastService();
foreach (var a in MulticastService.GetIPAddresses())
{
Console.WriteLine($"IP address {a}");
}
mdns.QueryReceived += (s, e) =>
{
var names = e.Message.Questions
.Select(q => q.Name + " " + q.Type);
Console.WriteLine($"got a query for {String.Join(", ", names)}");
};
mdns.AnswerReceived += (s, e) =>
{
var names = e.Message.Answers
.Select(q => q.Name + " " + q.Type)
.Distinct();
Console.WriteLine($"got answer for {String.Join(", ", names)}");
};
mdns.NetworkInterfaceDiscovered += (s, e) =>
{
foreach (var nic in e.NetworkInterfaces)
{
Console.WriteLine($"discovered NIC '{nic.Name}'");
}
};
var sd = new ServiceDiscovery(mdns);
sd.Advertise(new ServiceProfile("x1", "_xservice._tcp", 5011));
sd.Advertise(new ServiceProfile("x2", "_xservice._tcp", 666));
var z1 = new ServiceProfile("z1", "_zservice.udp", 5012);
z1.AddProperty("foo", "bar");
sd.Advertise(z1);
mdns.Start();
Console.ReadKey();
}
}
}