Results 1 to 5 of 5
  1. #1
    anuta is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default Cnanging the DNS server

    Hi

    How can I change (from java code) the DNS server used by InetAddress for hosts resolution?

    Thanks in advance

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    What do you want to achieve? The DNS server comes from machine configuration.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    anuta is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    In Machine configuration can be defined a list of active DNS servers. Those servers can recognize a different set of domains.
    There could be an IP adress that only known by the secondary DNS server and not known by the primary one.

    When I am trying to resolve a host
    InetAddress.getByName()
    it is connected to the first active DNS server on the my machine configuration, and if the host is not recognized not trying to connect to next DNS server on the list.

    So that is what I am trying to do, to specify the DNS server for the host resolution.

  4. #4
    anuta is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Smile well no one answers so i figured it out by myself

    static public String resolveHostWithAllDnsServers(String ip)
    {
    List nameservers = sun.net.dns.ResolverConfiguration.open ().nameservers();
    for( int i =1; i< nameservers.size() ; i++ )
    {
    String dnsServer =nameservers.get(i).toString();
    try
    {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
    env.put(Context.PROVIDER_URL, "dns://"+dnsServer+"/.");
    DirContext ictx = new InitialDirContext(env);
    String reversedIp=getReversedIP(ip) ;
    Attributes attr = ictx.getAttributes(reversedIp+".in-addr.arpa",new String[]{"PTR"});
    Attribute attrib = attr.get("PTR") ;
    String host=(String)attrib.get(0);
    return host;
    }
    catch (NamingException e)
    {
    e.printStackTrace();
    }
    }
    return ip;
    }

    public static String getReversedIP(String ip)
    {
    StringTokenizer st = new java.util.StringTokenizer(ip, ".");
    String reversedIp=st.nextToken();
    while ( st.hasMoreTokens())
    {
    reversedIp = st.nextToken()+"."+reversedIp;
    }
    return reversedIp;
    }

  5. #5
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    6

    Default

    Nicely done :). Thanks for posting for others too.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

Similar Threads

  1. SQL server
    By elish11 in forum JDBC
    Replies: 2
    Last Post: 08-20-2008, 06:00 PM
  2. Replies: 2
    Last Post: 07-15-2008, 06:11 PM
  3. One server to another server redirection
    By chaudhuri_abhi in forum Java Servlet
    Replies: 1
    Last Post: 02-11-2008, 07:05 PM
  4. 123 Web Messenger Server 1.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 08-10-2007, 04:32 PM
  5. WAS Server
    By Albert in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-25-2007, 05:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •