Ordinary domain name resolution method:
import java.net.*; public class Kkkk { public static void main(String args[]) throws Exception { InetAddress address = InetAddress.getByName("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); } } }Implement domain name resolution with dynamic host IP
The purpose is to use an INTERNET DNS HOST to direct the domain name to the host at home (personal).
At present, CABLEMODEM is very fast and has dynamic IP, so it is possible to resolve domain names to personal machines.
It can be done with very simple settings.
For example, if you have a domain name yourdomain.com, you can resolve this domain name to any host.
1. The server (IP: AAAA) starts a small process of DNS dynamic resolution.
The domain name file of VeVB.COm is /var/named/VeVB.COm
Create another /var/named/VeVB.COm.static (template file can be used to generate VeVB.COm)
(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++; } } 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="VeVB.COm"; 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); } if ( str.trim().equals("BYE ")) done = true; } } incoming.close(); //Add user verification. //Write the VeVB.COm.static file in the server to VeVB.COm 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)
# Add the client to the startup (or process) every time it starts: SockTest.java import java.io.*; import java.net.*; public class SocketTest { public static void main(String[] args) { try { Socket s=new Socket("AAAA",8189); 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 file: (VeVB.COm.static)
@ ns.dnsserver.com xxxxx xxxx host IN A 127.0.0.1 // This is where to change www IN CNAME host ...