Java write broadband connection. ADSL dialing, and detect disconnection automatically reconnect, replace IP

Source: Internet
Author: User
Tags readline stringbuffer
Package Com.hua;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;

Import Java.util.Scanner; public class Connectnetwork {/** * executes the cmd command and returns a string string */public static string Executecmd (String strcmd) throws Ex
		ception {Process p = runtime.getruntime (). EXEC ("cmd/c" + strcmd);
		StringBuilder sbcmd = new StringBuilder ();
		BufferedReader br = new BufferedReader (New InputStreamReader (P.getinputstream (), "GB2312"));
		String Line;
		while (line = Br.readline ())!= null) {sbcmd.append (line + \ n));

	return sbcmd.tostring (); /** * Connect ADSL/public static Boolean CONNADSL (String adsltitle, String adslname, String adslpass) throws Excepti
		On {System.out.println ("connection is being established.");
		String adslcmd = "rasdial" + Adsltitle + "" + Adslname + "" + adslpass;
		String tempcmd = Executecmd (adslcmd);
			Determines whether the connection succeeds if (Tempcmd.indexof (connected) > 0) {System.out.println ("The connection was successfully established."); return true;
		else {System.err.println (tempcmd);
			SYSTEM.ERR.PRINTLN ("Establish connection failed");
		return false;  }/** * Disconnect ADSL/public static Boolean CUTADSL (String adsltitle) throws Exception {string cutadsl = "rasdial
		"+ Adsltitle +"/disconnect ";

		String result = Executecmd (CUTADSL);
			if (Result.indexof ("no Connection")!=-1) {System.err.println (adsltitle + "Connection does not exist!");
		return false;
			else {System.out.println ("disconnected");
		return true;
	        }/** * Test network connectivity/public static Boolean Isconnect () {Boolean connect = false;
	        Runtime Runtime = Runtime.getruntime ();
	        Process process;
	            try {process = Runtime.exec ("ping" + "www.baidu.com"); 
	            InputStream is = Process.getinputstream (); 
	            InputStreamReader ISR = new InputStreamReader (IS); 
	            BufferedReader br = new BufferedReader (ISR); 
	            String line = null; 
	            StringBuffer sb = new StringBuffer (); while (LinE = Br.readline ())!= null) {sb.append (line);  
	            } System.out.println ("Return value is:" +SB); 
	            Is.close (); 
	            Isr.close (); 
	 
	            Br.close (); 
	                if (null!= SB &&!sb.tostring (). Equals ("")) {String logstring = "";
	                if (sb.tostring (). IndexOf ("TTL") > 0) {//network unblocked connect = true;
	                else {//network not unblocked connect = false;
	        A catch (IOException e) {e.printstacktrace ());
	    return connect; }//test code public static void Main (string[] args) throws Interruptedexception, Exception {Scanner sc = new Scanner (S
	  ystem.in); SYSTEM.OUT.PRINTLN ("Broadband Connection Name:");
	  Look at you. The name of the broadband connection String name = Sc.next (); 
	  System.out.println ("Broadband Account:");
	  String username = Sc.next (); 
	SYSTEM.OUT.PRINTLN ("Broadband Password:");  String password = sc.next ();
		  String adsl= "broadband connection";
	          while (True) {Boolean connect = Isconnect ();
	              Thread.Sleep (100000);//in milliseconds, I set the 100 seconds. Look at yourself. Change if (!connect) {System.out.println ("No network, dialing on");
	          CONNADSL (Name,username,password); }
		  }
	  
	}
}
/**
* Automatic replacement of IP
* *  */
public void Changeip () throws exception{
    Scanner sc = new Scanner (system.in);
    SYSTEM.OUT.PRINTLN ("Broadband Connection Name:"); Look at you. The name of the broadband connection
    String name = Sc.next ();
    System.out.println ("Broadband Account:");
    String username = Sc.next ();
    SYSTEM.OUT.PRINTLN ("Broadband Password:");
    String password = sc.next ();
    SYSTEM.OUT.PRINTLN ("Replacement time (1 seconds in milliseconds equals 1000 milliseconds):");
    int wait = Sc.nextint ();
    while (true) {
      CONNADSL (name, username, password);
      Thread.Sleep (wait);
      CUTADSL (name);
      Thread.Sleep (wait);
      Again, assign a new IP
      CONNADSL (name, username, password);
    }
    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.