[img]
[img]http://dl.iteye.com/upload/attachment/0081/8590/1931be4f-72e5-3617-85a8-1add1b72d533.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0081/8592/96fe0081-ca5d-3b3f-af94-d12750ecb3ea.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0081/8594/032df6fb-8aee-3b8e-b0ec-c689225074ff.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0081/8596/de69b1ee-881f-3293-a64d-12c3c1f1c689.png[/img]
[/img]
运行结果:
[img]http://dl.iteye.com/upload/attachment/0081/8590/1931be4f-72e5-3617-85a8-1add1b72d533.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0081/8592/96fe0081-ca5d-3b3f-af94-d12750ecb3ea.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0081/8594/032df6fb-8aee-3b8e-b0ec-c689225074ff.png[/img]
[/img]
[img]
[img]http://dl.iteye.com/upload/attachment/0081/8596/de69b1ee-881f-3293-a64d-12c3c1f1c689.png[/img]
[/img]
package com.zzl.thread;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NetDemo {
public static void main(String[] args) {
try {
//获取本机的ip地址和计算机名
InetAddress ia = InetAddress.getLocalHost();
String name = ia.getHostName();
String address = ia.getHostAddress();
System.out.println("name==="+name);
System.out.println("address==="+address);
} catch (UnknownHostException e) {
e.printStackTrace();
}
try {
//获取百度的主机地址和主机名
InetAddress ia = InetAddress.getByName("www.baidu.com");
String name = ia.getHostName();
String address = ia.getHostAddress();
System.out.println("百度name=="+name);
System.out.println("百度address=="+address);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
运行结果:
name===admin-PC
address===192.168.0.124
百度name==www.baidu.com
百度address==61.135.169.125