以下是think in java经典例子,通过机器名获得IP, 运行用 java WhoAmI WhoAmI就可以了。
import java.net.*;
public class WhoAmI {
public static void main(String[] args)
throws Exception {
if(args.length != 1) {
System.err.println(
"Usage: WhoAmI MachineName");
System.exit(1);
}
InetAddress a =
InetAddress.getByName(args[0]);
System.out.println(a);
}
}
import java.net.*;
public class WhoAmI {
public static void main(String[] args)
throws Exception {
if(args.length != 1) {
System.err.println(
"Usage: WhoAmI MachineName");
System.exit(1);
}
InetAddress a =
InetAddress.getByName(args[0]);
System.out.println(a);
}
}
用那一个了?
有没有人能不能帮我解释下这个方法了?
getByAddress
public static InetAddress getByAddress(byte[] addr)
throws UnknownHostException
这个方法和public static InetAddress getByName(String host)
throws UnknownHostException
有什么区别?