Java-网络初学01
getLocalHost方法
代码来源:java核心技术卷二第四章网络
package inetAddress;
import java.io.*;
import java.net.*;
/**
* This program demonstrates the InetAddress class. Supply a host name as command-line
* argument, or run without command-line arguments to see the address of the local host.
* @version 1.02 2012-06-05
* @author Cay Horstmann
*/
public class InetAddressTest
{
public static void main(String[] args) throws IOException
{
if (args.length > 0)
{
String host = args[0];
InetAddress[] addresses = InetAddress.getAllByName(host);
for (InetAddress a : addresses)
System.out.println(a);
}
else
{
InetAddress localHostAddress = InetAddress.getLocalHost();
System.out.println(localHostAddress);
}
}
}
如果不在命令行中设置任何参数,打印本地主机的IP地址,如果在命令行中指定了主机名,打印该主机所有IP地址
然后
115.24.160.45原来是校园网
在天津的河北工大
杭州的PTA
北京的百度
谷歌这个直接德国法兰克福