如果计算机通过adsl上网的,那编程中如何获得adsl网络连接的ip地址?我现在只能得到本地局域网的ip地址.郁闷ing!

解决方案 »

  1.   

    调用ipconfig /all
    从结果中解析你需要的东西
      

  2.   

    一楼的方法我现在用着,只能得到本地局域网的ip得不到adsl连接的ip
    二楼的兄弟能否说详细一点呢?
      

  3.   

    你可以用:
    Process p=Runtime.getRuntime().exec("netstat -e");
    得到一个netstat -e命令的子进程,用该进程的getInputstream将信息输出.
      

  4.   

    我有一个获取mac地址的程序!楼主可以参考!
      

  5.   

    我采用的是调用ipconfig /all来显示信息!
    程序如下:
    import java.io.InputStreamReader;
    import java.io.BufferedReader;public class macaddress {
      public static void main(String[] args) {
        macaddress mdd = new macaddress();
        String str=mdd.getMacOnWindow();
        System.out.println(str);
      }
      private static String getMacOnWindow() {
            String s = "";
            try {
                String s1 = "ipconfig /all";
                Process process = Runtime.getRuntime().exec(s1);
                BufferedReader bufferedreader = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));
                String nextLine;
                for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
                    nextLine = bufferedreader.readLine();
                    if (line.indexOf("Physical Address") <= 0) {
                        continue;
                    }
                    int i = line.indexOf("Physical Address") + 36;
                    s = line.substring(i);
                    break;
                }            bufferedreader.close();
                process.waitFor();
            } catch (Exception exception) {
                s = "";
            }
            return s.trim();
        }}
      

  6.   

    我有个办法,可是,我现在没有带着,收藏先,等我拿到了再回给你。PS:Java是个和平台相对无关的技术啊!
      

  7.   

    int deviceNum;
       int ipNum;   protected void analyzeDevices() {
          deviceNum = 0;
          ipNum = 0;
          NetworkInterface tmpInterface;
          String tmpStr;
          Vector devicesVector = new Vector();
          Vector ipNumVector = new Vector();
          Vector ipAddressVector = new Vector();
          Enumeration ipAddressesOnEachDevice;
          Enumeration devices;
          try {
             devices = NetworkInterface.getNetworkInterfaces();
             while (devices.hasMoreElements()) {
                tmpInterface = (NetworkInterface) devices.nextElement();
                tmpStr = tmpInterface.toString();
                devicesVector.add(tmpStr.substring(5,
                                  tmpStr.lastIndexOf("index:") - 1));
                ipNum = 0;
                ipAddressesOnEachDevice = tmpInterface.getInetAddresses();
                while (ipAddressesOnEachDevice.hasMoreElements()) {
                   String ip = ipAddressesOnEachDevice.nextElement().toString();
                   ipAddressVector.add(ip);
                   ipNum++;
                }
                ipNumVector.add(new Integer(ipNum));
             }
          } catch (SocketException se) {
          }
          deviceNum = devicesVector.size();
          deviceNames = new String[deviceNum];
          ipNumOfEachDevice = new int[deviceNum];
          for (int i = 0; i < deviceNum; i++) {
             deviceNames[i] = (String) devicesVector.elementAt(i);
             ipNumOfEachDevice[i] = ((Integer) ipNumVector.elementAt(i)).intValue();
          }
          ipNum = ipAddressVector.size();
          ipAddresses = new String[ipNum];
          for (int i = 0; i < ipAddresses.length; i++) {
             ipAddresses[i] = (String) ipAddressVector.elementAt(i);
          }
       }
    由于時間比較緊,我就不給你分析代碼了,你自己看看吧。
      

  8.   

    OnlyFor_love(将《java编程思想》藏在心中!) 的代码不错,谢谢,学习
      

  9.   

    看了OnlyFor_love(将《java编程思想》藏在心中!)和森林之洋的代码,
    个人感觉前者确实只适合在windows下调用,而后者做到了平台无关性,
    应该是一种更好的解决方案,我自己当时也查阅了森林兄用到的NetworkInterface
    这个类,当时感觉应该是可以用它解决的,但是没有细看,现在看了代码,
    感觉很激动,明天考试,考完就开始实验。感谢大家参与,期望更多的讨论!
      

  10.   

    None of the above offered solutions will you the external ip address you wanted. They can only give you your local ip address(es). One possible solution is to visit http://checkip.dyndns.org/ in your Java program and parse the returned html to extract your external IP address.
      

  11.   

    txranger, thankou for your advice and i think is't a good opinion!but
    if i use proxy ,by your method,i think i just can obtain the proxy ip!
    do you think so?
      

  12.   

    (中文版回帖)texranger, 很感谢你的建议,我想这也是一个很好的想法,但是
    但我使用了代理服务器的话那我不是只能得到代理服务器的地址了?你说呢?欢迎讨论!
    刚考完试!哈哈!
      

  13.   

    You didn't mention proxy server in your original post. That's an interesting question. I don't have a solution off hand.