LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1029|回复: 1

求助关于获取本机ip的问题

[复制链接]
发表于 2005-8-15 11:18:06 | 显示全部楼层 |阅读模式
本人需要获取本机ip地址,但通过程序段
   InetAddress addr = InetAddress.getLocalHost();
     localhostip = addr.getHostAddress().toString();
     得到的ip是127.0.0.1,是回环地址,并非真实ip地址

    然后本人尝试通过程序段
    String eth = "eth0";
      NetworkInterface face = NetworkInterface.getByName(eth);
      Enumeration ethip = face.getInetAddresses();
      来获取ip地址,其得到的字符串确是 fe80:0:0:0:230:48ff:fe22:3746

     实在不解,希望大家多多指教,怎样才能获取到ip地址.谢谢!
发表于 2005-8-31 10:06:03 | 显示全部楼层
import java.net.*;

/**
* @author 空心菜
* @date 2005-8-31 9:59:16
*/
public class Test {
        String ip;

        String name;

        public Test() {
                try {
                        InetAddress addr = InetAddress.getLocalHost();
                        ip = addr.getHostAddress();
                        name = addr.getHostName();
                        System.out.println("ip:" + ip + " 地址" + name);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public static void main(String agrs[]) {
                new Test();
        }
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表