本文整理汇总了Java中java.net.NetworkInterface.getInterfaceAddresses方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.getInterfaceAddresses方法的具体用法?Java NetworkInterface.getInterfaceAddresses怎么用?Java NetworkInterface.getInterfaceAddresses使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface.getInterfaceAddresses方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateIpAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
private void updateIpAddress() {
try {
Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
ipAddress = null;
while( b.hasMoreElements()){
NetworkInterface iface = b.nextElement();
if (iface.getName().startsWith("dock")) {
continue;
}
for ( InterfaceAddress f : iface.getInterfaceAddresses()) {
if (f.getAddress().isSiteLocalAddress()) {
ipAddress = f.getAddress().getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
示例2: main
import java.net.NetworkInterface; //导入方法依赖的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");
}
示例3: getBroadcastAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
static List<String> getBroadcastAddress() throws BrowsingException, SocketException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
List<String> broadcastAddresses = new ArrayList<>();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback()) {
continue;
}
for (InterfaceAddress interfaceAddress :
networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null) {
broadcastAddresses.add(broadcast.toString().substring(1));
}
}
}
return broadcastAddresses;
}
示例4: setIpInfoFromNetworkInterface
import java.net.NetworkInterface; //导入方法依赖的package包/类
private void setIpInfoFromNetworkInterface() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
if (networkInterfaces == null) {
return;
}
while (networkInterfaces.hasMoreElements()) {
NetworkInterface netIf = networkInterfaces.nextElement();
for (Enumeration<InetAddress> inetAddresses = netIf.getInetAddresses(); inetAddresses.hasMoreElements();) {
InetAddress inetAddress = inetAddresses.nextElement();
if (inetAddress.isLoopbackAddress() || inetAddress instanceof Inet6Address) {
continue;
}
if (netIf.getDisplayName().contains("wlan0")
|| netIf.getDisplayName().contains("eth0")
|| netIf.getDisplayName().contains("ap0")) {
FDroidApp.ipAddressString = inetAddress.getHostAddress();
for (InterfaceAddress address : netIf.getInterfaceAddresses()) {
short networkPrefixLength = address.getNetworkPrefixLength();
if (networkPrefixLength > 32) {
// something is giving a "/64" netmask, IPv6?
// java.lang.IllegalArgumentException: Value [64] not in range [0,32]
continue;
}
if (inetAddress.equals(address.getAddress()) && !TextUtils.isEmpty(FDroidApp.ipAddressString)) {
String cidr = String.format(Locale.ENGLISH, "%s/%d",
FDroidApp.ipAddressString, networkPrefixLength);
FDroidApp.subnetInfo = new SubnetUtils(cidr).getInfo();
break;
}
}
}
}
}
} catch (SocketException e) {
Log.e(TAG, "Could not get ip address", e);
}
}
示例5: getLocalAddresses
import java.net.NetworkInterface; //导入方法依赖的package包/类
public static Set<InetSocketAddress> getLocalAddresses ( final IoAcceptor acceptor ) throws SocketException
{
final Set<InetSocketAddress> result = new HashSet<InetSocketAddress> ();
for ( final SocketAddress address : acceptor.getLocalAddresses () )
{
logger.info ( "Bound to: {}", address );
if ( ! ( address instanceof InetSocketAddress ) )
{
continue;
}
final InetSocketAddress socketAddress = (InetSocketAddress)address;
if ( socketAddress.getAddress ().isAnyLocalAddress () )
{
final int port = socketAddress.getPort ();
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces ();
while ( interfaces.hasMoreElements () )
{
final NetworkInterface networkInterface = interfaces.nextElement ();
for ( final InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses () )
{
result.add ( new InetSocketAddress ( interfaceAddress.getAddress (), port ) );
}
}
}
else
{
result.add ( socketAddress );
}
}
return result;
}
示例6: getLocalAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Gets the local {@link java.net.InetAddress} of the network interface connected to a give remote address.
*
* @param remote the remote address
* @return the local {@link java.net.InetAddress} of the network interface
* @throws SocketException if an I/O error occurs.
*/
public static InetAddress getLocalAddress(InetAddress remote) throws SocketException{
byte[] remoteAddrByte = remote.getAddress();
Enumeration<NetworkInterface> interEnum = NetworkInterface.getNetworkInterfaces();
while(interEnum.hasMoreElements()){
NetworkInterface inter = interEnum.nextElement();
List<InterfaceAddress> addresses = inter.getInterfaceAddresses();
for (int i = 0; i < addresses.size(); i++) {
InterfaceAddress address = addresses.get(i);
byte[] byteAddr = address.getAddress().getAddress();
if(byteAddr.length != remoteAddrByte.length)
continue;
boolean error = false;
for(int j = 0; j < remoteAddrByte.length - 1; j++){
if(byteAddr[j] != remoteAddrByte[j]){
error = true;
break;
}
}
if(!error)
return address.getAddress();
}
}
return null;
}
示例7: testNetworkInterface_getInterfaceAddresses
import java.net.NetworkInterface; //导入方法依赖的package包/类
private static void testNetworkInterface_getInterfaceAddresses(
NetworkInterface netIf) {
try {
netIf.getInterfaceAddresses();
} catch (Exception ex) {
ex.printStackTrace();
incrementExceptionCount();
}
}
示例8: setBroadcastMask
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Set the broadcast mask to use for NetBIOS name lookups
*
* @param bcastMask String
* @exception AlfrescoRuntimeException
*/
public final void setBroadcastMask( String bcastMask)
throws IOException {
if ( bcastMask == null || bcastMask.length() == 0) {
// Clear the NetBIOS subnet mask
NetBIOSSession.setDefaultSubnetMask( null);
return;
}
// Find the network adapter with the matching broadcast mask
try {
Enumeration<NetworkInterface> netEnum = NetworkInterface.getNetworkInterfaces();
NetworkInterface bcastIface = null;
while ( netEnum.hasMoreElements() && bcastIface == null) {
NetworkInterface ni = netEnum.nextElement();
for ( InterfaceAddress iAddr : ni.getInterfaceAddresses()) {
InetAddress broadcast = iAddr.getBroadcast();
if ( broadcast != null && broadcast.getHostAddress().equals( bcastMask))
bcastIface = ni;
}
}
// DEBUG
if ( logger.isDebugEnabled()) {
if ( bcastIface != null)
logger.debug("Broadcast mask " + bcastMask + " found on network interface " + bcastIface.getDisplayName() + "/" + bcastIface.getName());
else
logger.debug("Failed to find network interface for broadcast mask " + bcastMask);
}
// Check if we found a valid network interface for the broadcast mask
if ( bcastIface == null)
throw new AlfrescoRuntimeException("Network interface for broadcast mask " + bcastMask + " not found");
// Set the NetBIOS broadcast mask
NetBIOSSession.setDefaultSubnetMask( bcastMask);
}
catch ( SocketException ex) {
}
}
示例9: findIPv6Interface
import java.net.NetworkInterface; //导入方法依赖的package包/类
private NetworkInterface findIPv6Interface(Inet6Address address) throws IOException {
if(blacklisted.isEmpty()) {
if(log.isDebugEnabled()) {
log.debug("Ignore IP6 default network interface setup with empty blacklist");
}
return null;
}
if(address.getScopeId() != 0) {
if(log.isDebugEnabled()) {
log.debug(String.format("Ignore IP6 default network interface setup for address with scope identifier %d", address.getScopeId()));
}
return null;
}
// If we find an interface name en0 that supports IPv6 make it the default.
// We must use the index of the network interface. Referencing the interface by name will still
// set the scope id to '0' referencing the awdl0 interface that is first in the list of enumerated
// network interfaces instead of its correct index in <code>java.net.Inet6Address</code>
// Use private API to defer the numeric format for the address
List<Integer> indexes = new ArrayList<Integer>();
final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
while(enumeration.hasMoreElements()) {
indexes.add(enumeration.nextElement().getIndex());
}
for(Integer index : indexes) {
final NetworkInterface n = NetworkInterface.getByIndex(index);
if(log.isDebugEnabled()) {
log.debug(String.format("Evaluate interface with %s index %d", n, index));
}
if(!n.isUp()) {
if(log.isDebugEnabled()) {
log.debug(String.format("Ignore interface %s not up", n));
}
continue;
}
if(blacklisted.contains(n.getName())) {
log.warn(String.format("Ignore network interface %s disabled with blacklist", n));
continue;
}
for(InterfaceAddress i : n.getInterfaceAddresses()) {
if(i.getAddress() instanceof Inet6Address) {
if(log.isInfoEnabled()) {
log.info(String.format("Selected network interface %s", n));
}
return n;
}
}
log.warn(String.format("No IPv6 for interface %s", n));
}
log.warn("No network interface found for IPv6");
return null;
}