DNS Concept: DNS stands for Domain Name System. It is generally used as named resolver.
Where DNS is used:
When we want to use front door keeper attitude such as watch men, so that all the request could come to that server first and depending on the request either that server gives you the host name or the related ipaddress of the host name DNS concept is used.
Request comes to DNS Server for ip Address of hostname1 DNS Server hostname1 10.10.10.11 hostname2 10.10.10.12 hostname3 10.10.10.13 Response will provide Ipaddress of related hostname1 to the user
Use below given code to get desired IPAddress from DNS server or proper hostname.
i
import java.util.Hashtable; import java.util.Iterator; import java.util.List; import java.util.Vector; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; public class DnsLookup { public static final String RECORD_A = "A"; public static List lookup(String hostName, String record) { List result = new Vector(); try { Hashtable env = new Hashtable(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url","dns://DNSIPADDRESS"); DirContext ictx = new InitialDirContext(env); Attributes attrs = ictx.getAttributes(hostName, new String[] { record }); Attribute attr = attrs.get(record); NamingEnumeration attrEnum = attr.getAll(); while (attrEnum.hasMoreElements()) result.add(attrEnum.next()); } catch (NamingException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } return result; } static void printList(List l) { Iterator iter = l.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } } /** * Find the DNS name associated with an IP * * @param args not used */ public static void main ( String[] args ) { printList(lookup("hostname1", RECORD_A)); /*printList(lookup("hostname1", RECORD_MX)); printList(lookup("hostname1", RECORD_NS)); printList(lookup("hostname1", RECORD_SOA));*/ } }