网络通讯的三要素
IP地址
public class dome {
public static void main(String[] args) {
try {
//获取本机IP地址对象
InetAddress localHost = InetAddress.getLocalHost();
//获取本机名
System.out.println(localHost.getHostName());
//获取本机地址名
System.out.println(localHost.getHostAddress());
//获取指定IP或局域网的IP地址对象
InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress.getHostName());
System.out.println(inetAddress.getHostAddress());
//判断本机和与该ip地址是否连通
System.out.println(inetAddress.isReachable(2000));
} catch (UnknownHostException e) {
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
}
}