本文整理匯總了Java中java.net.InterfaceAddress.getAddress方法的典型用法代碼示例。如果您正苦於以下問題:Java InterfaceAddress.getAddress方法的具體用法?Java InterfaceAddress.getAddress怎麽用?Java InterfaceAddress.getAddress使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.net.InterfaceAddress
的用法示例。
在下文中一共展示了InterfaceAddress.getAddress方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import java.net.InterfaceAddress; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
示例2: getLocalCidrs
import java.net.InterfaceAddress; //導入方法依賴的package包/類
public static String[] getLocalCidrs() {
final String defaultHostIp = getDefaultHostIp();
final List<String> cidrList = new ArrayList<>();
try {
for (final NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
for (final InterfaceAddress address : ifc.getInterfaceAddresses()) {
final InetAddress addr = address.getAddress();
final int prefixLength = address.getNetworkPrefixLength();
if (prefixLength < MAX_CIDR && prefixLength > 0) {
final String ip = addr.getHostAddress();
if (ip.equalsIgnoreCase(defaultHostIp)) {
cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
}
}
}
}
}
} catch (final SocketException e) {
s_logger.warn("UnknownHostException in getLocalCidrs().", e);
}
return cidrList.toArray(new String[0]);
}
示例3: getPrivateInetInetAddress
import java.net.InterfaceAddress; //導入方法依賴的package包/類
public static final List<InetAddress> getPrivateInetInetAddress() throws SocketException {
List<InetAddress> inetAddresses = new LinkedList<>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
NetworkInterface netInterface = e.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
}
for (InterfaceAddress address : netInterface.getInterfaceAddresses()) {
InetAddress inetAddress = address.getAddress();
if (inetAddress.isSiteLocalAddress() && !inetAddress.isLoopbackAddress()
&& (inetAddress instanceof Inet4Address)) {
inetAddresses.add(inetAddress);
}
}
}
return inetAddresses;
}
示例4: getPublicInetInetAddress
import java.net.InterfaceAddress; //導入方法依賴的package包/類
public static final List<InetAddress> getPublicInetInetAddress() throws SocketException {
List<InetAddress> inetAddresses = new LinkedList<>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
NetworkInterface netInterface = e.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
}
for (InterfaceAddress address : netInterface.getInterfaceAddresses()) {
InetAddress inetAddress = address.getAddress();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& !inetAddress.isSiteLocalAddress() && (inetAddress instanceof Inet4Address)) {
inetAddresses.add(inetAddress);
}
}
}
return inetAddresses;
}
示例5: getDataNodeAddress
import java.net.InterfaceAddress; //導入方法依賴的package包/類
public static InetSocketAddress getDataNodeAddress() throws IOException {
String ifname = TcpStorageConstants.STORAGE_TCP_INTERFACE;
int port = TcpStorageConstants.STORAGE_TCP_PORT;
NetworkInterface netif = NetworkInterface.getByName(ifname);
if (netif == null){
return null;
}
List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
InetAddress addr = null;
for (InterfaceAddress address: addresses){
if (address.getBroadcast() != null){
InetAddress _addr = address.getAddress();
addr = _addr;
}
}
InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
return inetAddr;
}
示例6: getDataNodeAddress
import java.net.InterfaceAddress; //導入方法依賴的package包/類
public static InetSocketAddress getDataNodeAddress() throws IOException {
String ifname = RdmaConstants.STORAGE_RDMA_INTERFACE;
int port = RdmaConstants.STORAGE_RDMA_PORT;
NetworkInterface netif = NetworkInterface.getByName(ifname);
if (netif == null){
return null;
}
List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
InetAddress addr = null;
for (InterfaceAddress address: addresses){
// LOG.info("address* " + address.toString() + ", _addr " + _addr.toString() + ", isSiteLocal " + _addr.isSiteLocalAddress() + ", tmp " + tmp + ", size " + tmp.length + ", broadcast " + address.getBroadcast());
if (address.getBroadcast() != null){
InetAddress _addr = address.getAddress();
addr = _addr;
}
}
InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
return inetAddr;
}
示例7: firstLocalNonLoopbackIpv4Address
import java.net.InterfaceAddress; //導入方法依賴的package包/類
/**
* @deprecated This method is no longer used by LittleProxy and may be removed in a future release.
*/
@Deprecated
public static InetAddress firstLocalNonLoopbackIpv4Address() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces
.nextElement();
if (networkInterface.isUp()) {
for (InterfaceAddress ifAddress : networkInterface
.getInterfaceAddresses()) {
if (ifAddress.getNetworkPrefixLength() > 0
&& ifAddress.getNetworkPrefixLength() <= 32
&& !ifAddress.getAddress().isLoopbackAddress()) {
return ifAddress.getAddress();
}
}
}
}
return null;
} catch (SocketException se) {
return null;
}
}
示例8: getAllServerAddress
import java.net.InterfaceAddress; //導入方法依賴的package包/類
protected static List<InetAddress> getAllServerAddress() {
List<InetAddress> result = new LinkedList<>();
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces == null) {
return null;
}
while (interfaces.hasMoreElements()) {
NetworkInterface current = interfaces.nextElement();
List<InterfaceAddress> addresses = current.getInterfaceAddresses();
if (addresses.size() == 0) {
continue;
}
for (InterfaceAddress interfaceAddress : addresses) {
InetAddress address = interfaceAddress.getAddress();
result.add(address);
}
}
} catch (SocketException e) {
}
return result;
}
示例9: getLocalAddress
import java.net.InterfaceAddress; //導入方法依賴的package包/類
public final InetAddress getLocalAddress() throws SocketException {
for (final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
.hasMoreElements();) {
final NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback()) {
continue;
}
for (final InterfaceAddress interfaceAddr : networkInterface.getInterfaceAddresses()) {
final InetAddress inetAddr = interfaceAddr.getAddress();
if (!(inetAddr instanceof Inet4Address)) {
continue;
}
return inetAddr;
}
}
return null;
}
示例10: firstLocalNonLoopbackIpv4Address
import java.net.InterfaceAddress; //導入方法依賴的package包/類
/**
* Go through our network interfaces and find the first bound address for an
* up interface that's in the IPv4 address space and is not the loopback
* address.
*
* @return
*/
public static InetAddress firstLocalNonLoopbackIpv4Address() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces
.nextElement();
if (networkInterface.isUp()) {
for (InterfaceAddress ifAddress : networkInterface
.getInterfaceAddresses()) {
if (ifAddress.getNetworkPrefixLength() > 0
&& ifAddress.getNetworkPrefixLength() <= 32
&& !ifAddress.getAddress().isLoopbackAddress()) {
return ifAddress.getAddress();
}
}
}
}
return null;
} catch (SocketException se) {
return null;
}
}
示例11: getIP
import java.net.InterfaceAddress; //導入方法依賴的package包/類
/**
* Get an IP.
*
* @return the first IP address available.
* @throws SocketException on network error.
*/
public static InetAddress getIP() throws SocketException {
if (ip != null) {
return ip;
}
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback()/*not loopback*/ && ni.isUp()/*it works now*/) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 for ipv4, 16 for ipv6
ip = ia.getAddress();
break;
}
}
}
}
return ip;
}
示例12: getIPs
import java.net.InterfaceAddress; //導入方法依賴的package包/類
/**
* Get all IPs.
*
* @return all IP addresses available.
* @throws SocketException on network error.
*/
public static InetAddress[] getIPs() throws SocketException {
if (ips != null) {
return ips;
}
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
List<InetAddress> ret = new ArrayList<>();
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback()/*not loopback*/ && ni.isUp()/*it works now*/) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 for ipv4, 16 for ipv6
ip = ia.getAddress();
ret.add(ip);
// return _ip;
}
}
}
}
ips = ret.toArray(new InetAddress[]{});
return ips;
}
示例13: getFirstActiveIPv4Interface
import java.net.InterfaceAddress; //導入方法依賴的package包/類
/**
* Gets the first active i pv4 interface.
*
* @return the first active i pv4 interface
* @throws SocketException the socket exception
*/
private NetworkInterface getFirstActiveIPv4Interface() throws SocketException {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface iface = networkInterfaces.nextElement();
if (iface.isUp() && !iface.isLoopback()) {
for (InterfaceAddress ifaceAddr : iface.getInterfaceAddresses()) {
if (ifaceAddr.getAddress() instanceof Inet4Address) {
return iface;
}
}
}
}
logger.debug("Unable to retrieve active network interface.");
return null;
}
示例14: testLoopback
import java.net.InterfaceAddress; //導入方法依賴的package包/類
public void testLoopback() throws Exception {
// We know lo shouldn't have a hardware address.
NetworkInterface lo = NetworkInterface.getByName("lo0");
assertNull(lo.getHardwareAddress());
// But eth0, if it exists, should...
NetworkInterface eth0 = NetworkInterface.getByName("eth0");
if (eth0 != null) {
assertEquals(6, eth0.getHardwareAddress().length);
for (InterfaceAddress ia : eth0.getInterfaceAddresses()) {
if (ia.getAddress() instanceof Inet4Address) {
assertNotNull(ia.getBroadcast());
}
}
}
}
示例15: getIPs
import java.net.InterfaceAddress; //導入方法依賴的package包/類
/**
* Get all IPs.
*
* @return
* @throws SocketException
*/
public static InetAddress[] getIPs() throws SocketException {
if (_ips != null) {
return _ips;
}
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
NetworkInterface ni;
ArrayList<InetAddress> ret = new ArrayList<InetAddress>();
while (nis.hasMoreElements()) {
ni = nis.nextElement();
if (!ni.isLoopback()/*not loopback*/ && ni.isUp()/*it works now*/) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
//filter for ipv4/ipv6
if (ia.getAddress().getAddress().length == 4) {
//4 for ipv4, 16 for ipv6
_ip = ia.getAddress();
ret.add(_ip);
// return _ip;
}
}
}
}
_ips = ret.toArray(new InetAddress[]{});
return _ips;
}