2009-08-03 11 views

उत्तर

4

java.net। एसई 6 में इंटरफेस एड्रेस में एक GetNetworkPrefixLength विधि है जो नाम बताती है, नेटवर्क उपसर्ग लंबाई। यदि आप इसे उस प्रारूप में रखना चाहते हैं तो आप इससे सबनेट मास्क की गणना कर सकते हैं। java.net.InterfaceAddress आईपीवी 4 और आईपीवी 6 दोनों का समर्थन करता है।

getSubnetMask() कई नेटवर्क आवेदन एपीआई में देता निर्दिष्ट IP पते के लिए java.net.InetAddress रूप में सबनेट मास्क (किसी स्थानीय सिस्टम कई स्थानीय आईपी पते हो सकते हैं)

22

स्थानीय होस्ट के पहले पते की नेटमास्क इंटरफ़ेस:

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

अधिक पूर्ण दृष्टिकोण:

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

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

/24 का मतलब है 255.255.255।

+0

क्यों इस विधि एंड्रॉयड पर निजी आईपी जानकारी प्रदर्शित नहीं करता है। यह एक कंप्यूटर पर लेकिन एंड्रॉयड पर नहीं काम करता है। यह मेरे IPv6 और स्थानीय होस्ट पर निजी जानकारी देता है। लेकिन आईपीवी 4 04-04 14: 16: 02.269 1570-1630/com.example.android.droidscanner वी/आईएनईटी:/:: 1% 1% 1/128 [शून्य] 04-04 14: 16: 02.269 1570- 1630/com.example.android.droidscanner वी/INET: /127.0.0.1/8 [अमान्य] – miatech

1

Fwiw, अतीत में मैं (इस विंडोज पर है, सूर्य JDK 1.6.0 अद्यतन 10) के साथ InterfaceAddress.getNetworkPrefixLength() और InterfaceAddress.getBroadcast() उपयोग करने की कोशिश थी, लेकिन वे सही जानकारी नहीं लौटाते हैं। नेटवर्क उपसर्ग लंबाई 128 है (24 नहीं, जो यह मेरे नेटवर्क पर है), और लौटाया गया प्रसारण पता 255.255.255.255 (1 9 2.168.1.255 नहीं है, जो कि यह मेरे नेटवर्क पर है)।

जेम्स

अद्यतन: मैं सिर्फ पाया समाधान यहां पोस्ट:

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

आप इतना है कि यह IPv6 द्वारा आईपीवी 4 के लिए हो रही है, आईपीवी 6 का उपयोग करने से जावा को रोकने के लिए की जरूरत है। जोड़ना -Djava.net.preferIPv4Stack = कमांड लाइन के लिए सच इंटरफ़ेस एड्रेस.getNetworkPrefixLength() और InterfaceAddress.getBroadcast() से परिणामों को ठीक करता है।

+0

मेरे लिए दुर्भाग्य से नहीं .... यह असंगत परिणाम दे देंगे। – gd1

+0

लिंक – Andy

3

मैंने एक आईपीवी 4 केवल समाधान तैयार किया जो काफी आसान है। मुझे उन सबनेट्स को सही तरीके से प्रतिनिधि करने के लिए यहां सबनेटवर्क के लिए नेटमास्क उत्पन्न करने की आवश्यकता थी। मुझे पता है कि मैं 32 संभावित मास्क की एक तालिका उत्पन्न कर सकता था, लेकिन मैंने इसे हर बार गणना करने के लिए पसंद किया।

तो मेरा समाधान यहां है।

/* 
* 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; 
} 

तुम बस आईपी से कॉल करने और उपसर्ग का उपयोग करना चाहते हैं, यह आप के लिए नेटमास्क उत्पन्न होगा।

2

मैंने अभी जावा के साथ नेटवर्क को सबनेट करने के लिए एक एपीआई पर काम करना समाप्त कर दिया है।

https://launchpad.net/subnettingapi

यह है कि कार्यक्षमता और अधिक है।

3

मैंने पाया कि:

NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); 

IPv6 के लिए subnetmask प्राप्त करने के लिए हम उपयोग कर सकते हैं:

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

प्राप्त करने के लिए आईपीवी 4 के लिए subnetmask हम उपयोग कर सकते हैं:

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

यहाँ एक है link

: जवाब है, कैसे वाईफ़ाई कनेक्शन से एक submask पाने के लिए 210

मैं अपने की जरूरत के लिए यह अनुकूलित, और यहाँ यह है:

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

काम नहीं करता है यह एंड्रॉयड विशिष्ट है, जो सवाल निर्दिष्ट नहीं किया है। – WouterH

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

यह मेरे लिए काम किया। यहाँ चर सबनेट सबनेट पता है।

-2

आप इस तरह मानक शाब्दिक प्रारूप में प्राप्त मूल्य में बदल सकते हैं:

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; 
संबंधित मुद्दे