-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
003370e
commit 1b976bb
Showing
4 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\Mdns.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="Properties\" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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); | ||
}; | ||
|
||
mdns.AnswerReceived += (s, e) => | ||
{ | ||
// Is this an answer to a instances query? | ||
var pointers = e.Message.Answers.OfType<PTRRecord>().Where(p => p.Name != ServiceDiscovery.ServiceName); | ||
foreach (var pointer in pointers) | ||
{ | ||
Console.WriteLine($"service instance '{pointer.DomainName}'"); | ||
|
||
// Ask for the service instance details. | ||
mdns.SendQuery(pointer.DomainName, type: DnsType.SRV); | ||
} | ||
|
||
// 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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters