First, what exactly Bonjour does (pleas read my guesses written bellow)?
Here I found out that Bonjour enables automatic discovery of computers, devices, and services on IP networks. But I thought that it not only "discovers devices on IP network" it also creates an IP network by assigning IP addresses to devices where Bonjour is running. Am I right?And I still miss the essence. Does it work in the following way? First I connect devices (for example laptops) physically so that they potentially can communicate with each other. Then, let say, on some laptops I have Bonjour running and then, as a consequence, these laptops assign IP addresses to them self in automatic way. So, laptops (where Bonjour is running) build an IP network. Does it work in this way?
Or may be a computer running Bonjour is not considered as a service and it does not broadcast itself just because Bonjour is running on this computer. I mean that the applications running on the computers need to use Bonjour to broadcast themself. So, it is applications that broadcast themself (not computers) and it is not done automatically (application needs to broadcast themself explicitly). Is it right?
How exactly my application can broadcast itself? Can I use command line to register an service (so that all applications using Bonjour knows that a new service appeared)?
Further, I would like to have an application which use the IP network created by Bonjour. For that my application needs to know which devices/services are present in the network. In more details, my application needs to have a list of services. Each service in the list should have a name, the IP address where it is running and the port which is used by the application. Can Bonjour provide this information in some way? If it is the case, how exactly it works. How my program can get this information from Bonjour? Can my program read some file created by Bonjour and containing the above mentioned information? Can I use some commands in command line to retrieve this information?
I have a special interest in accessing the information about services from files, environment variables or commands in command line. These options seems to me to be the simplest! Since in these case I do not need to use any additional libraries to communicate with Bonjour from a particular programming language.
P.S. Pleas ask questions if something is not clear in my question. I will try to formulate my question in a more clear way.
P.P.S. I use Windows 7.
ADDED: I plan to write my applications in PHP. Every computer should run a Apache web server. And I want to use Bonjour to help computer discover each other (computers are working in a local network).
Yes. Stuart Cheshire, who was the creator and is a primary maintainer of Rendezvous/Bonjour at Apple, who also co-chaired the IETF ZeroConf working group, and wrote the O’Reilly book on Zero Configuration Networking, has described Bonjour as a “three-legged stool” where the legs are:
The IETF ZeroConf working group and Apple both consider link-local addressing, especially IPv4 link-local addressing (
169.254.0.0/16
addresses) to be part of ZeroConf/Bonjour, even though link-local addressing shipped years before the other two “legs of the stool”.Note that since Windows already supports automatic link-local addressing even without Apple’s Bonjour for Windows software installed, many Windows users do not think of IPv4 link-local addressing to be part of Bonjour/ZeroConf.
Yes. Macs and Windows machines, by default, do IPv4 link-local addressing if they are configured for DHCP but there is no DHCP server available. Linux and BSD machines with Avahi (or possibly other ZeroConf implementations) installed will also do this.
If a computer is running Bonjour, its hostname is published on the LAN via mDNS. If your machine’s name is “Alice”, it will be
Alice.local
via mDNS. From another computer (let’s call it “Bob”) on the same LAN (specifically, on the same link-local multicast domain), you should be able to simply typeping Alice.local
, and Bob should do an mDNS lookup ofAlice.local
to discover Alice's IP address(es), and ping (one of) the address(es) it gets back.Note, though, that Bonjour differentiates between hostnames and service names. For example, if you have two separate USB printers, let’s say “HP” and “Canon”, connected to Alice, and Alice is acting as, say, an
lpr
print server for both of them, they can each show up as their own service, which maps back toAlice.local
as the host.Their service names would show up to the user as “HP” and “Canon” with no mention of Alice. Behind the scenes, they would be known as
HP._printer._tcp.local
andCanon._printer._tcp.local
, and DNS-SD lookups on those service names would show that those services are available onAlice.local
on two different TCP ports.So yes, applications must notify the Bonjour daemon (called
mDNSResponder
in Apple’s implementation) that they have services they want to advertise. macOS has mechanisms to automatically handle service advertisement for legacy services that are not natively Bonjour-aware. For instance, macOS'ssshd
is OpenSSH, which doesn't support Bonjour directly, but macOS takes care of advertising thessh
service via Bonjour so that you can justssh [email protected]
from other machines on the LAN.On macOS, there's a "dns-sd" command-line tool that can register a service using this syntax:
So for example:
I would not be surprised if it is included in Bonjour for Windows, or the Bonjour SDK for Windows, or if you can compile it for Windows from Apple’s mDNSResponder open-source project. Googling for
dns-sd.exe
, I see such a thing exists. I am not sure I would just download a binary for it. Instead I would try to get it from one of the packages mentioned above, or compile it myself from the mDNSResponder project sources.You can also use the
dns-sd
command-line tool to browse for services and look them up. Here is an example of looking up a local web service:Browse for local web services with
-B
:Look up the one I want, "My Cool Web App", with
-L
:Query the IP addresses for MyWebServer.local, with
-Q
:Note in these examples that you must
Ctrl-C
out of thedns-sd
tool. Otherwise it will stay open forever, continuously watching the network and reporting any changes in the results of the query you issued (such as web servers coming and going on the network, while you sit with a-B
browse query open). I have found that for this and other reasons, thedns-sd
tool is not well suited for being called from a script. You might want to look at what the ZeroConf libraries for your preferred language after all.To answer one of your other questions, I am not aware of any ZeroConf implementation that allows you to perform queries and get results just by reading/writing files. Most apps that use Bonjour do so by calling the APIs, either directly (C/C++/Obj-C/Swift apps) or through a library specific to the language (interpreted/scripting languages).
Not quite; as Jon pointed out, DHCP (usually) is used to allocate IP addresses. In much the same way that ordinary DNS translates domain names to IP addresses, Bonjour is used to translate temporary domain names (within the local network) to the IP addresses providing them.
Nope; the laptops are mostly likely getting their IP addresses from a local DHCP server. Bonjour just helps them work out what services each can provide to the others.
Yes - generally, your application will need to advertise its existence explicitly. This can often be done using system-provided libraries (common on OS X) or custom libraries (e.g. perl's Net::Bonjour).
I'm not aware of a command-line tool that does this, but many major programming languages have libraries available to do this.
Most of the Bonjour libraries should be able to do this for you; see the first example at http://search.cpan.org/~chlige/Net-Bonjour-0.96/lib/Net/Bonjour.pm
Using the appropriate libraries, the program uses the Bonjour protocol to ask all the other computers in your local network to tell it what services they have available. That library will then parse the responses and present them to you in a more useful format.
I'm not aware of a tool that does this, but it might exist.
Some more information about what the program you're writing will do, and what language you're writing it in, would make it easier to help.
For more introductory information, try both:
Bonjour does not assign IP addresses - it is a discovery protocol - you still need DHCP/Static/Link-Local(APIPA) IP addresses for it to work.
It uses multicast DNS (mDNS) to discover what hosts are on the same broadcast domain as itself and essentially it becomes its own DNS server.
See Bonjour & Zeroconf for the lowdown.
Unless your application is for a home/un-managed network, that is to say, lacking a local DNS server you just don't need it.