mDNS is Multicast DNS, which mainly enables hosts in the LAN to discover and communicate with each other without a traditional DNS server, and the port used is5353, follow the DNS protocol, and use the existing DNS information structure, syntax, and resource record types. And no new action code or response code is specified. In the LAN, the device and the device need to know each other's IP address before communicating with each other, in most cases, the IP of the device is not a static IP address, but a dynamically assigned IP address through the DHCP protocol, how to discover the device, is to show its skills by MDNS, for example: now the communication between the Internet of Things device and the app, either the app through broadcasting, or through multicast, send some specific information, the interested device responds, to achieve the discovery of LAN devices, of course, mDNS is more powerful than this.
Service registration
First, create a new .NET Core 3.1 project and use the nuget command to reference the following package:
The current service name and address are discovered by broadcasting, with the following code:
Service Discovery
Service discovery, which we can achieve through .NET/C# code, is as follows:
You can also enable service discovery to receive all broadcasts, the code is as follows:
Server: CentOS Linux release 7.8.2003 (Core), by installing avahi and avahi-tools tools, the service can also be discovered, the installation command is as follows:
avahi-daemon is a service daemon that runs on a Linux operating system and implements a lookup for a network-based Zeroconf service on a client machine. This service can implement DNS service discovery and DNS multicast specifications for the Zeroconf network. The user program receives notifications of discovered network services and resources through Linux D-Bus messaging. This daemon works with caching user program replies to help reduce network traffic resulting from replies. Run the avahi-daemon.service service with the following command:
You can use the avahi-browse command after installing avahi-tools.
You can view all MDNS services in the LAN with the following command:
To query a specified service, the command is as follows:
If you close the .NET Core application and query the service on another server, the following error will be reported:
+ eth0 IPv4 test _itsvse_discovery._tcp local Failed to resolve service 'test' of type '_itsvse_discovery._tcp' in domain 'local': Timeout reached
Attached is the source code:
Tourists, if you want to see the hidden content of this post, please Reply
(End)
|