本文整理汇总了Java中java.net.NetworkInterface.getInetAddresses方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.getInetAddresses方法的具体用法?Java NetworkInterface.getInetAddresses怎么用?Java NetworkInterface.getInetAddresses使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface.getInetAddresses方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLocalIpAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Get local IP address
*
* @return ip address
*/
static String getLocalIpAddress() {
String ip = "127.0.0.1";
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()) {
ip = inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
}
示例2: getIpAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 获取本机IP地址
*
* @return null:没有网络连接
*/
public static String getIpAddress() {
try {
NetworkInterface nerworkInterface;
InetAddress inetAddress;
for (Enumeration<NetworkInterface> en
= NetworkInterface.getNetworkInterfaces();
en.hasMoreElements(); ) {
nerworkInterface = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr
= nerworkInterface.getInetAddresses();
enumIpAddr.hasMoreElements(); ) {
inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
return null;
} catch (SocketException ex) {
ex.printStackTrace();
return null;
}
}
示例3: getInterfaceIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Returns interface IP by index.
*
* @param interfaceIndex interface index
* @return interface IP by index
*/
private static Ip4Address getInterfaceIp(int interfaceIndex) {
Ip4Address ipAddress = null;
try {
NetworkInterface networkInterface = NetworkInterface.getByIndex(interfaceIndex);
Enumeration ipAddresses = networkInterface.getInetAddresses();
while (ipAddresses.hasMoreElements()) {
InetAddress address = (InetAddress) ipAddresses.nextElement();
if (!address.isLinkLocalAddress()) {
ipAddress = Ip4Address.valueOf(address.getAddress());
break;
}
}
} catch (Exception e) {
log.debug("Error while getting Interface IP by index");
return OspfUtil.DEFAULTIP;
}
return ipAddress;
}
示例4: getLocalMacAddressFromIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getLocalMacAddressFromIp() {
String mac_s = "";
try {
byte[] mac;
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()) {
mac = netI.getHardwareAddress();
mac_s = byte2hex(mac);
return mac_s;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return mac_s;
}
示例5: getLocalIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 根据网卡取本机配置的IP 如果是双网卡的,则取出外网IP
*
* @return
*/
public static String getLocalIp() {
String localip = null;// 返回它
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外网IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (ip.isSiteLocalAddress() && !ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) {// 内网IP
localip = ip.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return localip;
}
示例6: findLocalIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
private IpAddress findLocalIp(Collection<ControllerNode> controllerNodes) throws SocketException {
Enumeration<NetworkInterface> interfaces =
NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
IpAddress ip = IpAddress.valueOf(inetAddresses.nextElement());
if (controllerNodes.stream()
.map(ControllerNode::ip)
.anyMatch(nodeIp -> ip.equals(nodeIp))) {
return ip;
}
}
}
throw new IllegalStateException("Unable to determine local ip");
}
示例7: GetIpAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
public String GetIpAddress() {
StringBuilder ip = new StringBuilder();
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip.append("SiteLocalAddress: ").append(inetAddress.getHostAddress()).append("\n");
}
}
}
} catch (SocketException e) {
Logger.getInstance().Error(TAG, e.toString());
ip.append("Something Wrong! ").append(e.toString()).append("\n");
}
return ip.toString();
}
示例8: 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;
}
示例9: getPublicIP4
import java.net.NetworkInterface; //导入方法依赖的package包/类
static public String getPublicIP4() {
Enumeration<NetworkInterface> ifs;
try {
ifs = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
return null;
}
while (ifs.hasMoreElements()) {
NetworkInterface iface = ifs.nextElement();
Enumeration<InetAddress> iad = iface.getInetAddresses();
while (iad.hasMoreElements()) {
InetAddress localIP = iad.nextElement();
if (!localIP.isSiteLocalAddress() && !localIP.isLoopbackAddress()) {
// found an IPv4 address
if (localIP instanceof java.net.Inet4Address)
return localIP.getHostAddress();
}
}
}
return null;
}
示例10: getDevicesIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 获取设备IP
*
* @return
*/
public String getDevicesIp() {
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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
示例11: getIpv4Address
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getIpv4Address() {
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 "";
}
示例12: main
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(new File(args[0]));
try {
while (s.hasNextLine()) {
String link = s.nextLine();
NetworkInterface ni = NetworkInterface.getByName(link);
if (ni != null) {
Enumeration<InetAddress> addrs = ni.getInetAddresses();
while (addrs.hasMoreElements()) {
InetAddress addr = addrs.nextElement();
System.out.println(addr.getHostAddress());
}
}
}
} finally {
s.close();
}
}
示例13: getLocalIpAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* get local ip address
*
* @return
*/
public static String getLocalIpAddress(Context context) {
String localIp = "";
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()) {
localIp = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return localIp;
}
示例14: getIpAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getIpAddress() {
final Enumeration<NetworkInterface> nis;
try {
nis = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
return StringConstant.EMPTY;
}
NetworkInterface ni;
Enumeration<InetAddress> eia;
InetAddress ia;
String ip;
while (nis.hasMoreElements()) {
ni = nis.nextElement();
eia = ni.getInetAddresses();
while (eia.hasMoreElements()) {
ia = eia.nextElement();
if (!ia.isLoopbackAddress()) {
ip = ia.getHostAddress();
if (!TextUtils.isEmpty(ip)) {
return parseIpAddress(ip);
}
}
}
}
return StringConstant.EMPTY;
}
示例15: parseAdapterName
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Parse an adapter name string and return the matching address
*
* @param adapter String
* @return InetAddress
* @exception InvalidConfigurationException
*/
protected final InetAddress parseAdapterName(String adapter)
throws InvalidConfigurationException {
NetworkInterface ni = null;
try {
ni = NetworkInterface.getByName( adapter);
}
catch (SocketException ex) {
throw new InvalidConfigurationException( "Invalid adapter name, " + adapter);
}
if ( ni == null)
throw new InvalidConfigurationException( "Invalid network adapter name, " + adapter);
// Get the IP address for the adapter
InetAddress adapAddr = null;
Enumeration<InetAddress> addrEnum = ni.getInetAddresses();
while ( addrEnum.hasMoreElements() && adapAddr == null) {
// Get the current address
InetAddress addr = addrEnum.nextElement();
if ( IPAddress.isNumericAddress( addr.getHostAddress()))
adapAddr = addr;
}
// Check if we found the IP address to bind to
if ( adapAddr == null)
throw new InvalidConfigurationException( "Adapter " + adapter + " does not have a valid IP address");
// Return the adapter address
return adapAddr;
}