惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 晨月

思恋 Google打不开的解决方法和IP地址表 Reporting service 中文参数问题的解决办法 BI项目技术资料 文本框输入限制 [大全] - 晨月 - 博客园 应用HttpClient来对付各种顽固的WEB服务器 不得不看的几个经典网站 运行 bat(批处理) 文件 一个如何从某一天格式(2005-12-31)得到该天的下一天格式(2006-01-01)的函数 谈谈JAVA程序的反编译 Oracle SQL FAQ df zt() 男人25岁之前必须懂得21个道理 爱有多销魂就有多伤人 javascript 实现多级联动 众里寻他千百度,蓦然回首,那人却在,灯火阑珊处 学习的一个过程 google高手必经之路 正则表达式小技巧
java根据网卡取本机配置的IP - 晨月 - 博客园
晨月 · 2006-03-20 · via 博客园 - 晨月

可以用如下代码:

           InetAddress inet = InetAddress.getLocalHost();
            System.out.println("本机的ip=" + inet.getHostAddress());

在window下面可以工作。在linux下返回127.0.0.1。主要是在linux下返回的是/etc/hosts中配置的localhost的ip地址,而不是网卡的绑定地址。后来改用网卡的绑定地址,可以取到本机的ip地址:)

代码如下:

//根据网卡取本机配置的IP
      Enumeration netInterfaces=NetworkInterface.getNetworkInterfaces();
      InetAddress ip = null;
   while(netInterfaces.hasMoreElements())
   {
    NetworkInterface ni=(NetworkInterface)netInterfaces.nextElement();
    System.out.println(ni.getName());
    ip=(InetAddress) ni.getInetAddresses().nextElement();
    if( !ip.isSiteLocalAddress()
    && !ip.isLoopbackAddress()
    && ip.getHostAddress().indexOf(":")==-1)
    {
     System.out.println("本机的ip=" + ip.getHostAddress());
     break;
    }
    else
    {
                   ip=null;
       }
   }

posted on 2006-03-20 08:56  晨月  阅读(1046)  评论()    收藏  举报