本文整理汇总了Java中java.net.NetworkInterface.getByInetAddress方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.getByInetAddress方法的具体用法?Java NetworkInterface.getByInetAddress怎么用?Java NetworkInterface.getByInetAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface.getByInetAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: JdpBroadcaster
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Create a new broadcaster
*
* @param address - multicast group address
* @param srcAddress - address of interface we should use to broadcast.
* @param port - udp port to use
* @param ttl - packet ttl
* @throws IOException
*/
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
throws IOException, JdpException {
this.addr = address;
this.port = port;
ProtocolFamily family = (address instanceof Inet6Address)
? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
channel = DatagramChannel.open(family);
channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
// with srcAddress equal to null, this constructor do exactly the same as
// if srcAddress is not passed
if (srcAddress != null) {
// User requests particular interface to bind to
NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
try {
channel.bind(new InetSocketAddress(srcAddress, 0));
} catch (UnsupportedAddressTypeException ex) {
throw new JdpException("Unable to bind to source address");
}
channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
}
}
示例2: getDatacenterId
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* <p>
* 数据标识id部分
* </p>
*/
protected static long getDatacenterId(long maxDatacenterId) {
long id = 0L;
try {
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network == null) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
id = ((0x000000FF & (long) mac[mac.length - 1]) | (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
id = id % (maxDatacenterId + 1);
}
} catch (Exception e) {
logger.warn(" getDatacenterId: " + e.getMessage());
}
return id;
}
示例3: isLocalAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Given an InetAddress, checks to see if the address is a local address, by comparing the address
* with all the interfaces on the node.
* @param addr address to check if it is local node's address
* @return true if the address corresponds to the local node
*/
public static boolean isLocalAddress(InetAddress addr) {
// Check if the address is any local or loop back
boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();
// Check if the address is defined on any interface
if (!local) {
try {
local = NetworkInterface.getByInetAddress(addr) != null;
} catch (SocketException e) {
local = false;
}
}
return local;
}
示例4: getMSS
import java.net.NetworkInterface; //导入方法依赖的package包/类
private int getMSS(SocketChannel sc) {
int retMSS = Config.NETWORK_BUFF_LEN_SIZE;
try {
final InetAddress ia = sc.socket().getLocalAddress();
NetworkInterface ni = NetworkInterface.getByInetAddress(ia);
int mss = ni.getMTU() - 40;
if (mss > 1000) {
retMSS = mss;
}
} catch (Throwable t) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Cannot determine MTU for socket channel: " + sc);
}
}
return retMSS;
}
示例5: convert
import java.net.NetworkInterface; //导入方法依赖的package包/类
public NetworkInterface convert(String s) throws Exception {
try {
InetAddress addr = new InetAddressConverter().convert(s);
return NetworkInterface.getByInetAddress(addr);
} catch (Exception ex) {
try { return NetworkInterface.getByName(s);
} catch (Exception ex2) {
throw new TypeConversionException("'" + s + "' is not an InetAddress or NetworkInterface name");
}
}
}
示例6: getHardwareEntropy
import java.net.NetworkInterface; //导入方法依赖的package包/类
private final byte[] getHardwareEntropy() {
byte[] mac;
try {
InetAddress entropyEncoded = InetAddress.getLocalHost();
if(entropyEncoded == null) {
mac = DEF;
} else {
NetworkInterface digest = NetworkInterface.getByInetAddress(entropyEncoded);
if(digest != null) {
mac = digest.getHardwareAddress();
if(mac == null) {
mac = DEF;
}
} else {
mac = DEF;
}
}
} catch (UnknownHostException | SocketException var5) {
mac = DEF;
}
byte[] entropyEncoded1 = null;
try {
MessageDigest digest1 = MessageDigest.getInstance("SHA-512");
digest1.reset();
entropyEncoded1 = digest1.digest(mac);
} catch (NoSuchAlgorithmException var4) {
;
}
return entropyEncoded1;
}
示例7: JdpBroadcaster
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Create a new broadcaster
*
* @param address - multicast group address
* @param srcAddress - address of interface we should use to broadcast.
* @param port - udp port to use
* @param ttl - packet ttl
* @throws IOException
*/
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
throws IOException, JdpException {
this.addr = address;
this.port = port;
ProtocolFamily family = (address instanceof Inet6Address)
? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
channel = DatagramChannel.open(family);
channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);
// with srcAddress equal to null, this constructor do exactly the same as
// if srcAddress is not passed
if (srcAddress != null) {
// User requests particular interface to bind to
NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
if (interf == null) {
throw new JdpException("Unable to get network interface for " + srcAddress.toString());
}
if (!interf.isUp()) {
throw new JdpException(interf.getName() + " is not up.");
}
if (!interf.supportsMulticast()) {
throw new JdpException(interf.getName() + " does not support multicast.");
}
try {
channel.bind(new InetSocketAddress(srcAddress, 0));
} catch (UnsupportedAddressTypeException ex) {
throw new JdpException("Unable to bind to source address");
}
channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
}
}
示例8: isThisMyIpAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Determine if the specified address is local to the machine we're running on.
*
* @param addr Internet protocol address object
* @return 'true' if the specified address is local; otherwise 'false'
*/
public static boolean isThisMyIpAddress(InetAddress addr) {
// Check if the address is a valid special local or loop back
if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) {
return true;
}
// Check if the address is defined on any interface
try {
return NetworkInterface.getByInetAddress(addr) != null;
} catch (SocketException e) {
LOGGER.warn("Attempt to associate IP address with adapter triggered I/O exception: {}", e.getMessage());
return false;
}
}
示例9: getLocalInetAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Checks if {@code host} is a local host name and return {@link InetAddress}
* corresponding to that address.
*
* @param host the specified host
* @return a valid local {@link InetAddress} or null
* @throws SocketException if an I/O error occurs
*/
public static InetAddress getLocalInetAddress(String host)
throws SocketException {
if (host == null) {
return null;
}
InetAddress addr = null;
try {
addr = SecurityUtil.getByName(host);
if (NetworkInterface.getByInetAddress(addr) == null) {
addr = null; // Not a local address
}
} catch (UnknownHostException ignore) { }
return addr;
}
示例10: isLocalAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Given an InetAddress, checks to see if the address is a local address, by
* comparing the address with all the interfaces on the node.
* @param addr address to check if it is local node's address
* @return true if the address corresponds to the local node
*/
public static boolean isLocalAddress(InetAddress addr) {
// Check if the address is any local or loop back
boolean local = addr.isAnyLocalAddress() || addr.isLoopbackAddress();
// Check if the address is defined on any interface
if (!local) {
try {
local = NetworkInterface.getByInetAddress(addr) != null;
} catch (SocketException e) {
local = false;
}
}
return local;
}
示例11: isValidLocalAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Tests whether a given address is a valid local address.
*
* @param address an address
* @return true if the address is a valid local address
*/
public static boolean isValidLocalAddress(final InetAddress address) {
boolean local = address.isAnyLocalAddress() || address.isLoopbackAddress();
if (!local) {
try {
local = NetworkInterface.getByInetAddress(address) != null;
} catch (final SocketException e) {
local = false;
}
}
return local;
}
示例12: getMacAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
@Deprecated
public static String getMacAddress() throws IOException {
String macaddr = "";
InetAddress addr = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(addr);
byte[] maca = ni.getHardwareAddress();
for (int k = 0; k < maca.length; k++) {
macaddr += String.format("%02X%s", maca[k], (k < maca.length - 1) ? ":" : "");
}
return macaddr;
}
示例13: main
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static void main(String[] args) {
try {
InetAddress addr = InetAddress.getByName("localhost");
if (!addr.isReachable(10000))
throw new RuntimeException("Localhost should always be reachable");
NetworkInterface inf = NetworkInterface.getByInetAddress(addr);
if (inf != null) {
if (!addr.isReachable(inf, 20, 10000))
throw new RuntimeException("Localhost should always be reachable");
}
} catch (IOException e) {
throw new RuntimeException("Unexpected exception:" + e);
}
}
示例14: main
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
int checkFailureCount = 0;
try {
Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface ni = en.nextElement();
Enumeration<InetAddress> addrs = ni.getInetAddresses();
System.out.println("############ Checking network interface + "
+ ni + " #############");
while (addrs.hasMoreElements()) {
InetAddress addr = addrs.nextElement();
System.out.println("************ Checking address + "
+ addr + " *************");
NetworkInterface addrNetIf = NetworkInterface
.getByInetAddress(addr);
if (addrNetIf.equals(ni)) {
System.out.println("Retreived net if " + addrNetIf
+ " equal to owning net if " + ni);
} else {
System.out.println("Retreived net if " + addrNetIf
+ "NOT equal to owning net if " + ni
+ "***********");
checkFailureCount++;
}
}
}
} catch (Exception ex) {
}
if (checkFailureCount > 0) {
throw new RuntimeException(
"NetworkInterface lookup by address didn't match owner network interface");
}
}
示例15: getInterfaceMask
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Returns interface MAC by index.
*
* @param interfaceIndex interface index
* @return interface IP by index
*/
private static String getInterfaceMask(int interfaceIndex) {
String subnetMask = null;
try {
Ip4Address ipAddress = getInterfaceIp(interfaceIndex);
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(
InetAddress.getByName(ipAddress.toString()));
Enumeration ipAddresses = networkInterface.getInetAddresses();
int index = 0;
while (ipAddresses.hasMoreElements()) {
InetAddress address = (InetAddress) ipAddresses.nextElement();
if (!address.isLinkLocalAddress()) {
break;
}
index++;
}
int prfLen = networkInterface.getInterfaceAddresses().get(index).getNetworkPrefixLength();
int shft = 0xffffffff << (32 - prfLen);
int oct1 = ((byte) ((shft & 0xff000000) >> 24)) & 0xff;
int oct2 = ((byte) ((shft & 0x00ff0000) >> 16)) & 0xff;
int oct3 = ((byte) ((shft & 0x0000ff00) >> 8)) & 0xff;
int oct4 = ((byte) (shft & 0x000000ff)) & 0xff;
subnetMask = oct1 + "." + oct2 + "." + oct3 + "." + oct4;
} catch (Exception e) {
log.debug("Error while getting Interface network mask by index");
return subnetMask;
}
return subnetMask;
}