本文整理汇总了Java中java.net.NetworkInterface.getName方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.getName方法的具体用法?Java NetworkInterface.getName怎么用?Java NetworkInterface.getName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface.getName方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findLoopbackInterface
import java.net.NetworkInterface; //导入方法依赖的package包/类
private NetworkInterface findLoopbackInterface(InetAddress address) throws SocketException, HekateException {
NetworkInterface lo = null;
for (NetworkInterface nif : AddressUtils.activeNetworks()) {
if (nif.isUp() && nif.isLoopback()) {
for (InetAddress nifAddress : Collections.list(nif.getInetAddresses())) {
if (!nifAddress.isLinkLocalAddress() && nifAddress.isLoopbackAddress()) {
if (lo != null) {
throw new HekateException("Failed to resolve a loopback network interface. "
+ "Multiple loopback interfaces were detected [address=" + address + ", interface1=" + lo.getName()
+ ", interface2=" + nif.getName() + ']');
}
lo = nif;
break;
}
}
}
}
return lo;
}
示例2: startServer
import java.net.NetworkInterface; //导入方法依赖的package包/类
void startServer(final NetworkInterface networkInterface, List<DiscoveredCube> cubes) throws Exception {
if (networkInterface == null) {
throw new IllegalArgumentException("Network interface may not be null");
}
acceptor.getFilterChain().addLast("logging", new LoggingFilter());
acceptor.setHandler(new DiscoveryServerHandler(cubes));
acceptor.getSessionConfig().setReuseAddress(true);
acceptor.setCloseOnDeactivation(true);
Optional<InetAddress> ipv4Address = Collections.list(networkInterface.getInetAddresses()).stream()
.filter(addr -> addr instanceof Inet4Address).findFirst();
if (!ipv4Address.isPresent()) {
throw new IllegalStateException("No ipv4 address found for interface: " + networkInterface.getName());
}
acceptor.bind(new InetSocketAddress(ipv4Address.get(), port));
logger.debug("MinaDiscoveryClient bound to [{}]", acceptor.getLocalAddress());
}
示例3: isInterfaceAvailable
import java.net.NetworkInterface; //导入方法依赖的package包/类
@Override
public boolean isInterfaceAvailable(String ifaceName) throws SocketException {
/*
* String[]ifaces = getAvailableInterfaces(); if(ifaces == null ||
* ifaces.length == 0){ return false; }
*
* for(String name : ifaces){
*
* if (name.equals(ifaceName)){ return true; } }
*
* return false;
*/
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface nif = interfaces.nextElement();
String nm = nif.getName();
if (nm.equals(ifaceName)) {
return true;
}
}
return false;
}
示例4: getIp
import java.net.NetworkInterface; //导入方法依赖的package包/类
public void getIp() {
try {
// Automatic interface selection
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
.hasMoreElements();) {
NetworkInterface ni = en.nextElement();
intf = ni.getName();
ip = getInterfaceFirstIp(ni);
if (!ip.equals(NO_IP)) {
// if (ip != NO_IP) {
break;
}
}
} catch (SocketException e) {
Log.e(TAG, e.getMessage());
}
getCidr();
}
示例5: getDefaultNetworkInteface
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* this function, invoked by <code>getNetworkSpeed()</code>, identifies default network interface
* @return The name of default network interface
* @throws SocketException exception managed in <code>getNetworkSpeed()</code>
* @throws UnknownHostException exception managed in <code>getNetworkSpeed()</code>
*/
private String getDefaultNetworkInteface() throws SocketException, UnknownHostException {
Enumeration<NetworkInterface> netifs = NetworkInterface.getNetworkInterfaces();
InetAddress myAddr = InetAddress.getLocalHost();
while (netifs.hasMoreElements()) {
NetworkInterface networkInterface = netifs.nextElement();
Enumeration<InetAddress> inAddrs = networkInterface.getInetAddresses();
while (inAddrs.hasMoreElements()) {
InetAddress inAddr = inAddrs.nextElement();
if (inAddr.equals(myAddr)) {
return networkInterface.getName();
}
}
}
return "";
}
示例6: testAddressInterfaceLookup
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Test that selecting by name is possible and properly matches the addresses on all interfaces and virtual
* interfaces.
*
* Note that to avoid that this test fails when interfaces are down or they do not have addresses assigned to them,
* they are ignored.
*/
public void testAddressInterfaceLookup() throws Exception {
for (NetworkInterface netIf : NetworkUtils.getInterfaces()) {
if (!netIf.isUp() || Collections.list(netIf.getInetAddresses()).isEmpty()) {
continue;
}
String name = netIf.getName();
InetAddress[] expectedAddresses = Collections.list(netIf.getInetAddresses()).toArray(new InetAddress[0]);
InetAddress[] foundAddresses = NetworkUtils.getAddressesForInterface(name);
assertArrayEquals(expectedAddresses, foundAddresses);
}
}
示例7: selectMulticastInterface
import java.net.NetworkInterface; //导入方法依赖的package包/类
NetworkInterface selectMulticastInterface(InetSocketAddress node) throws HekateException {
InetAddress address = node.getAddress();
try {
if (DEBUG) {
log.debug("Resolving a network interface [address={}]", address);
}
NetworkInterface nif = NetworkInterface.getByInetAddress(address);
if (nif == null && address.isLoopbackAddress()) {
if (DEBUG) {
log.debug("Failed to resolve a network interface for a loopback address. Will try to find a loopback interface.");
}
nif = findLoopbackInterface(address);
}
if (nif == null) {
throw new HekateException("Failed to resolve a network interface by address [address=" + address + ']');
}
if (!nif.supportsMulticast()) {
throw new HekateException("Network interface doesn't support multicasting [name=" + nif.getName()
+ ", interface-address=" + address + ']');
}
return nif;
} catch (IOException e) {
throw new HekateException("Failed to resolve multicast network interface [interface-address=" + address + ']', e);
}
}
示例8: getIPs
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static String getIPs()
{
try
{
Enumeration<NetworkInterface> nets;
NetworkInterface n;
Enumeration<InetAddress> addrs;
InetAddress a;
String s = "";
nets = NetworkInterface.getNetworkInterfaces();
while (nets.hasMoreElements())
{
n = nets.nextElement();
if (n.isUp())
{
s += n.getName() + "\n";
addrs = n.getInetAddresses();
while (addrs.hasMoreElements())
{
a = addrs.nextElement();
if (a instanceof Inet4Address)
{
s += " * " + a.getHostAddress() + "\n";
}
}
}
}
return s.trim();
}
catch (SocketException e)
{
System.out.println("Error: couldn't get ip addresses for network interfaces");
return "";
}
}
示例9: getDeviceName
import java.net.NetworkInterface; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private static String getDeviceName(WifiManager wifiManager) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
Log.w(TAG, "Older device - falling back to the default device name: " + FALLBACK_DEVICE);
return FALLBACK_DEVICE;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.w(TAG, "6.0 or later, unaccessible MAC - falling back to the default device name: " + FALLBACK_DEVICE);
return FALLBACK_DEVICE;
}
String macString = wifiManager.getConnectionInfo().getMacAddress();
if (macString == null) {
Log.w(TAG, "MAC Address not found - Wi-Fi disabled? Falling back to the default device name: " + FALLBACK_DEVICE);
return FALLBACK_DEVICE;
}
byte[] macBytes = macAddressToByteArray(macString);
try {
Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
byte[] hardwareAddress = iface.getHardwareAddress();
if (hardwareAddress != null && Arrays.equals(macBytes, hardwareAddress)) {
return iface.getName();
}
}
} catch (IOException e) {
Log.e(TAG, "", e);
}
Log.w(TAG, "None found - falling back to the default device name: " + FALLBACK_DEVICE);
return FALLBACK_DEVICE;
}
示例10: assertNetworkInterfaceNameEqual
import java.net.NetworkInterface; //导入方法依赖的package包/类
static void assertNetworkInterfaceNameEqual(String expectedNetworkIfName,
NetworkInterface deserializedNetworkInterface) {
if (deserializedNetworkInterface != null) {
String deserializedNetworkIfName = deserializedNetworkInterface
.getName();
System.err
.println("Inet6AddressSerializationTest.assertHostNameEqual:");
if (expectedNetworkIfName == null) {
if (deserializedNetworkIfName == null) {
// ok, do nothing.
} else {
System.err.println("Error checking "
+ " NetworkIfName, expected: "
+ expectedNetworkIfName + ", got: "
+ deserializedNetworkIfName);
failed = true;
}
} else if (!expectedNetworkIfName.equals(deserializedNetworkIfName)) {
System.err.println("Error checking "
+ " NetworkIfName, expected: " + expectedNetworkIfName
+ ", got: " + deserializedNetworkIfName);
failed = true;
} else {
System.err.println("NetworkIfName equality "
+ " NetworkIfName, expected: " + expectedNetworkIfName
+ ", got: " + deserializedNetworkIfName);
}
} else {
System.err
.println("Warning "
+ " NetworkInterface expected, but is null - ifname not relevant on deserializing host");
}
}
示例11: 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);
}
}
示例12: getStatus
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Method to return status of the firewall.
* Status can be retrieved over REST API.
*
*/
public static String getStatus() {
String s = "";
s += "Jetty Server Status = " + jettyServer.getState() + "\n";
s += "Jetty Date = " + jettyServer.getDateField().toString() + "\n";
s += "Jetty URI = " + jettyServer.getURI().toString() + "\n";
s += "\n";
s += "SCTP Associations\n";
for (Map.Entry<String, Association> a : sctpManagement.getAssociations().entrySet()) {
s += " Name = " + a.getKey() + "\n";
s += " Details = " + a.getValue().toString() + "\n";
s += " isStarted = " + a.getValue().isStarted() + "\n";
s += " isConnected = " + a.getValue().isConnected() + "\n";
}
s += "\n";
s += "SCTP Servers = " + sctpManagement.getServers().toString() + "\n";
s += "\n";
s += "OS statistics\n";
s += " Available processors (cores): " + Runtime.getRuntime().availableProcessors() + "\n";
s += " Free memory (bytes): " + Runtime.getRuntime().freeMemory() + "\n";
long maxMemory = Runtime.getRuntime().maxMemory();
s += " Maximum memory (bytes): " + (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory) + "\n";
s += " Total memory available to JVM (bytes): " + Runtime.getRuntime().totalMemory() + "\n";
File[] roots = File.listRoots();
/* For each filesystem root, print some info */
for (File root : roots) {
s += " File system root: " + root.getAbsolutePath() + "\n";
s += " Total space (bytes): " + root.getTotalSpace() + "\n";
s += " Free space (bytes): " + root.getFreeSpace() + "\n";
s += " Usable space (bytes): " + root.getUsableSpace() + "\n";
}
s += "\n";
s += "Network interfaces\n";
try {
Enumeration<NetworkInterface> nets;
nets = NetworkInterface.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
s += " Display name: " + netint.getDisplayName() + "\n";
s += " Name: " + netint.getName() + "\n";
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
s += " InetAddress: " + inetAddress + "\n";
}
}
} catch (SocketException ex) {
java.util.logging.Logger.getLogger(DiameterFirewall.class.getName()).log(Level.SEVERE, null, ex);
}
return s;
}
示例13: select
import java.net.NetworkInterface; //导入方法依赖的package包/类
@Override
public InetAddress select() throws HekateException {
try {
if (opts.exactAddress() != null) {
if (DEBUG) {
log.debug("Using the exact address [{}]", opts);
}
return InetAddress.getByName(opts.exactAddress());
}
if (DEBUG) {
log.debug("Trying to resolve address [{}]", opts);
}
Pattern niIncludes = regex(opts.interfaceMatch());
Pattern niExcludes = regex(opts.interfaceNotMatch());
Pattern addrIncludes = regex(opts.ipMatch());
Pattern addrExcludes = regex(opts.ipNotMatch());
List<NetworkInterface> nis = networkInterfaces();
for (NetworkInterface ni : nis) {
if (!ni.isUp() || ni.isLoopback()) {
continue;
}
String niName = ni.getName();
if (matches(true, niName, niIncludes) && matches(false, niName, niExcludes)) {
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (DEBUG) {
log.debug("Trying address {}", address);
}
if (checkAddress(addrIncludes, addrExcludes, niName, address)) {
if (DEBUG) {
log.debug("Resolved address [interface={}, address={}]", niName, address);
}
return address;
}
}
} else {
if (DEBUG) {
log.debug("Skipped network interface that doesn't match name pattern [name={}]", niName);
}
}
}
} catch (IOException e) {
throw new HekateException("Failed to resolve node address [" + opts + ']', e);
}
return null;
}