2009-08-03 51 views

答えて

4

SE6のjava.net.InterfaceAddressには、名前が示すように、ネットワークプレフィックスの長さを返すgetNetworkPrefixLengthメソッドがあります。この形式でサブネットマスクを使用する場合は、サブネットマスクを計算することができます。 java.net.InterfaceAddressは、IPv4とIPv6の両方をサポートしています。

getSubnetMask()指定されたIPアドレスのjava.net.InetAddress形態のいくつかのネットワークアプリケーションAPIリターンサブネットマスク内(ローカル・システムは、多くのローカルIPアドレスを有していてもよい)

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とlocalhostに関する私的な情報を与えます。しかし、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では、これまでInterfaceAddress.getNetworkPrefixLength()とInterfaceAddress.getBroadcast()を使用していましたが、正確な情報は返されませんでした(WindowsではSun JDK 1.6.0アップデート10)。ネットワークプレフィックスの長さは128です(ネットワーク上の24ではなく)。ブロードキャストアドレスは255.255.255.255(192.168.1.255ではなく、ネットワーク上にあります)です。

ジェームズ

アップデート:私は解決策がここに掲載見つかりました

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

あなたはそれがIPv6経由のIPv4になっていないように、IPv6を使用してからJavaを防ぐために必要です。 -Djava.net.preferIPv4Stack = trueをコマンドラインに追加すると、InterfaceAddress.getNetworkPrefixLength()およびInterfaceAddress.getBroadcast()の結果が修正されます。

+0

残念ながら私にとってはそうではありません....これは矛盾した結果をもたらします。 – gd1

+0

リンクが動作しません – Andy

3

私は十分に単純なIPv4専用ソリューションを考案しました。これらのサブネットを正しく委任するために、サブネットワークのネットマスクを生成する必要がありました。私は可能な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; 
} 

使用するIPとプレフィックスで呼び出すだけで、ネットマスクが生成されます。

2

Javaでネットワークをサブネット化するためのAPIの作業が終了しました。

https://launchpad.net/subnettingapi

それはその機能性とより多くを持っています。

3

私がことがわかった:

NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost); 

私たちが使用できるIPv6のサブネットマスクを取得するには、次の

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

を我々が使用できるIPv4のサブネットマスクを取得するには:ここで

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

があります答え、WIFI接続からサブマスクを取得する方法:link

私は私のニーズのためにそれを適応し、そしてここにある:

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

あなたはこのような標準的なテキスト形式に得られた値を変換することができます。この方法は、Android上のプライベートIP情報が表示されない理由を

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; 
関連する問題