当前位置:编程学习 > JAVA >>

请问JAVA程序中如何实现根据IP地址,获得域名,也就是所说的域名反向解析

请问JAVA程序中如何实现根据IP地址,获得域名,也就是所说的域名反向解析
最近的项目中用用到这块,涉及域名和IP的管理和信息安全的内容
请知道思路的高手指教,谢谢! --------------------编程问答--------------------  InetAddress.getHostname() --------------------编程问答-------------------- java.net.InetAddress ia = InetAddress.getByName("192.168.100.100");
ia.getHostName(); 

--------------------编程问答-------------------- 反向解析首先要求被解析的机器的DNS支持,我也只理解这么多了
byte[]   b   =   {(byte)202,   (byte)119,   (byte)24,   (byte)35};    
InetAddress   addr   =   InetAddress.getByAddress(b); 
addr.getHostName() --------------------编程问答-------------------- ia.getHostName();
这个方法获取不到域名
我尝试用nslookup查询到了ip域名对应关系,但用这个方法出不来
--------------------编程问答-------------------- 我用2楼的写法打印出来就是192.168.100.100 --------------------编程问答-------------------- 你试试我3楼的方法,用byte转一下 --------------------编程问答-------------------- java技术群:69705156 --------------------编程问答--------------------

public class Test090518 {
public static void main(String args[]){
try {
        String name = Test090518.getHostNameByNbtstat("192.168.12.12");
            System.out.println("name="+name);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//  得到遠程IP的機器名
public static String getHostNameByNbtstat(String clientIP){
        String s = "";
        String sb = clientIP.trim().substring(0, 3);
        //System.out.println("clientIP="+clientIP+"\t"+"截取字符串為:"+sb);
        if(sb.equals("127")){
         //System.out.println("是127.0.0.1");
         s = getHostName();
        }else{
         //System.out.println("不是本地ip");
            try {
             String s1 = "nbtstat -a "+clientIP;
                Process process = Runtime.getRuntime().exec(s1);
                BufferedReader bufferedreader = new BufferedReader(
                 new InputStreamReader(process.getInputStream()));
                String nextLine;
                int y = 0;
                int x = 0;
                for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
                 nextLine = bufferedreader.readLine();                             
                    //System.out.println("y= "+y+" nextLine="+nextLine);                              
                    if(y==13){
                     //System.out.println("此行:-----------------");
                        //System.out.println(nextLine.indexOf("<00>")+"--------");
                        s = (nextLine.substring(0, (nextLine.indexOf("<00>")))).toLowerCase();//截取字符串
                        }
                    y++;
                 }
                 bufferedreader.close();
                 process.waitFor();
                }catch(Exception exception) {
                 s = "";
                }
        }
         return s.trim();
}
//  得到本地IP的機器名
    public static String getHostName() {
            String s = "";
            try {
                    String s1 = "ipconfig /all";
                    Process process = Runtime.getRuntime().exec(s1);
                    BufferedReader bufferedreader = new BufferedReader(
                                    new InputStreamReader(process.getInputStream()));
                    String nextLine;
                    for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
                            nextLine = bufferedreader.readLine();
                            if (line.indexOf("Host Name") <= 0) {
                                    continue;
                            }
                            int i = line.indexOf("Host Name") + 36;
                            s = line.substring(i);
                            break;
                    }
                    bufferedreader.close();
                    process.waitFor();
            } catch (Exception exception) {
                    s = "";
            }
            return s.trim();
    }
}


百度的202.108.22.5被喀嚓掉了 試其他的 --------------------编程问答-------------------- 楼上的可以获得机器名,但是不是域名呀 --------------------编程问答-------------------- 我想知道的是域名-》ip

java做的动态域名解析,哪位高手做过,求指导
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,