package com.achun.test;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class HelloWorld {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("HelloWorld");
String ip = getHostAddress(false,true);
System.out.println(ip);

}

private static String getHostAddress(boolean ipv6, boolean connectTest)
{
Enumeration netInterfaces = null;
InetAddress address = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements())
{
NetworkInterface ni = (NetworkInterface)netInterfaces.nextElement();

if ((ni.isUp()) && (!ni.isLoopback()) && (!ni.isVirtual())) {
address = getInetAddress(ni, ipv6);
if (address != null)
if (connectTest) {
if (address.isReachable(3000))
return address.getHostAddress();
}
else return address.getHostAddress();
}
}
}
catch (Exception e) {
}
return "";
}

private static InetAddress getInetAddress(NetworkInterface ni, boolean ipv6)
{
Enumeration enumeration = ni.getInetAddresses();
InetAddress address = null;
while (enumeration.hasMoreElements()) {
address = (InetAddress)enumeration.nextElement();
if ((!ipv6) && ((address instanceof Inet4Address)))
return address;
if ((ipv6) && ((address instanceof Inet6Address)))
return address;
}
return null;
}

public static boolean isReachable(String ip)
{
try
{
return InetAddress.getByName(ip).isReachable(3000); } catch (Exception e) {
}
return false;
}

}

java 获取本机ip及mac地址的更多相关文章

  1. JAVA获取本机IP和Mac地址

       在项目中,时常需要获取本机的Ip或是Mac地址,进行身份和权限验证,本文就是通过java代码获取ip和Mac. package com.svse.query;import java.net.In ...

  2. 获取本机IP、mac地址、计算机名

    python获取本机IP.mac地址.计算机名 在python中获取ip地址和在php中有很大不同,我们先来看一下python 获得本机MAC地址: >>> import uuid ...

  3. python获取本机IP、mac地址、计算机名

    在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...

  4. Linux 获取本机IP、MAC地址用法大全

    getifaddrs()和struct ifaddrs的使用,获取本机IP ifaddrs结构体定义如下: struct ifaddrs { struct ifaddrs *ifa_next; /* ...

  5. QT获取本机IP和Mac地址

    #include <QNetworkInterface> #include <QList> void MainWindow::getIPPath() { QString str ...

  6. ioctrl 获取本机IP及MAC地址

    通过使用ioctl可以获得本机的一些信息,这里记录获得interface IP及MAC的过程. 1:ioctl 函数的作用是什么 man ioctl: DESCRIPTION The ioctl() ...

  7. java获取本机IP地址

    转载自:http://blog.csdn.net/thunder09/article/details/5360251 在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一 ...

  8. 详谈再论JAVA获取本机IP地址

    首先,你如果搜索“JAVA获取本机IP地址”,基本上搜到的资料全是无用的.比如这篇:http://www.cnblogs.com/zrui-xyu/p/5039551.html实际上的代码在复杂环境下 ...

  9. c#中如何获取本机用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存

    我们在利用C#开发桌面程序(Winform)程序的时候, 经常需要获取一些跟系统相关的信息, 以下这些代码获取能有些用处. c#中如何获取本机用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统 ...

随机推荐

  1. aix创建lv 在lv上创建文件系统

    创建LV命令: mklv -y softlv -t jfs2 rootvg 15G 创建文件系统命令: crfs -v jfs2 -d/dev/softlv -m /soft -A yes 把soft ...

  2. 怎样学习使用libiconv库

    怎样学习使用libiconv库 - My Study My Study About My Learn or Study etc. 怎样学习使用libiconv库 By Cnangel on Febru ...

  3. Win7下unetbootin-windows-585工具制作Ubuntu12.04 U盘启动盘

    1.下载unetbootin-windows-585工具,网址如下: unetbootin-windows-585 2.unetbootin-windows-585制作U盘启动盘 准备好1个4G的U盘 ...

  4. localStorage点击次数存储

    <!DOCTYPE html><html><head><meta charset="utf-8"> <title>菜鸟教 ...

  5. wx.ToolBar

    wx.ToolBar A toolbar is a bar of buttons and/or other controls usually placed below the menu bar in ...

  6. android 网络状态判断【转】

    import java.net.InetAddress; import android.app.Activity;import android.content.Context;import andro ...

  7. 在MyEclipse环境下写Struts,删除项目不干净的问题的解决

    这个头疼的问题弄了好几个小时,终于弄好了.方法如下:1.建立一个新的项目,确认自己已经部署好Struts2的环境(网上有好多教程).运行Tomcat还是会有之前的项目的错误,接下来进行第二步2.将To ...

  8. 扩展VirtualBox虚拟机磁盘容量

    1. 在cmd命令行下进入VirtualBox的安装目录,使用“VBoxManage list hdds”命令,找到需要修改磁盘容量的虚拟机的img路径或UUID: VirtualBox安装目录> ...

  9. 在网页中使用javascript提供反馈信息

    一,使用document.write() 二,使用window方法,prompt(),alert()和confirm()   <html lang="en"> < ...

  10. 服务端NETTY 客户端非NETTY处理粘包和拆包的问题

    之前为了调式和方便一直没有处理粘包的问题,今天专门花了时间来搞NETTY的粘包处理,要知道在高并发下,不处理粘包是不可能的,数据流的混乱会造成业务的崩溃什么的我就不说了.所以这个问题 在我心里一直是个 ...