Briefly introduce the local network connection requirements of the project: vehicle-machine connection to mobile devices.

So how do we connect.

  1. The first thing we came up with was the easiest way to connect over IP.

  2. UDP broadcast.

With UDP broadcast we can send a message to all devices on the same local network. Upon receiving the message we can resolve the port and IP of the sender.

Question:

  1. Power consumption problem. Constant UDP Broadcast consumes a lot of power.
  2. Network congestion. UDP broadcast sends messages to all hosts on the same local network. If you send too many messages, it may cause network congestion.
  3. Stability,UDP Socket can be broken due to various circumstances.

### What is Bonjour?

bonjourIs a base released by AppleZEROCONFThe working group(IETFThe subordinate group)The work of.Used to achieve zero configuration network networking solutions.BonjourIs based onIPLayer protocol.**

Bonjour is a zero-configuration network protocol launched by Apple. Its main purpose is to solve key problems such as IP acquisition, name resolution and service discovery of network devices in the absence of a central server.

What can ###Bonjour do

What Bonjour can do is solve the three problems of IP acquisition, name resolution, and service discovery in the absence of a central server.

  • Obtain THE IP address On a traditional network, you can obtain the IP address of a device in either of the following ways: static configuration: manually assign an IP address to the device; dynamic configuration: The device obtains a dynamic IP address through the DHCP service of a router. On a network without a central server, there is no central server to provide DHCP services, and it is not convenient for users to manually configure IP addresses. Therefore, a new method is required to help devices obtain IP addresses. In this way, the device can proactively specify an AVAILABLE IP address for itself. In the IPV6 environment, the IPV6 protocol itself provides the device with the ability to specify IP addresses, so the implementation is very simple, directly use IPV6 protocol support. In IPV4, Bonjour randomly assigns an IP address that belongs to the local network segment to the device, and then checks whether the IP address conflicts locally. If there is a conflict, a new IP address is randomly generated until an AVAILABLE IP address is found. I did not test this part when I did the test, but used the dynamic address of DHCP. I’ll share the results later when I have time to test this section.
  • ** Name resolution ** On traditional networks, the mapping between names and IP addresses is resolved through the DNS service. When a device needs to access a domain name, for example, www.abc.com, the device sends www.abc.com to the DNS server. The DNS server returns the IP address corresponding to the domain name, and the device uses the returned IP address to access the target server. In the network environment with no central server and no DNS server to provide domain name resolution service, name resolution becomes a serious problem. The industry’s solution to this problem is mDNS, or “multicast DNS” in Chinese, as defined in the standard document RFC6762. The principle of multicast DNS is simple. When a device needs to resolve a name, for example, abc.local, the device broadcasts a message to all devices on the local network through UDP asking who is abC. local. It responds with its OWN IP address. Multicast DNS is based on UDP and broadcasts messages. Therefore, local name resolution can be performed without a central server providing DNS resolution services. Bonjour is also based on the mDNS protocol, although Bonjour extends the mDNS protocol to enhance the device’s ability to respond to “multicast DNS” requests. Under Bonjour, applications only need to register a name, and then the job of responding to “multicast DNS” requests is handled by the underlying layer. This means that under Bonjour, applications do not need to listen for and respond to “multicast DNS” requests on the local network; the underlying system does the work. To distinguish global domain names from local domain names, the mDNS protocol uses.local. As the root of the local domain name.
  • ** Service search (automatic web search) ** When a service device obtains an IP address and assigns a domain name to itself, it still cannot meet the needs of users. Because the user needs some services, such as printing service and Web service, the user does not care about the server name and ITS IP address corresponding to these services. To make it easier for users to discover various services on their local network, Bonjour provides service discovery capabilities for devices. The “service discovery” capability provided by Bonjour is based on a simple and straightforward requirement that the device providing the service registers the service according to the following criteria: “Name. Service Type Transport Protocol Type local. For example, damonWebServer._HTTP._tcp.local. Or dummiesWebServer._http._tcp.local. This way, when a device uses an HTTP service that it wants to find, Bonjour looks for a service registered on the local network that contains “_http” and returns the result to the user to choose from. In this case, the user is faced with “DamonWebServer” and “DummiesWebServer”. The user does not care about which device the two Web services are on and what the IP address of the device is. How to use Bonjour

Bonjour is basically transparent to the end user, they don’t need to know how to use Bonjour, it’s often the application developer who thinks about how to use Bonjour. For application developers, there are two parts to consider: how to discover local services as Bonjour clients, and how to register Bonjour services as servers

  • How to discover local services as a Bonjour client

    IOS developers can use the NSNetServiceBrowser class in the NSNetService framework to discover local services.

    The basic process is as follows:

    First create an instance of NSNetServiceBrowser:
     serviceBrowser = [[NSNetServiceBrowser alloc] init];
     serviceBrowser.delegate = self;
     [serviceBrowser searchForServicesOfType:@"_http._tcp." inDomain:@"local."];Copy the code
- (void)netServiceBrowser:(NSNetServiceBrowser *)netServiceBrowser didFindService:(NSNetService *)netService MoreComing: (BOOL) moreServicesComing {}Copy the code
  • How to apply for registration as a server-side Bonjour service To register as a Bonjour services, developers can create NSNetService instance directly, and through the initwithDomain: type: name: port: method to initialize, specify service domain, type, name, and port, The sample code is as follows:

     service = [[NSNetService alloc] initWithDomain:@"local." type: @"_http._tcp." name:@"DamonWebServer" port:port];Copy the code

    After the NSNetService is created successfully, you can specify the proxy using setDelegate and publish the registered service using the publish method:

         [service setDelegate:self];
         [service publish];Copy the code

    The specified agent must comply with the NSNetServiceDelegate protocol, and can publish successful or failed services.

    Normally, if you need to publish a service, you need to prepare the service and start it before publishing it. However, NSNetService’s publish method does not depend on the service it publishes. Whether the service is ready or started, NSNetService’s publish can successfully publish the service. After the service is published, other clients using the service will find that the published service is invalid.