可能有多个网卡包括虚拟网卡,需要进行排除

String ip = "";
try {
Enumeration<?> e1 = NetworkInterface.getNetworkInterfaces();//获取多个网卡
while (e1.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) e1.nextElement(); if(("eth0").equals(ni.getName()) || ("ens33").equals(ni.getName())){//取“eth0”和“ens33”两个网卡
Enumeration<?> e2 = ni.getInetAddresses();
while (e2.hasMoreElements()) {
  InetAddress ia = (InetAddress) e2.nextElement();
   if (ia instanceof Inet6Address) {//排除IPv6地址
   continue;
  }
  ip = ia.getHostAddress();
  }
  break;
  }
}
} catch (SocketException e) {
e.printStackTrace();
}

最新文章

  1. 模板——RMQ
  2. ubuntu14.04 LTS 更新源
  3. BZOJ1272: [BeiJingWc2008]Gate Of Babylon
  4. QQwry
  5. [Python]peewee使用经验
  6. Android客户端与服务器交互方式-小结
  7. springboot 学习进度
  8. day17 python递归案例(二分查找,三级菜单)
  9. cocos2d-html5 碰撞检测的几种方法
  10. Java使用POI插件将数据以excel形式备份
  11. JAVA JComboBox的监听事件(ActionListener、ItemListener)
  12. python_ssh连接
  13. &lt;[完整版]中国式价值投资&gt;读书笔记
  14. java—实现一个监听器HttpServletRequest的创建销毁、在线人数 (56)
  15. linux 段页式内存管理
  16. linux memery dump
  17. spring+jdbc+template+transaction实现
  18. Windows服务的调试
  19. django实现密码加密的注册(数据对象插入)
  20. 小程序框架MpVue踩坑日记(一)

热门文章

  1. 加密web.config数据库连接
  2. linux yum的配置文件 repo文件详解
  3. Python CGI编程Ⅳ
  4. State Processor API:如何读取,写入和修改 Flink 应用程序的状态
  5. 自定义 Swiper 的上一页,下一页按钮
  6. 计算机网络(六),UDP报文段详解
  7. hdu 3917 修路与公司 最大权闭合图 好题
  8. 6.集成算法boosting----AdaBoost算法
  9. VS Code 最好用的 Markdown 插件
  10. js多种方法取数组的最后一个元素