Know more about Network Service Discovery(NSD) in Android and approaches to discover services in LAN

Mangesh Sambare
8 min readJan 23, 2022

--

“ Technology is like the ocean. No matter how many times we dive into it, every time we learn something new”

A good programmer always thinks of architecture and a good approach before starting development or writing code. So that there will be no problem with the code in the future. If there is, a programmer can solve it effortlessly.

As mentioned in the title, this article explains Network Service Discovery (NSD) in Local Area Network (LAN) using Android application. Fing and NetX Network tool applications are already on the play store. These applications reveal what’s happening on the Wi-Fi network and get the most from your connected devices like IP addresses, services running, etc.

I would like to share my learning collected from the mistakes I have made so far by making corrections in my learning phase, here I would like to provide you with specific knowledge about this subject.I hope that after reading this, you will save valuable time and learn about better approaches.

“Apply Better Approach while writing code or making any decisions in Life”

“You will enrich your life immeasurably if you approach it with a sense of wonder and discovery, and always challenge yourself to try new things.” ~ Nate Berkus

How to build network tools applications or how to find IP addresses in the local area network (LAN) in Android?

Like Fing and NetX network tools app, our app also can find out the dynamic IP address of the server in the local vicinity(same local wifi network) and able to connect local network server using private IP.

Like Fing and NetX network tools app, our app also can find out the dynamic IP address of the server in the local vicinity(same local wifi network) and able to connect to a local network server using a private IP address.

Before starting anything, we should know the basics of the topic. So, let’s start with the local area network (LAN).

What is the local area network (LAN) ?

Cisco definition for local area network (LAN) is a collection of devices connected together in one physical location, such as a building, office, or home. A LAN can be small or large, ranging from a home network with one user to an enterprise network with thousands of users and devices in an office or school. For more information you can read here.

Approach 1

Most developers follow this approach on Linux machines to find IP addresses in the local network. The Android platform architecture is based on Linux Kernel. In Android applications, android developers can use this approach. I will not recommend this approach as it’s no better way, but some android developers may feel helpful. You can find it on stack overflow questions. Before going through, I request you to visit these questions for more clarification.

String commandToRun = "adb shell netcfg";//find device ip address
Runtime.getRuntime().exec(commandToRun);

To achieve this, I recommend the stack overflow answer. Please have a look at the following StackOverflow question.

This implementation is not a good approach, for getting an IP address and hostname. I explained it because some of the android developers might find this helpful.

Approach 2

In the first approach, the application has ping each IP address in LAN. It takes too much time to execute. Sometimes the first approach fails to execute 2–3 times out of 10 execution to configure hostname.

As a developer, we always want to write a good algorithm for faster results, once the program is executed. In this approach, we can learn how to achieve it. Network Service Discovery (NSD) gives hostnames, services in LAN. In NSD, developers don’t have to worry about pinging each IP address instance or configuring hostnames. Before knowing Network Service Discovery (NSD), we have to know about mDNS, ZeroConfig and Bonjour.

On StackOverflow question, I have to know about mDNS configuration.

What is mDNS ?

In computer networking, the multicast DNS (mDNS) protocol resolves hostnames to IP addresses within small networks that do not include a local name server. It is a zero-configuration service, using essentially the same programming interfaces, packet formats and operating semantics as unicast Domain Name Service (DNS). It was designed to work as either a stand-alone protocol or compatibly with standard DNS servers.

Read on the Wikipedia for more inforamtion.

What is Zero-Configuration ?

A set of technologies that automatically creates a usable computer network based on the Internet Protocol Suite (TCP/IP) when computers or network peripherals are interconnected. It does not require manual operator intervention or special configuration servers.

Read on Wikipedia for more information.

What is Bonjour ?

It is known as zero-configuration networking, enables automatic discovery of devices and services on a local network using industry standard IP protocols. Bonjour makes it easy to discover, publish, and resolve network services with a sophisticated, easy-to-use programming interface that is accessible from Cocoa, Ruby, Python, and other languages.

Bonjour is Apple’s implementation of zero-configuration networking (zeroconf).

Read on Wikipedia for more information.

So like mDNS, Zeroconfig, Bonjour, I searched the same thing as on Android. I found a better approach here. NSD(Network Service Discovery).

What is Avahi ?

Before knowing NSD, the developer should know about the local server services configuration in the system. Avahi is one of the discovery services be installed and running on the Linux/Ubuntu system.

Avahi is a system that facilitates service discovery on a local network via the mDNS/DNS-SD protocol suite. This enables you to plug your laptop or computer into a network and instantly be able to view other people who you can chat with, find printers to print to or find files being shared.

Read on Avahi and wikipedia for more information.

Developers can develop and run their custom discovery services like finding printers to print. These discovery services are very lightweight. For this, developers have to define service types to discover the services. The International Assigned Numbers Authority (IANA) manages a centralized, authoritative list of service types used by service discovery protocols such as NSD and Bonjour. You can download the list from the IANA list of service names and port numbers. If you intend to use a new service type, you should reserve it by filling out the IANA Ports and Service registration form.

What is Network Service Discovery ?

NSD gives your app access to services that other devices provide on a local network. Devices that support NSD include printers, webcams, HTTPS servers, and other mobile devices.

NSD implements the DNS-based Service Discovery (DNS-SD) mechanism, which allows your app to request services by specifying a type of service and the name of a device instance that provides the desired type of service. DNS-SD is supported both on Android and on other mobile platforms.

After reading definitions of ZeroConfig, Bonjour and NSD, it is like the same but used in different architecture. Bonjour is used mainly for Apple devices. Likewise, NSD is used in Android devices.
For developing NSD, Android provides developer guidelines.

Developers can implement NSD in android applications using the above android documentation.

While testing NSD implementation in the local network, In Android 5.0 (Lollipop — API Level 21) and below this version, have an issue with discovering services in the local network. I asked question on StackOverflow

Android is improvising in each version level. Google fixed the above issue in Android 6.0 (Marshmallow — API Level 23)and above( I am not sure about this, I didn’t test it. You guys have to check this functionality yourself). How can we tackle this issue, Explain in the next approach.

Approach 3

As mentioned in the above approach, In Android 5.0 and below versions, NSD does not work well. This approach will fix the above issue. Client applications can find the server in LAN using publish-subscribe functionality for that both should know the specific key and able to achieve by using Universal Datagram Protocol (UDP).

What is Universal Datagram Protocol ?

In computer networking, the User Datagram Protocol (UDP) is one of the core members of the Internet protocol suite. With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network. Prior communications are not required in order to set up communication channels or data paths.

Read on Wikipedia for more information.

While using UDP, one can find out the IP address of a particular hostname immediately in the local network. For this, write one simple UDP server code in one programming language(prefer Python over Java because Linux/ubuntu already have packages. For Java language, we have to install JDK and configure JRE for this). Look into following python code.

import socketlocalIP     = "127.0.0.1"localPort   = 20001bufferSize  = 1024msgFromServer       = "Hello UDP Client"bytesToSend         = str.encode(msgFromServer)# Create a datagram socketUDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)# Bind to address and ipUDPServerSocket.bind((localIP, localPort))print("UDP server up and listening")# Listen for incoming datagramswhile(True):bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)message = bytesAddressPair[0]address = bytesAddressPair[1]clientMsg = "Message from Client:{}".format(message)
clientIP = "Client IP Address:{}".format(address)

print(clientMsg)
print(clientIP)
# Sending a reply to clientUDPServerSocket.sendto(bytesToSend, address)

Run this code in the local system for which have to find the IP address. This code can broadcast hostname/IP address e.g sever-hostname. The above code is available here.

Now in Android, have to write a UDP client application for finding IP address or hostname in the local network. For writing code, go through stack overflow question and answers.

This functionality 100% works. And it gives you a hostname with an IP address, hostname, services within 1–5 seconds. You can use this approach also.

Conclusion

For finding the IP address of the server or discover services running on the system in the local network like Fing and NetXnetwork tool apps, use NSD, Zeroconfig, Bonjour Configurations. It is hassle-free.

But in some cases, it does not configure proper IP address using service name and service type then go through UDP. Using NSD and UDP combinations is the ultimate approach to overcome all problems. For this implementation developers(backend) should also agree on this. Rosemary Wang explained very well. Please have a look at this.

Note

For all above approaches,

  • Run code in the background thread like coroutines, work manager, rxJava, rxAndroid.
  • Run this code only when Android device WiFi is connected to Wifi Router. For this add ACCESS_NETWORK_STATE & ACCESS_WIFI_STATE in the Android Manifest file in the application (on mobile data it is unable to find the local server).
  • For getting the result of IP addresses or discovering services it takes time. It depends on Wifi Network or internet availability.

“Sharing is caring”

Sharing this research, hope this will help people to solve implementation problems in the Internet of Things (IoT). Fingalready started working in the Internet of Things (IoT). Nowadays, IoT is booming in the information technology field. Lots of companies started working on it. For more understanding about this field, you can read here.

I hope you like this article. You can clap for my work. Let me know your feedback in the comment section.

You can connect me on LinkedIn so that we can build professional connections.

More interested to read my articles then do visit Mangesh Sambare Profile.

--

--