Java IP对象InetAddress
类 InetAddress
此类表示互联网协议 (IP) 地址。
没有构造函数
方法摘要 |
|
static InetAddress |
getLocalHost() |
getHostAddress() |
|
getHostName() |
|
static InetAddress |
getByName(String host) (String 可以是IP也可以是主机名) |
static InetAddress[] |
getAllByName(String host) |
toString() |
|
byte[] |
getAddress() |
// 获取本地主机IP地址对象 InetAddress address = InetAddress.getLocalHost(); // 获取其他主机IP地址对象 // address = InetAddress.getByName("119.103.127.20"); // address = InetAddress.getByName("www.baidu.com"); // InetAddress ip1[] = InetAddress.getAllByName("www.qq.com"); // for (InetAddress s : ip1) { // System.out.println(s.getHostAddress()); // } // 获取ip地址 String ip = address.getHostAddress(); // 119.103.127.20 // 获取主机名 String hname = address.getHostName(); // Lenovo System.out.println(address.toString()); // Lenovo/119.103.127.20