使用InetAddress获取IP地址及计算机名

/**
 *  InetAddress类没有提供构造,不可以实例化对象
 */
import java.net.InetAddress;
import java.util.Arrays;
public class InetAdressTest {
    public static void main(String[] args) throws Exception {
        InetAddress address = InetAddress.getLocalHost();
        System.out.println(address);//计算机名/IP地址
        System.out.println(address.getHostName());//计算机名字
        System.out.println(address.getHostAddress());//计算计ip

        byte[] bytes = address.getAddress();
        System.out.println(Arrays.toString(bytes));//得到[10.10.0.-128],为什么会得到负数呢?
        // 是因为byte字节的长度在-128到127之间,
    }
}

猜你喜欢

转载自blog.csdn.net/gadxiong/article/details/80110219