当前位置: 首页>>代码示例>>Java>>正文


Java NetworkInterface.getNetworkInterfaces方法代码示例

本文整理汇总了Java中java.net.NetworkInterface.getNetworkInterfaces方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.getNetworkInterfaces方法的具体用法?Java NetworkInterface.getNetworkInterfaces怎么用?Java NetworkInterface.getNetworkInterfaces使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.NetworkInterface的用法示例。


在下文中一共展示了NetworkInterface.getNetworkInterfaces方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getIPV4

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * Get ipv4 address.
 *
 * @return
 */
@Nullable
public static String getIPV4() {
    try {
        Enumeration<NetworkInterface> net = NetworkInterface.getNetworkInterfaces();
        while (net.hasMoreElements()) {
            NetworkInterface networkInterface = net.nextElement();
            Enumeration<InetAddress> add = networkInterface.getInetAddresses();
            while (add.hasMoreElements()) {
                InetAddress a = add.nextElement();
                if (!a.isLoopbackAddress()
                        && !a.getHostAddress().contains(":")) {
                    if (Debug.debug) {
                        Log.d(TAG, "getIPV4 : " + a.getHostAddress());
                    }
                    return a.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return null;
}
 
开发者ID:xyhuangjinfu,项目名称:MiniDownloader,代码行数:29,代码来源:NetworkUtil.java

示例2: getIp

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getIp() {
    try {
        for (Enumeration<NetworkInterface> enNetI = NetworkInterface
                .getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
            NetworkInterface netI = enNetI.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = netI
                    .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return "";
}
 
开发者ID:OddCN,项目名称:screen-share-to-browser,代码行数:19,代码来源:NetUtil.java

示例3: getPublicIPs6

import java.net.NetworkInterface; //导入方法依赖的package包/类
static public List<String> getPublicIPs6() {
    List<String> ips6 = new ArrayList<String>();
    Enumeration<NetworkInterface> ifs;
    try {
        ifs = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        e.printStackTrace();
        return ips6;
    }
    while (ifs.hasMoreElements()) {
        NetworkInterface iface = ifs.nextElement();
        Enumeration<InetAddress> iad = iface.getInetAddresses();
        while (iad.hasMoreElements()) {
            InetAddress localIP = iad.nextElement();
            if (!localIP.isSiteLocalAddress() && !localIP.isLinkLocalAddress() && !localIP.isLoopbackAddress()) {
                if (localIP instanceof java.net.Inet6Address)
                    ips6.add(localIP.getHostAddress());
            }
        }
    }
    return ips6;
}
 
开发者ID:fast-data-transfer,项目名称:fdt,代码行数:23,代码来源:LocalHost.java

示例4: start

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static void start() throws Exception
{
    database = new Db();

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    NetworkInterface nInterface = interfaces.nextElement();
    Enumeration<InetAddress> adresses = nInterface != null ? nInterface.getInetAddresses() : null;
    InetAddress iAdress = null;
    while (adresses != null && adresses.hasMoreElements())
        iAdress = adresses.nextElement();
    String adress = iAdress != null ? iAdress.getHostAddress() : "";
    server = HttpServer.create(new InetSocketAddress(adress,
            8001), 0);

    server.createContext("/test", new MyHandler());
    server.setExecutor(null); // creates a default executor
    server.start();
}
 
开发者ID:Jugendhackt,项目名称:OpenVertretung,代码行数:19,代码来源:HTTPServer.java

示例5: getLocalIpAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return "0.0.0.0";
}
 
开发者ID:Crixec,项目名称:ADBToolKitsInstaller,代码行数:17,代码来源:MainActivity.java

示例6: getIpAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
private static InetAddress getIpAddress(AddressSelectionCondition condition) throws
    SocketException {
  // Before we connect somewhere, we cannot be sure about what we'd be bound to; however,
  // we only connect when the message where client ID is, is long constructed. Thus,
  // just use whichever IP address we can find.
  Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
  while (interfaces.hasMoreElements()) {
    NetworkInterface current = interfaces.nextElement();
    if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue;
    Enumeration<InetAddress> addresses = current.getInetAddresses();
    while (addresses.hasMoreElements()) {
      InetAddress addr = addresses.nextElement();
      if (addr.isLoopbackAddress()) continue;
      if (condition.isAcceptableAddress(addr)) {
        return addr;
      }
    }
  }

  throw new SocketException("Can't get our ip address, interfaces are: " + interfaces);
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:22,代码来源:Addressing.java

示例7: getNetworkInterfaceAdresses

import java.net.NetworkInterface; //导入方法依赖的package包/类
public List<String> getNetworkInterfaceAdresses() {
    Enumeration<NetworkInterface> networkInterfaceEnumeration;
    List<String> networkInterfaceAddressesList = new ArrayList<>();
    try {
        networkInterfaceEnumeration = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaceEnumeration.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaceEnumeration.nextElement();
            Enumeration<InetAddress> inetAddressEnumeration = networkInterface.getInetAddresses();
            while (inetAddressEnumeration.hasMoreElements()) {
                InetAddress inetAddress = inetAddressEnumeration.nextElement();
                if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                    networkInterfaceAddressesList.add(
                            String.format("%5s %s",
                                    networkInterface.getDisplayName(),
                                    inetAddress.getHostAddress()));
                }
            }
        }
    } catch (SocketException e) {
        Timber.e(e);
    }
    return networkInterfaceAddressesList;
}
 
开发者ID:leinardi,项目名称:androidthings-kuman-sm9,代码行数:24,代码来源:SystemHelper.java

示例8: getGatewayIpAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static void getGatewayIpAddress(Context c) {
    // get wifi ip

    final WifiManager manager = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
    final DhcpInfo dhcp = manager.getDhcpInfo();
    final String address = Formatter.formatIpAddress(dhcp.gateway);

    StringBuilder IFCONFIG = new StringBuilder();
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
                        && inetAddress.isSiteLocalAddress()) {
                    IFCONFIG.append(inetAddress.getHostAddress().toString() + "\n");
                }

            }
        }
    } catch (SocketException ex) {
        Log.e("LOG_TAG", ex.toString());
    }
    MLog.d(TAG, "ifconfig " + IFCONFIG.toString());

    MLog.d(TAG, "hotspot address is " + address);

}
 
开发者ID:victordiaz,项目名称:phonk,代码行数:29,代码来源:NetworkUtils.java

示例9: getLocalAddress

import java.net.NetworkInterface; //导入方法依赖的package包/类
public static InetAddress getLocalAddress() {
    try {
        final InetAddress addr = InetAddress.getLocalHost();
        return addr;
    } catch (UnknownHostException e) {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            if (interfaces == null) {
                return null;
            }
            NetworkInterface intf = interfaces.nextElement();
            Enumeration<InetAddress> addresses = intf.getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress address = addresses.nextElement();
                if (address instanceof Inet4Address) {
                    return address;
                }
            }
            interfaces = NetworkInterface.getNetworkInterfaces();
            while (addresses.hasMoreElements()) {
                return addresses.nextElement();
            }
            return null;
        } catch (SocketException e1) {
            return null;
        }
    }
}
 
开发者ID:s-store,项目名称:s-store,代码行数:29,代码来源:ConnectionUtil.java

示例10: getSelfIpV4

import java.net.NetworkInterface; //导入方法依赖的package包/类
@Nullable
public static String getSelfIpV4() {
    try {
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface
                .getNetworkInterfaces();
        if (allNetInterfaces == null) {
            return null;
        }
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = allNetInterfaces.nextElement();
            if (!netInterface.getName().startsWith("eth")) {
                continue;
            }
            Enumeration<InetAddress> addresses = netInterface
                    .getInetAddresses();
            while (addresses.hasMoreElements()) {
                InetAddress ip = addresses.nextElement();
                if (ip != null && ip instanceof Inet4Address) {
                    return ip.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        logger.error("get sefl IP failed, {}", e);
    }
    return null;
}
 
开发者ID:wittyLuzhishen,项目名称:EasyPackage,代码行数:28,代码来源:Config.java

示例11: getLocalAddress0

import java.net.NetworkInterface; //导入方法依赖的package包/类
private static InetAddress getLocalAddress0() {
  InetAddress result = null;
  try {
    int lowest = Integer.MAX_VALUE;
    for (Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); nics
        .hasMoreElements();) {
      NetworkInterface ifc = nics.nextElement();
      if (ifc.isUp()) {
        logger.trace("Testing interface: " + ifc.getDisplayName());
        if (ifc.getIndex() < lowest || result == null) {
          lowest = ifc.getIndex();
        } else if (result != null) {
          continue;
        }
        for (Enumeration<InetAddress> addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) {
          InetAddress address = addrs.nextElement();
          if (address instanceof Inet4Address && !address.isLoopbackAddress()) {
            logger.trace("Found non-loopback interface: " + ifc.getDisplayName());
            result = address;
          }
        }

      }
    }
  } catch (IOException ex) {
    logger.error("Cannot get first non-loopback address", ex);
  }
  if (result != null && isValidAddress(result)) {
    return result;
  }
  try {
    return InetAddress.getLocalHost();
  } catch (UnknownHostException e) {
    logger.warn("Unable to retrieve localhost");
  }
  return null;
}
 
开发者ID:venus-boot,项目名称:saluki,代码行数:38,代码来源:NetUtils.java

示例12: getDeviceName

import java.net.NetworkInterface; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static String getDeviceName(WifiManager wifiManager) {
	if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
		Log.w(TAG, "Older device - falling back to the default device name: " + FALLBACK_DEVICE);
		return FALLBACK_DEVICE;
	}
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
		Log.w(TAG, "6.0 or later, unaccessible MAC - falling back to the default device name: " + FALLBACK_DEVICE);
		return FALLBACK_DEVICE;
	}

	String macString = wifiManager.getConnectionInfo().getMacAddress();
	if (macString == null) {
		Log.w(TAG, "MAC Address not found - Wi-Fi disabled? Falling back to the default device name: " + FALLBACK_DEVICE);
		return FALLBACK_DEVICE;
	}
	byte[] macBytes = macAddressToByteArray(macString);

	try {
		Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
		while (ifaces.hasMoreElements()) {
			NetworkInterface iface = ifaces.nextElement();

			byte[] hardwareAddress = iface.getHardwareAddress();
			if (hardwareAddress != null && Arrays.equals(macBytes, hardwareAddress)) {
				return iface.getName();
			}
		}
	} catch (IOException e) {
		Log.e(TAG, "", e);
	}

	Log.w(TAG, "None found - falling back to the default device name: " + FALLBACK_DEVICE);
	return FALLBACK_DEVICE;
}
 
开发者ID:dmtan90,项目名称:Sense-Hub-Android-Things,代码行数:36,代码来源:WifiApControl.java

示例13: execute

import java.net.NetworkInterface; //导入方法依赖的package包/类
public void execute() throws Exception {
    Enumeration<NetworkInterface> networkInterfaces;
    boolean areMacAddressesUnique = false;
    List<NetworkInterface> networkInterfaceList = new ArrayList<NetworkInterface>();
        networkInterfaces = NetworkInterface.getNetworkInterfaces();

    // build a list of NetworkInterface objects to test MAC address
    // uniqueness
    createNetworkInterfaceList(networkInterfaces, networkInterfaceList);
    areMacAddressesUnique = checkMacAddressesAreUnique(networkInterfaceList);
    if (!areMacAddressesUnique) {
        throw new RuntimeException("mac address uniqueness test failed");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:15,代码来源:UniqueMacAddressesTest.java

示例14: createMachineIdentifier

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * Creates the machine identifier from the physical MAC address.
 * 
 * @return long the machine identifier
 */
private static long createMachineIdentifier() {
    byte[] mac = null;
    try {
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface ni = e.nextElement();
            if (!ni.isLoopback()) {
                mac = ni.getHardwareAddress();
            }
            // ?? mac[1] != (byte) 0xff it is from http://johannburkard.de/software/uuid/
            if (mac != null && mac.length == 6 && mac[1] != (byte) 0xff) {
                break;
            } else {
                continue;
            }
        }
    } catch (Throwable t) {
        throw new RuntimeException("Could not get MAC address", t);
    }
    if (mac != null && mac.length == 6 && mac[1] != (byte) 0xff) {
        return bytes2long(mac);
    } else {
        if (null == mac) {
            throw new RuntimeException("Could not get MAC address!");
        } else {
            throw new RuntimeException("MAC address is not correct:" + toHexString(mac));
        }
    }

}
 
开发者ID:fintx,项目名称:fintx-common,代码行数:36,代码来源:UniqueId.java

示例15: getInternetIp

import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
 * 获得外网IP
 * @return 外网IP
 */
public static String getInternetIp(){
	if( true )
		return getIntranetIp();
	try{
		Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
		InetAddress ip = null;
		Enumeration<InetAddress> addrs;
		while (networks.hasMoreElements())
		{
			addrs = networks.nextElement().getInetAddresses();
			while (addrs.hasMoreElements())
			{
				ip = addrs.nextElement();
				if (ip != null
						&& ip instanceof Inet4Address
						&& ip.isSiteLocalAddress()
						&& !ip.getHostAddress().equals(getIntranetIp()))
				{
					return ip.getHostAddress();
				}
			}
		}

		// 如果没有外网IP,就返回内网IP
		return getIntranetIp();
	} catch(Exception e){
		throw new RuntimeException(e);
	}
}
 
开发者ID:weizhenyi,项目名称:leaf-snowflake,代码行数:34,代码来源:Utils.java


注:本文中的java.net.NetworkInterface.getNetworkInterfaces方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。