JAVA获取计算机IP地址

import java.net.InetAddress;
import java.net.UnknownHostException;
public class HuoQu {
    public static void main(String[] args) throws UnknownHostException {
        // TODO Auto-generated method stub
        InetAddress IP = InetAddress.getByName("DESKTP-MDC2C");                    //在我的电脑属性查看名称粘贴过来,此为我的虚拟名。
        System.out.println("第一种方式"+IP);
        
        InetAddress ip = InetAddress.getLocalHost();
        System.out.println("第二种方式"+ip);
        
        byte[] addr= {10,10,21,(byte)168};                                                   //在电脑网络设置查看自己的IP地址,粘贴过来,此为我的虚拟地址。
        InetAddress ip2=InetAddress.getByAddress(addr);
        System.out.println("第三种方式"+ip2);
        
        System.out.println(ip2.getHostName());
        byte[] address=ip2.getAddress();
        for(byte a:address) {
            int b=(a<0)?(a+256):a;
            System.out.println("常规方法"+b+".");
        }
        }

}

猜你喜欢

转载自www.cnblogs.com/tongyanwujia/p/10318778.html
今日推荐