java 获取局域网ip的HostName

package com.zeus.rpc.thrift.server.demo;

import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;

/**  * Created by Administrator on 2016/2/18.  * java 获取局域网ip的HostName  */ public class TestMyTool {

    public static void main(String[] args) throws SocketException, UnknownHostException {


       // byte[] bs = new byte[] { (byte) 127, (byte) 0, 0, 1 };
       // System.out.println(InetAddress.getByAddress(bs).getHostName());

        testIP("192.168.1.109");
        testIP("192.168.1.104");
        testIP("192.168.1.118");
        testIP("192.168.1.107");
        testIP("192.168.1.161");
        testIP("192.168.1.131");


    }

    private static void testIP(String ip) throws UnknownHostException {
        byte[] address=toIpByte(ip);
        System.out.println(Arrays.toString(address));
        InetAddress addr = InetAddress.getByAddress(address);
        System.out.println(addr.getHostName());
    }

    private static byte[] toIpByte(String ip) {
        String[] ips=ip.split("\\.");
        byte[] address=new byte[ips.length];
        for (int i=0;i<ips.length;i++)
        {
            address[i]=(byte) Integer.parseInt(ips[i]);
        }
        return address;
    }
}

你可能感兴趣的:(java 获取局域网ip的HostName)