當前位置: 首頁>>代碼示例>>Java>>正文


Java NetworkInterface.isLoopback方法代碼示例

本文整理匯總了Java中java.net.NetworkInterface.isLoopback方法的典型用法代碼示例。如果您正苦於以下問題:Java NetworkInterface.isLoopback方法的具體用法?Java NetworkInterface.isLoopback怎麽用?Java NetworkInterface.isLoopback使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.NetworkInterface的用法示例。


在下文中一共展示了NetworkInterface.isLoopback方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getMacAddress

import java.net.NetworkInterface; //導入方法依賴的package包/類
private static byte[] getMacAddress() throws SocketException {
    Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
    if (en != null) {
        while (en.hasMoreElements()) {
            NetworkInterface nint = en.nextElement();
            if (!nint.isLoopback()) {
                // Pick the first valid non loopback address we find
                byte[] address = nint.getHardwareAddress();
                if (isValidAddress(address)) {
                    return address;
                }
            }
        }
    }
    // Could not find a mac address
    return null;
}
 
開發者ID:baidu,項目名稱:Elasticsearch,代碼行數:18,代碼來源:MacAddressProvider.java

示例2: getLocalInetAddress

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * Retrieve the first validated local ip address(the Public and LAN ip addresses are validated).
 *
 * @return the local address
 * @throws SocketException the socket exception
 */
public static InetAddress getLocalInetAddress() throws SocketException {
    // enumerates all network interfaces
    Enumeration<NetworkInterface> enu = NetworkInterface.getNetworkInterfaces();

    while (enu.hasMoreElements()) {
        NetworkInterface ni = enu.nextElement();
        if (ni.isLoopback()) {
            continue;
        }

        Enumeration<InetAddress> addressEnumeration = ni.getInetAddresses();
        while (addressEnumeration.hasMoreElements()) {
            InetAddress address = addressEnumeration.nextElement();

            // ignores all invalidated addresses
            if (address.isLinkLocalAddress() || address.isLoopbackAddress() || address.isAnyLocalAddress()) {
                continue;
            }

            return address;
        }
    }

    throw new RuntimeException("No validated local address!");
}
 
開發者ID:baidu,項目名稱:uid-generator,代碼行數:32,代碼來源:NetUtils.java

示例3: formatFlags

import java.net.NetworkInterface; //導入方法依賴的package包/類
/** format network interface flags */
private static String formatFlags(NetworkInterface nic) throws SocketException {
    StringBuilder flags = new StringBuilder();
    if (nic.isUp()) {
        flags.append("UP ");
    }
    if (nic.supportsMulticast()) {
        flags.append("MULTICAST ");
    }
    if (nic.isLoopback()) {
        flags.append("LOOPBACK ");
    }
    if (nic.isPointToPoint()) {
        flags.append("POINTOPOINT ");
    }
    if (nic.isVirtual()) {
        flags.append("VIRTUAL ");
    }
    flags.append("mtu:" + nic.getMTU());
    flags.append(" index:" + nic.getIndex());
    return flags.toString();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:23,代碼來源:IfConfig.java

示例4: getLoopbackAddresses

import java.net.NetworkInterface; //導入方法依賴的package包/類
/** Returns all interface-local scope (loopback) addresses for interfaces that are up. */
static InetAddress[] getLoopbackAddresses() throws SocketException {
    List<InetAddress> list = new ArrayList<>();
    for (NetworkInterface intf : getInterfaces()) {
        if (intf.isUp()) {
            // NOTE: some operating systems (e.g. BSD stack) assign a link local address to the loopback interface
            // while technically not a loopback address, some of these treat them as one (e.g. OS X "localhost") so we must too,
            // otherwise things just won't work out of box. So we include all addresses from loopback interfaces.
            for (InetAddress address : Collections.list(intf.getInetAddresses())) {
                if (intf.isLoopback() || address.isLoopbackAddress()) {
                    list.add(address);
                }
            }
        }
    }
    if (list.isEmpty()) {
        throw new IllegalArgumentException("No up-and-running loopback addresses found, got " + getInterfaces());
    }
    return list.toArray(new InetAddress[list.size()]);
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:21,代碼來源:NetworkUtils.java

示例5: getBroadcastAddress

import java.net.NetworkInterface; //導入方法依賴的package包/類
static List<String> getBroadcastAddress() throws BrowsingException, SocketException {
  Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();

  List<String> broadcastAddresses = new ArrayList<>();

  while (interfaces.hasMoreElements()) {
    NetworkInterface networkInterface = interfaces.nextElement();
    if (networkInterface.isLoopback()) {
      continue;
    }

    for (InterfaceAddress interfaceAddress :
            networkInterface.getInterfaceAddresses()) {
      InetAddress broadcast = interfaceAddress.getBroadcast();

      if (broadcast != null) {
        broadcastAddresses.add(broadcast.toString().substring(1));
      }
    }
  }

  return broadcastAddresses;
}
 
開發者ID:google,項目名稱:samba-documents-provider,代碼行數:24,代碼來源:BroadcastUtils.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: getActiveNetworkInterfaceIP

import java.net.NetworkInterface; //導入方法依賴的package包/類
private String getActiveNetworkInterfaceIP() throws SocketException {
    Enumeration<NetworkInterface>
            networkInterface = NetworkInterface.getNetworkInterfaces();
    String ipv6AddrStr = null;
    while (networkInterface.hasMoreElements()) {
        NetworkInterface nic = networkInterface.nextElement();
        if (nic.isUp() && !nic.isLoopback()) {
            Enumeration<InetAddress> inet = nic.getInetAddresses();
            while (inet.hasMoreElements()) {
                InetAddress addr = inet.nextElement();
                if (addr instanceof Inet4Address
                        && !addr.isLinkLocalAddress()) {
                    return addr.getHostAddress();
                }else if (addr instanceof Inet6Address
                        && !addr.isLinkLocalAddress()) {
                    /*
                    We save last seen IPv6 address which we will return
                    if we do not find any interface with IPv4 address.
                    */
                    ipv6AddrStr = addr.getHostAddress();
                }
            }
        }
    }
    return ipv6AddrStr;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:27,代碼來源:JMXServiceURL.java

示例8: findPublicAddresses

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * Find all ipv4 and ipv6 addresses on the local host that can be used for sending packets to the internet
 * @return
 * @throws IOException
 */
public List<InetAddress> findPublicAddresses() throws IOException {
	List<InetAddress> result = new ArrayList<InetAddress>();
	Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
	while (networkInterfaces.hasMoreElements()) {
		NetworkInterface networkInterface = networkInterfaces.nextElement();
		if (!networkInterface.isLoopback() && networkInterface.isUp()) {
			Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
			while (inetAddresses.hasMoreElements()) {
				InetAddress inetAddress = inetAddresses.nextElement();
				if (!inetAddress.isLinkLocalAddress()) {
					result.add(inetAddress);
				}
			}
		}
	}
	return result;
}
 
開發者ID:xtianus,項目名稱:yadaframework,代碼行數:23,代碼來源:YadaNetworkUtil.java

示例9: getIp

import java.net.NetworkInterface; //導入方法依賴的package包/類
public String getIp() {
    String id = "-1";
    try {
        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        while (allNetInterfaces.hasMoreElements()) {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
            if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                continue;
            } else {
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        id = ip.getHostAddress();
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("Cannot get Id, something wrong with getting Ipv4 address");
    }
    return id;
}
 
開發者ID:xiongbeer,項目名稱:Cobweb,代碼行數:25,代碼來源:IdProvider.java

示例10: getLocalNetWorkIp

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * 獲取本機的網絡IP
 */
public static String getLocalNetWorkIp() {
    if (localIp != null) {
        return localIp;
    }
    try {
        Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        while (netInterfaces.hasMoreElements()) {// 遍曆所有的網卡
            NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
            if (ni.isLoopback() || ni.isVirtual()) {// 如果是回環和虛擬網絡地址的話繼續
                continue;
            }
            Enumeration<InetAddress> addresss = ni.getInetAddresses();
            while (addresss.hasMoreElements()) {
                InetAddress address = addresss.nextElement();
                if (address instanceof Inet4Address) {// 這裏暫時隻獲取ipv4地址
                    ip = address;
                    break;
                }
            }
            if (ip != null) {
                break;
            }
        }
        if (ip != null) {
            localIp = ip.getHostAddress();
        } else {
            localIp = "127.0.0.1";
        }
    } catch (Exception e) {
        localIp = "127.0.0.1";
    }
    return localIp;
}
 
開發者ID:wendal,項目名稱:alipay-sdk,代碼行數:38,代碼來源:AlipayUtils.java

示例11: doGetIpv4AddressFromNetworkInterface

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * docker環境中,有時無法通過InetAddress.getLocalHost()獲取 ,會報unknown host Exception, system error
 * 此時,通過遍曆網卡接口的方式規避,出來的數據不一定對
 */
private static void doGetIpv4AddressFromNetworkInterface() throws SocketException {
  Enumeration<NetworkInterface> iterNetwork = NetworkInterface.getNetworkInterfaces();

  while (iterNetwork.hasMoreElements()) {
    NetworkInterface network = iterNetwork.nextElement();

    if (!network.isUp() || network.isLoopback() || network.isVirtual()) {
      continue;
    }

    Enumeration<InetAddress> iterAddress = network.getInetAddresses();
    while (iterAddress.hasMoreElements()) {
      InetAddress address = iterAddress.nextElement();

      if (address.isAnyLocalAddress() || address.isLoopbackAddress() || address.isMulticastAddress()
          || Inet6Address.class.isInstance(address)) {
        continue;
      }

      if (Inet4Address.class.isInstance(address)) {
        LOGGER.info(
            "add network interface name:" + network.getName() + ",host address:" + address.getHostAddress());
        allInterfaceAddresses.put(network.getName(), address);
      }
    }
  }
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:32,代碼來源:NetUtils.java

示例12: 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

示例13: getLocalNetWorkIp

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * 獲取本機的網絡IP
 */
public static String getLocalNetWorkIp() {
	if (localIp != null) {
		return localIp;
	}
	try {
		Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
		InetAddress ip = null;
		while (netInterfaces.hasMoreElements()) {// 遍曆所有的網卡
			NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
			if (ni.isLoopback() || ni.isVirtual()) {// 如果是回環和虛擬網絡地址的話繼續
				continue;
			}
			Enumeration<InetAddress> addresss = ni.getInetAddresses();
			while (addresss.hasMoreElements()) {
				InetAddress address = addresss.nextElement();
				if (address instanceof Inet4Address) {// 這裏暫時隻獲取ipv4地址
					ip = address;
					break;
				}
			}
			if (ip != null) {
				break;
			}
		}
		if (ip != null) {
			localIp = ip.getHostAddress();
		} else {
			localIp = "127.0.0.1";
		}
	} catch (Exception e) {
		localIp = "127.0.0.1";
	}
	return localIp;
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:38,代碼來源:AlipayUtils.java

示例14: getInetAddresses

import java.net.NetworkInterface; //導入方法依賴的package包/類
public static final ArrayList<InetAddress> getInetAddresses(InetAddressFilter inetAddressFilter) {
    try {
        final ArrayList<InetAddress> inetAddresses = new ArrayList<>();
        final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            final NetworkInterface networkInterface = networkInterfaces.nextElement();
            if (networkInterface == null || networkInterface.isLoopback() || !networkInterface.isUp() || networkInterface.isVirtual() || networkInterface.isPointToPoint()) {
                continue;
            }
            final Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                final InetAddress inetAddress = addresses.nextElement();
                if (inetAddress == null) {
                    continue;
                }
                if (inetAddressFilter.filter(inetAddress)) {
                    inetAddresses.add(inetAddress);
                }
            }
        }
        return inetAddresses;
    } catch (Exception ex) {
        System.err.println("NetworkUtil: getInetAddresses error");
        ex.printStackTrace();
        return new ArrayList<>();
    }
}
 
開發者ID:Panzer1119,項目名稱:Supreme-Bot,代碼行數:28,代碼來源:NetworkUtil.java

示例15: getInetAddresses

import java.net.NetworkInterface; //導入方法依賴的package包/類
/**
 * @return a bunch of InetAddresses that are "real", ie, not virtual,
 *         loopback, multicast and are up.
 */
public static List<Pair<NetworkInterface, InetAddress>> getInetAddresses()
{
	List<Pair<NetworkInterface, InetAddress>> addrs = Lists.newArrayList();
	try
	{
		Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
		while( nis.hasMoreElements() )
		{
			NetworkInterface ni = nis.nextElement();
			if( !ni.isLoopback() && !ni.isVirtual() && ni.isUp() )
			{
				for( Enumeration<InetAddress> ias = ni.getInetAddresses(); ias.hasMoreElements(); )
				{
					InetAddress ia = ias.nextElement();
					if( !ia.isMulticastAddress() && !ia.isLoopbackAddress() )
					{
						addrs.add(new Pair<NetworkInterface, InetAddress>(ni, ia));
					}
				}
			}
		}
	}
	catch( SocketException e )
	{
		// Carry on
	}
	return addrs;
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:33,代碼來源:NetworkUtils.java


注:本文中的java.net.NetworkInterface.isLoopback方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。