欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Java实现DNS域名解析的简单示例

程序员文章站 2024-03-07 11:47:15
普通的域名解析方法: import java.net.*; public class kkkk { public static void main...

普通的域名解析方法:

import java.net.*; 
 
public class kkkk { 
 public static void main(string args[]) throws exception { 
 inetaddress address = inetaddress.getbyname("wxh-pc");// wxh-pc是我的计算机名 
 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); 
 } 
 } 
} 

实现具有动态主机ip的域名解析


    目的利用一台internet dns host 将域名定向到家中(个人)的主机上.
  目前cablemodem已经很快了,而且有动态ip,所以就有了将域名解析到个人机器的上可能.
  只要通过很简单的设置就可以搞定.
  
  例如你有一个域名叫 yourdomain.com ,你可以将此域名解析任意的主机.
  
  1.服务器 (ip: a.a.a.a) 启动dns动态解析的一个小进程.
  jb51.net的域名文件为 /var/named/jb51.net  

        再建个/var/named/jb51.net.static (模板文件可以用来生成jb51.net的)
  
  (jdk1.3 , linux )

  # 原代码如下: 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="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);
  }
  
  if ( str.trim().equals("bye")) done = true;
  }
  
  }
  incoming.close();
  //加入用户验证.
  //将服务器中的jb51.net.static文件写入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.客户机(windows / linux)
  

# 客户端每次启动时加入到开机启动(或进程中): 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",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 样本文件: ( jb51.net.static)

  @ ns.dnsserver.com xxxxx
  xxxx
  host    in a 127.0.0.1      // 此处就是要更改的地方
  www    in cname host
  ...