A simple example of using Java to implement DNS domain name resolution _java

Source: Internet
Author: User
Tags sin

Common domain Name resolution method:

Import java.net.*; 
 
public class Kkkk {public 
 static void Main (String args[]) throws Exception { 
 inetaddress address = inetaddress.ge Tbyname ("wxh-pc");//WXH-PC is my Computer name 
 System.out.println (address); 
 SYSTEM.OUT.PRINTLN ("-----"); 
 InetAddress Address1 = Inetaddress.getlocalhost (); 
 System.out.println (ADDRESS1); 
  
  
 Inetaddress[] addresses = inetaddress 
  . Getallbyname ("www.baidu.com"); 
 System.out.println (addresses.length); 
 for (inetaddress addr:addresses) { 
  System.out.println (addr);}}} 
 

Implementation of domain name resolution with Dynamic host IP


Objective to use an Internet DNS host to redirect the domain name to the host (individual) in the home.
At present, Cablemodem is very fast, and has dynamic IP, so there is a domain name resolution to the personal machine possible.
As long as the simple setting can be done.
  
For example, you have a domain named yourdomain.com, you can resolve this domain name to any host.
  
1. Server (IP:A.A.A.A) a small process that initiates DNS dynamic resolution.
Jb51.net's domain name file is/var/named/jb51.net

Build a/var/named/jb51.net.static (template file can be used to generate jb51.net)
  
(JDK1.3, Linux)

  # The original code is as follows: Threaddnsreloadserver.java import java.io.*;
  
  Import java.net.*;
  public class Threaddnsreloadserver {public static void main (string[] args) {int i=1;
  try {serversocket s=new serversocket (8189);
  for (;;)
  {Socket incoming =s.accept ();
  System.out.println ("Accept new client:" + i);
  New Threaddnsreloadhandle (Incoming,i). Start ();
  i++;
  The catch (Exception e) {System.out.println (e); Class Threaddnsreloadhandle extends Thread {public threaddnsreloadhandle (Socket i,int c) {incoming= I ; Counter=c; public void Run () {try {BufferedReader in=new BufferedReader (New InputStreamReader (Incoming.getinputstream ())
  
  );
  String user= "Unkown";
  String dnsfilepath= "/var/named/";
  String dnsfile= "Jb51.net";
  String hostip= "127.0.0.1";
  Boolean user_validated=true;
  
  Boolean done=false;
  while (!done) {String str=in.readline ();
  Validate user;
  if (str ==null) done=true; else {Str=str.trim ();
  if (str.substring (0,2). Equals ("LA")) {if (Str.length () >2) user=str.substring (2);
  } if (Str.substring (0,2). Equals ("LB")) {if (Str.length () >2) dnsfile=str.substring (2);
  } if (Str.substring (0,2). Equals ("LC")) {if (Str.length () >2) hostip=str.substring (2);
  The IF (Str.trim (). Equals ("BYE") is done = true;
  } incoming.close ();
  Join user authentication.
  Writes the jb51.net.static file in the server to the Jb51.net String dnstmp =dnsfilepath + dnsfile + ". Static";
  BufferedReader sin=new BufferedReader (New FileReader (dnstmp));
  PrintWriter sout=new PrintWriter (New FileWriter (Dnsfilepath + dnsfile), true);
  String s;
  while ((S=sin.readline ())!= null) {if (S.startswith ("host")) {s= ' host in A ' + hostip;
  Sout.println (s);
  S= "@ in A" + hostip;
  } sout.println (s);
  }//system.out.println ("User:" + user);
  System.out.println ("Dnsfile:" + dnsfile);
  
  System.out.println ("HostIP:" + hostip);
  NDC Reload String command= "NDC reload" + dnsfile; JAva.lang.Runtime.getRuntime (). exec (command);
  catch (Exception e) {System.out.println (e);
  } private Socket incoming;
  
  private int counter;
 }

  
2. Client (Windows/linux)
  

# The client is added to the boot (or process) each time it is started: Socktest.java
import java.io.*;
Import java.net.*;
public class Sockettest
{public static void main (string[] args)
{try
{socket s=new socket ("A.A.A.A", 8  189);
BufferedReader in=new BufferedReader (New InputStreamReader (S.getinputstream ()));
PrintWriter out=new PrintWriter (S.getoutputstream (), true);
InetAddress localhostaddress =inetaddress.getlocalhost ();  Get LocalHost Ip
out.println ("Laguest");
Out.println ("lbyourdomain.com");
Out.println ("LC" + localhostaddress.gethostaddress ());
Out.close ();
S.close ();
catch (IOException e)
{System.out.println (e);
}
}
      

DNS Sample files: (jb51.net.static)

@ ns.dnsserver.com xxxxx
xxxx
host in A 127.0.0.1//Here is the place to change
www. CNAME host
. ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.