本文整理汇总了Java中java.net.NetworkInterface.isVirtual方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.isVirtual方法的具体用法?Java NetworkInterface.isVirtual怎么用?Java NetworkInterface.isVirtual使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface.isVirtual方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
示例2: 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;
}
示例3: getLocalAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 获取本机的IP
*
* @return Ip地址
*/
private static String getLocalAddress() {
try {
for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
continue;
}
Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
if (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if(address instanceof Inet4Address) {
return address.getHostAddress();
}
}
}
} catch (SocketException e) {
logger.debug("Error when getting host ip address: <{}>.", e.getMessage());
}
return "127.0.0.1";
}
示例4: 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);
}
示例5: 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);
}
}
}
}
示例6: 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;
}
示例7: 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<>();
}
}
示例8: 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;
}
示例9: getLocalInetAddresses
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static List<InetAddress> getLocalInetAddresses() throws IOException {
List<InetAddress> ret = new ArrayList<>();
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
if (networkInterfaces == null) {
return ret;
}
while (networkInterfaces.hasMoreElements()) {
NetworkInterface card = networkInterfaces.nextElement();
if (card.isLoopback() || card.isPointToPoint() || card.isVirtual() || !card.isUp()) {
continue;
}
Enumeration<InetAddress> addresses = card.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress inetAddress = addresses.nextElement();
int index = ret.size();
if (Inet4Address.class.isInstance(inetAddress)) {
ret.add(index, inetAddress);
}
}
}
return ret;
}
示例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: getLocalNetWorkIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
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;
}
示例12: readInfo
import java.net.NetworkInterface; //导入方法依赖的package包/类
private List<InetAddress> readInfo(String name) {
List<InetAddress> ipList = new ArrayList<InetAddress>();
InetAddress ip = null;
Enumeration<NetworkInterface> netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
ip = ips.nextElement();
if (!ni.isVirtual() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {
if (name != null) {
if (ni.getName().equalsIgnoreCase(name)) {
ipList.add(ip);
}
}
else {
ipList.add(ip);
}
}
}
}
}
catch (SocketException e) {
// ignore
}
return ipList;
}