Scenario / Questions

  1. 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?

  2. 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?

  3. 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?

  4. 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)?

  5. 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?

  6. 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).

Find below all possible solutions or suggestions for the above questions..

Suggestion: 1
  1. 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:

    1. IPv4 (and IPv6) link-local addressing
    2. Multicast Name Resolution (mDNS)
    3. DNS Service Discovery (DNS-SD)

    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.

  2. 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.

  3. 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 type ping Alice.local, and Bob should do an mDNS lookup of Alice.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 to Alice.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 and Canon._printer._tcp.local, and DNS-SD lookups on those service names would show that those services are available on Alice.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’s sshd is OpenSSH, which doesn’t support Bonjour directly, but macOS takes care of advertising the ssh service via Bonjour so that you can just ssh username@Alice.local from other machines on the LAN.

  4. On macOS, there’s a “dns-sd” command-line tool that can register a service using this syntax:

    dns-sd -R <Name> <Type> <Domain> <Port> [<TXT>...]  
    # (Register a service)
    

    So for example:

    dns-sd -R MyWebsite _http._tcp local 80
    

    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.

  5. 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:

    $ dns-sd -B _http._tcp local  
    Browsing for _http._tcp.local  
    Timestamp     A/R Flags if Domain                    Service Type              Instance Name  
    16:30:59.870  Add     3  6 local.                    _http._tcp.               My Cool Web App  
    16:30:59.871  Add     3  6 local.                    _http._tcp.               Someone Else's Web Service  
    16:30:59.871  Add     3  6 local.                    _http._tcp.               A Third One  
    ^C
    

    Look up the one I want, “My Cool Web App”, with -L:

    $ dns-sd -L "My Cool Web App" _http._tcp local  
    Lookup My Cool Web App._http._tcp.local  
    16:31:52.678  My\032Cool\032Web\032App._http._tcp.local. can be reached at MyWebServer.local.:80 (interface 6)  
    ^C  
    

    Query the IP addresses for MyWebServer.local, with -Q:

    $ dns-sd -Q MyWebServer.local  
    Timestamp     A/R Flags if Name                             T   C Rdata  
    16:32:40.786  Add     2  6 MyWebServer.local.               1   1 169.254.45.209  
    ^C  
    

    Note in these examples that you must Ctrl-C out of the dns-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, the dns-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).

Suggestion: 2

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?

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.

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?

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.

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?

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).

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)

I’m not aware of a command-line tool that does this, but many major programming languages have libraries available to do this.

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?

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

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?

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 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.

I’m not aware of a tool that does this, but it might exist.

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.

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:

Suggestion: 3

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.