在thinking in java里有个取本机IP的程序段,里面用到了一个包,是作者自己定义的。我这里没有光盘,所以就不知道包是做什么用的了。类还是给你写下来,看有没有用。import java.net.*;public class WhoAmI {
public static void main(String[] args) throws Exception{
if (args.length != 1) {
System.err.pringln("usage:WhoAmI machineName"):
System.exit(1);
}
InetAddress a = InetAddress.getByName(args[0]);
System.out.println(a);
}
}
public static void main(String[] args) throws Exception{
if (args.length != 1) {
System.err.pringln("usage:WhoAmI machineName"):
System.exit(1);
}
InetAddress a = InetAddress.getByName(args[0]);
System.out.println(a);
}
}
要取IP地址这样最精确:
InetAddress clientIp = InetAddress.getByName(InetAddress.getLocalHost().getHostName());
String myIp = clientIp.getHostAddress();虽然在application中可以简化成 InetAddress.getLocalHost().getHostAddress(),但是这样在applet中不能取出正确的ip地址(在IE下取不出,但用appletviewer还是可以的)