日期:2014-05-20  浏览次数:20786 次

问下 关于宽带连接 重复连接 直到第三段ip等于某个值 怎么弄呢
Java code
package test;

import java.io.IOException;
import java.net.InetAddress;

public class IpTest {
    public static void main(String[] args){

            boolean key = false;
            do{
                try {
                    Runtime.getRuntime().exec("cmd /c start rasdial \"宽带连接\" /disconnect");
                    Runtime.getRuntime().exec("cmd /c start rasdial \"宽带连接\" zhanghao mima");
                } catch (IOException ee) {
                    ee.printStackTrace();
                }
                InetAddress[]   mArLocalIP   =   null; 
                if(mArLocalIP   ==   null) { 
                    try { 
                        mArLocalIP   =   InetAddress.getAllByName(InetAddress.getLocalHost().getHostName());
                        String adress = mArLocalIP[0].toString();
                        System.out.println(adress);
                        String address2 = adress.substring(16, 19);
                        System.out.println(address2);
                        if (address2.equals("152")){
                            key = true;
                        }
                    } 
                    catch(Exception   e) {
                        System.out.println( "getHostIP   error "); 
                    } 
                    
                } 
            }while(key == false);
    } 
}



这样的话出现太多的DOS窗口了

差点死机 

有没有什么好的方法呢



------解决方案--------------------
个人建议
1.能不能在cmd /c start rasdial \"宽带连接\" zhanghao mima后面紧接着执行exit关闭dos
类似cmd /c start rasdial \"宽带连接\" zhanghao mima;exit
2.sleep一段时间,不然机器受不鸟

------解决方案--------------------
Java code
import java.io.IOException;
import java.net.InetAddress;

public class Test {
    public static void main(String[] args) throws IOException, InterruptedException {
        InetAddress addr = InetAddress.getLocalHost();
        String ip = addr.getHostAddress().toString();
        System.out.println(ip);
        while(!"192.168.18.1".equals(ip)){
            Process disconnect = Runtime.getRuntime().exec("rasdial.exe 宽带连接 /disconnect");   
            disconnect.waitFor();   
            System.out.println("断开");   
            Process connect = Runtime.getRuntime().exec("rasdial.exe 宽带连接 username password");   
            connect.waitFor();     
            System.out.println("连接");   
            ip = addr.getHostAddress().toString();
        }
    }
}