2009-08-03 4 views

risposta

4

java.net.InterfaceAddress in SE6 ha un metodo getNetworkPrefixLength che restituisce, come suggerisce il nome, la lunghezza del prefisso di rete. È possibile calcolare la subnet mask da questo se si preferisce averlo in quel formato. java.net.InterfaceAddress supporta sia IPv4 che IPv6.

getSubnetMask() in varie API di applicazioni rete restituisce la maschera di sottorete in forma java.net.InetAddress per l'indirizzo IP specificato (un sistema locale può avere molti indirizzi IP locali)

22

la maschera di rete del primo indirizzo del localhost interfaccia:

InetAddress localHost = Inet4Address.getLocalHost(); 
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); 
networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength(); 

un approccio più completo:

InetAddress localHost = Inet4Address.getLocalHost(); 
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); 

for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) { 
    System.out.println(address.getNetworkPrefixLength()); 
} 

/24 significa 255.255.255.

+0

perché questo metodo non visualizza le informazioni IP privato su android. funziona su un computer ma non su Android. mi dà informazioni private su ipv6 e localhost. Ma non ipv4 04-04 14: 16: 02.269 1570-1630/com.example.android.droidscanner V/INET:/:: 1% 1% 1/128 [null] 04-04 14: 16: 02.269 1570- 1630/com.example.android.droidscanner V/INET: /127.0.0.1/8 [null] – miatech

1

FWIW, in passato avevo provato a utilizzare InterfaceAddress.getNetworkPrefixLength() e InterfaceAddress.getBroadcast(), ma non restituiscono informazioni accurate (questo è su Windows, con Sun JDK 1.6.0 update 10). La lunghezza del prefisso di rete è 128 (non 24, che è sulla mia rete), e l'indirizzo di broadcast restituito è 255.255.255.255 (non 192.168.1.255, che è sulla mia rete).

James

Aggiornamento: Ho appena trovato la soluzione postato qui:

 http://forums.sun.com/thread.jspa?threadID=5277744 

è necessario per prevenire Java da utilizzare IPv6, in modo che non è di arrivare a IPv4 tramite IPv6. Aggiunta di -Djava.net.preferIPv4Stack = true alla riga di comando corregge i risultati da InterfaceAddress.getNetworkPrefixLength() e InterfaceAddress.getBroadcast() per me.

+0

Sfortunatamente non per me .... Darà risultati incoerenti. Il collegamento – gd1

+0

non funziona – Andy

3

Ho ideato una soluzione solo IPv4 che sia abbastanza semplice. Avevo bisogno di questo per generare maschera di rete per sottoreti qui per delegare correttamente quelle sottoreti. So che avrei potuto generare una tabella delle 32 possibili maschere, ma ho preferito ottenerlo calcolato ogni volta.

Quindi ecco la mia soluzione.

/* 
* Get network mask for the IP address and network prefix specified... 
* The network mask will be returned has an IP, thus you can 
* print it out with .getHostAddress()... 
*/ 
public static InetAddress getIPv4LocalNetMask(InetAddress ip, int netPrefix) { 

    try { 
     // Since this is for IPv4, it's 32 bits, so set the sign value of 
     // the int to "negative"... 
     int shiftby = (1<<31); 
     // For the number of bits of the prefix -1 (we already set the sign bit) 
     for (int i=netPrefix-1; i>0; i--) { 
      // Shift the sign right... Java makes the sign bit sticky on a shift... 
      // So no need to "set it back up"... 
      shiftby = (shiftby >> 1); 
     } 
     // Transform the resulting value in xxx.xxx.xxx.xxx format, like if 
     /// it was a standard address... 
     String maskString = Integer.toString((shiftby >> 24) & 255) + "." + Integer.toString((shiftby >> 16) & 255) + "." + Integer.toString((shiftby >> 8) & 255) + "." + Integer.toString(shiftby & 255); 
     // Return the address thus created... 
     return InetAddress.getByName(maskString); 
    } 
     catch(Exception e){e.printStackTrace(); 
    } 
    // Something went wrong here... 
    return null; 
} 

basta chiamare con l'IP e il prefisso che si desidera utilizzare, verrà generato la maschera di rete per voi.

3

ho scoperto che:

NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); 

Per ottenere subnetmask per IPv6 possiamo usare:

networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength(); 

Per ottenere subnetmask per IPv4 possiamo usare:

networkInterface.getInterfaceAddresses().get(1).getNetworkPrefixLength(); 
1

Ecco un rispondi, come ottenere una sottomaschera dalla connessione WIFI: link

ho adattato per le mie esigenze, e qui è:

private static String intToIP(int ipAddress) { 
    String ret = String.format("%d.%d.%d.%d", (ipAddress & 0xff), 
      (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), 
      (ipAddress >> 24 & 0xff)); 

    return ret; 
} 

public static String GetSubnetMask_WIFI() { 

    WifiManager wifiManager = (WifiManager) Global.getMainActivity() 
      .getSystemService(Context.WIFI_SERVICE); 
    WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 

    DhcpInfo dhcp = wifiManager.getDhcpInfo(); 
    String mask = intToIP(dhcp.netmask); 

    return mask; 
} 
+4

Questo è specifico per Android, che la domanda non ha specificato. – WouterH

0
String local=InetAddress.getLocalHost().getHostAddress(); 
String[] ip_component = local.split("\\."); 
String subnet=ip_component[0]+"."+ip_component[1]+"."+ip_component[2]+"."; 

Questo ha funzionato per me. qui la subnet variabile ha l'indirizzo di sottorete.

-2

È possibile convertire il valore ottenuto nel formato testuale standard come questo:

short prflen=...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; 
String submask = oct1+"."+oct2+"."+oct3+"."+oct4; 
Problemi correlati