请问怎么获取本局域网的子网掩码呀?谢谢

解决方案 »

  1.   

    用JAVA调用系统的PING命令(windows,linux则是调用另一个相应命令),然后读取相应字符串。
    曾经有次网络的小实验就是任选门语言获取本机的MAC地址,最后我是通过这种方式实现的,不过很麻烦...看别人还有更好的办法没
      

  2.   

    ....打错了...是ipconfig
    RI,真失败- -!
      

  3.   


    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    public class MaskAddress {
        public MaskAddress() {
        }    public static String getMaskAddress() {        String address = null;
            String os = System.getProperty("os.name");
            if (os != null && os.startsWith("Windows")) {
                try {
                    String command = "cmd.exe /c ipconfig /all";
                    Process p = Runtime.getRuntime().exec(command);
                    BufferedReader br =
                            new BufferedReader(
                                    new InputStreamReader(p.getInputStream()));
                    String line;
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf("Subnet Mask") > 0) {
                            int index = line.indexOf(":");
                            index += 2;
                            address = line.substring(index);
                            break;
                        }
                    }
                    br.close();
                    return address.trim();
                } catch (IOException e) {}
            }
            return address;
        } public static void main(String[] args) {
        System.out.println("子网掩码:"+MaskAddress.getMaskAddress());
    }
    }
    利用ipconfig命令得到的,应该还有其它更好的办法我想。
      

  4.   

       肯定有,调用cmd的话,java的优势何在啊?都和操作系统捆绑了
      

  5.   

    不啊,没捆绑呀?
    我开始不就说了么,如果是linux就调用另外的命令。
    由if (os != null && os.startsWith("Windows")) 来判断你使用的操作系统,因为我估计LZ要的是WINDOWS下的,所以就只写了Windows部分。
    把其余操作系统的实现部分给添加上不就不限于特定系统了,何来捆绑不过还是希望能有更简单的操作就能得到这些信息就好了