本文整理汇总了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;
}
示例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!");
}
示例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();
}
示例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()]);
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
}
}
}
示例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));
}
}
}
示例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;
}
示例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<>();
}
}
示例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;
}