Welcome!

Java IoT Authors: Automic Blog, Liz McMillan, Elizabeth White, Pat Romanski, Roger Strukhoff

Related Topics: Java IoT

Java IoT: Blog Post

DNS and Java

Computers on the ARPANET used to download the hosts.txt file the central location and used it for Name Resolution

This article discusses DNS and HostName resolution in Java. The following are the topics that will be briefed in this article:

    • Hostname Resolution
    • What is DNS
    • Methods of Hostname Resolution
    • Hostname Resolution on Windows/Linux
    • DNS Client Resolver Cache
    • Viewing and Flushing DNS Cache
    • Hostname resolution in Java.
    • Debugging Approach

Hostname Resolution.
Determining the IP Address of the machine from the Hostname is termed as Hostname Resolution. Every computer (host) has a name. The Hostname should correspond to an IP address mapping stored in local Hosts file or in a database on a DNS Server.

Translating (resolving) machine (and domain) names into the numbers actually used on the Internet is the business of machines that offer the Domain Name Service.

What is DNS.

Background:
During the earlier days(in the age of ARPANET Project) the hosts.txt was used for Name Resolution. Computers on the ARPANET used to download the hosts.txt file the central location and used it for Name Resolution. As the number of hosts on internet increased usage of hosts.txt files became difficult and cumbersome process to maintain/update the hosts.txt file.

The replacement of hosts.txt file is the DNS.

Definition of DNS:
Domain Name System is a hierarchical naming system for computers, services or any resources connected to the internet or a private network.

It translates meaningful domain names to IP Addresses. An often-analogy to explain the Domain Name System is that it serves as phone-book for the internet by translating human-friendly computer hostname into IP Addresses.

Methods of HostName Resolution:

Local HostName

Return value of command hostname or the name configured for the computer.This name is compared with destination hostname.

Hosts File This file maps hostname to IP Addresses
DNS Server A server that maintains a database of IP Address-to-host name  mappings and has the ability query other DNS servers for mappings that it does not contain.

Hostname Resolution on Windows/Linux:
Windows

(a) Local hostname, if the destination address is same as local address then IP address for the same is returned and hostname resolution process is stopped.

(b) If (a) returns false, then "DNS Client Resolver Cache" is looked upon to check whether it consists of required hostname, if true then IP Address is returned else DNS server is queried to get the IP Address.

Linux

If your application needs to know the IP address of a particular computer. The application requiring this information asks the 'resolver' on your Linux PC to provide this information.

a) The resolver queries the local host file (/etc/hosts and/or the domain name servers it knows about (the exact behavior of the resolver is determined by /etc/host.conf) ;

b) If the answer is found in the host file, this answer is returned;

  • if a domain name server is specified, your PC queries this machine
  • The DNS machine already knows the IP number for the required name, it returns it. If it does not, it queries other name servers across the Internet to find the information. The name server than passes this information back to the requesting resolver - which gives the information to the requesting application.

DNS Client Resolver Cache
"DNS Client Resolver Cache" is a RAM based table (dynamically constructed) consisting of entries in the Hosts file and the host names that OS has previously resolved successfully through DNS. Response received from DNS queries are cached for a certain period of time and that period of time is configurable thru the parameter "Time to Live" (TTL).

Managing DNS Resolver cache: Following variables are used to maintain the DNS Resolver cache.

networkaddress.cache.ttl Specified in java. Security to indicate the caching policy for successful name lookups from the name service. The value is specified as integer to indicate the number of seconds to cache the successful lookup.
networkaddress.cache.negative.ttl Specified in java. Security to indicate the caching policy for un-successful name lookups from the name service. The value is specified as integer to indicate the number of seconds to cache the failure for un-successful lookups. A value of 0 indicates never cache. A value of -1 indicates cache forever.

Starting Java 6 the default value for "networkaddress.cache.ttl" is changed to 30 and this can be determined thru the following program:

public class Foo {

public static void main (String[] args) {

System.out.println(sun.net.InetAddressCachePolicy.get());

}

}

Viewing and Flushing DNS Cache

View the contents of "DNS Client Resolver Cache" on windows

Issue ipconfig /displaydns.from command prompt.
Flush the DNS Cache contents on windows. Issue ipconfig /flushdns from command prompt

Flush the DNS Cache contents on Linux. On Linux the nscd daemon manages the DNS cache.

restart the nscd daemon To restart the nscd daemon, use the command `/etc/init.d/nscd restart`.

Hosts File is a common way to resolve hostname to IP Address through a locally stored text file containing IP-address-to-host-name mappings. The file is resident in /etc/hosts on UNIX platform and systemroot/System32/Drivers/Etc folder on windows.

Hostname resolution in Java.

Hostname to IP Address is resolved in Java thru the

A) Use of combination of local machine configuration and

b) Network naming service such as DNS and

c) Network information service (NIS).

The inetAddress class provides methods to resolve hostname to IP Address and vice-versa also. Following are the methods that can be used for hostname resolution:

getAddress Returns the raw IP Address for this object.
getAllByName(String host) Given the name of host, an array of IP address is returned.
getByAddress(byte[] addr) Returns an InetAddress object given the raw IP address
getByAddress(String host, byte[] addr) Create an InetAddress based on the provided host name and IP address
getByName(String host) Determines the IP address of a host, given the host's name.
getCanonicalHostName() Gets the fully qualified domain name for this IP address.
getHostAddress() Returns the IP address string in textual presentation
getHostName() Gets the host name for this IP address
getLocalHost() Returns the local host.

 

Simple Program exercising few of the above methods:

import java.net.*;

class InetAddressTest

{

public static void main(String args[]) throws

UnknownHostException {

InetAddress Address = InetAddress.getLocalHost();

System.out.println(Address);

Address = InetAddress.getByName("starwave.com");

System.out.println(Address);

InetAddress SW[] = InetAddress.getAllByName("www.nba.com");

for (int i=0; i<SW.length; i++)

System.out.println(SW[i]);

System.out.println("InetAddress.getCanonicalHostName()"+Address.getCanonicalHostName());

}

}

Debugging Approach:

What process is making the DNS queries on my server:

Use Dtrace to determine the processes making the DNS requests.

Reference:

http://en.wikipedia.org/wiki/DTrace

http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html

http://forums.sun.com/thread.jspa?forumID=861&threadID=5418298

Further Reading:

Understanding DNS : http://www.tech-faq.com/understanding-dns.html

Java DNS Logger :http://www.genady.net/dns/

DNS Overview : http://technet.microsoft.com/en-us/library/bb727007.aspx#EHAA

Setting up Name to Address Resolution(DNS) :http://tldp.org/HOWTO/PPP-HOWTO/dns.html#AEN877

More Stories By ChandraShekar Dattatreya

Chandra Shekara Dattatreya is a DevOps guy working in a Fortune-500 company and has 10+ years' of experience debugging JVM-related issues. In the course of debugging, he has encountered multiple scenarios from various customers and provided solutions to all of them with success. In his current role, he is engaged in identifying and resolving JVM-related issues for an e-commerce company.

@ThingsExpo Stories
Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, discussed some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he covered some of the best practices for structured team migration an...
Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, discussed how they built...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and B...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
With tough new regulations coming to Europe on data privacy in May 2018, Calligo will explain why in reality the effect is global and transforms how you consider critical data. EU GDPR fundamentally rewrites the rules for cloud, Big Data and IoT. In his session at 21st Cloud Expo, Adam Ryan, Vice President and General Manager EMEA at Calligo, examined the regulations and provided insight on how it affects technology, challenges the established rules and will usher in new levels of diligence arou...
The 22nd International Cloud Expo | 1st DXWorld Expo has announced that its Call for Papers is open. Cloud Expo | DXWorld Expo, to be held June 5-7, 2018, at the Javits Center in New York, NY, brings together Cloud Computing, Digital Transformation, Big Data, Internet of Things, DevOps, Machine Learning and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding busin...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
No hype cycles or predictions of a gazillion things here. IoT is here. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, an Associate Partner of Analytics, IoT & Cybersecurity at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He also discussed the evaluation of communication standards and IoT messaging protocols, data...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
DevOps at Cloud Expo – being held June 5-7, 2018, at the Javits Center in New York, NY – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real results. Among the proven benefits,...
@DevOpsSummit at Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, is co-located with 22nd Cloud Expo | 1st DXWorld Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait...
Cloud Expo | DXWorld Expo have announced the conference tracks for Cloud Expo 2018. Cloud Expo will be held June 5-7, 2018, at the Javits Center in New York City, and November 6-8, 2018, at the Santa Clara Convention Center, Santa Clara, CA. Digital Transformation (DX) is a major focus with the introduction of DX Expo within the program. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive ov...
SYS-CON Events announced today that T-Mobile exhibited at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on qua...
SYS-CON Events announced today that Cedexis will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Cedexis is the leader in data-driven enterprise global traffic management. Whether optimizing traffic through datacenters, clouds, CDNs, or any combination, Cedexis solutions drive quality and cost-effectiveness. For more information, please visit https://www.cedexis.com.
SYS-CON Events announced today that Google Cloud has been named “Keynote Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Companies come to Google Cloud to transform their businesses. Google Cloud’s comprehensive portfolio – from infrastructure to apps to devices – helps enterprises innovate faster, scale smarter, stay secure, and do more with data than ever before.
SYS-CON Events announced today that Vivint to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. As a leading smart home technology provider, Vivint offers home security, energy management, home automation, local cloud storage, and high-speed Internet solutions to more than one million customers throughout the United States and Canada. The end result is a smart home solution that sav...
SYS-CON Events announced today that Opsani will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Opsani is the leading provider of deployment automation systems for running and scaling traditional enterprise applications on container infrastructure.
SYS-CON Events announced today that Nirmata will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nirmata provides a comprehensive platform, for deploying, operating, and optimizing containerized applications across clouds, powered by Kubernetes. Nirmata empowers enterprise DevOps teams by fully automating the complex operations and management of application containers and its underlying ...
SYS-CON Events announced today that Opsani to exhibit at SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California. Opsani is creating the next generation of automated continuous deployment tools designed specifically for containers. How is continuous deployment different from continuous integration and continuous delivery? CI/CD tools provide build and test. Continuous Deployment is the means by which...