forked from richardschneider/net-mdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
78 lines (66 loc) · 2.3 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
66
67
68
69
70
71
72
73
74
75
76
77
78
using Makaretu.Dns;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Browser
{
class Program
{
public static void Main(string[] args)
{
var mdns = new MulticastService();
var sd = new ServiceDiscovery(mdns);
mdns.NetworkInterfaceDiscovered += (s, e) =>
{
foreach (var nic in e.NetworkInterfaces)
{
Console.WriteLine($"NIC '{nic.Name}'");
}
// Ask for the name of all services.
sd.QueryAllServices();
};
sd.ServiceDiscovered += (s, serviceName) =>
{
Console.WriteLine($"service '{serviceName}'");
// Ask for the name of instances of the service.
mdns.SendQuery(serviceName, type: DnsType.PTR);
};
sd.ServiceInstanceDiscovered += (s, e) =>
{
Console.WriteLine($"service instance '{e.ServiceInstanceName}'");
// Ask for the service instance details.
mdns.SendQuery(e.ServiceInstanceName, type: DnsType.SRV);
};
mdns.AnswerReceived += (s, e) =>
{
// Is this an answer to a service instance details?
var servers = e.Message.Answers.OfType<SRVRecord>();
foreach (var server in servers)
{
Console.WriteLine($"host '{server.Target}' for '{server.Name}'");
// Ask for the host IP addresses.
mdns.SendQuery(server.Target, type: DnsType.A);
mdns.SendQuery(server.Target, type: DnsType.AAAA);
}
// Is this an answer to host addresses?
var addresses = e.Message.Answers.OfType<AddressRecord>();
foreach (var address in addresses)
{
Console.WriteLine($"host '{address.Name}' at {address.Address}");
}
};
try
{
mdns.Start();
Console.ReadKey();
}
finally
{
sd.Dispose();
mdns.Stop();
}
}
}
}