服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - 使用Java实现DNS域名解析的简单示例

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

2020-01-09 14:32ISH_ICODE JAVA教程

这篇文章主要介绍了使用Java实现DNS域名解析的简单示例,包括对一个动态IP主机的域名解析例子,需要的朋友可以参考下

普通的域名解析方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 )

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 原代码如下: 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)
  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 客户端每次启动时加入到开机启动(或进程中): 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)

?
1
2
3
4
5
@ ns.dnsserver.com xxxxx
xxxx
host    IN A 127.0.0.1      // 此处就是要更改的地方
www    IN CNAME host
...

延伸 · 阅读

精彩推荐