Java implements Dynamic IP address switching (ADSL dial-up Internet access) Java disconnection and reconnection (original) Solve the Problem of reading cmd message garbled

Source: Internet
Author: User

Http://gezhicheng.javaeye.com/blog/764228

The Implementation of Dynamic IP switching is also provided by the Windows rasdial command. In fact, it is not Java's credit. Java only calls the bat script:

Rasdial command:

 

Dialing

Java Code :
    1. Syntax: rasdial connection name Username Password
    2. Example: rasdial my broadband hzhz1234567890 dfdfdfdfdf
 

Network disconnection

Java code
    1. Syntax: rasdial connection name/disconnect
    2. Instance: rasdial broadband/disconnect

 

JavaProgramCall the rasdial command :(Garbled characters may occur when reading the CMD returned message. Pay attention to the encoding settings of the sentence in the red font.)

Java code
  Package  Com. Sesame. Network;

Import Java. Io. bufferedreader;
Import Java. Io. inputstreamreader;

Public Class Connectnetwork {

/**
* Run the CMD command and return the string.
*/
Public Static String executecmd (string strcmd) Throws Exception {
PROCESS p = Runtime.getruntime(cmd.exe C ( " CMD/C " + Strcmd );
Stringbuilder sbcmd = New Stringbuilder ();
Bufferedreader br = New Bufferedreader ( New Inputstreamreader (P
. Getinputstream () , "Gb2312"));// This is very important. Set gb2312 to fix Garbled text !!!
// If the default code of the program is gb2312, do not write
// Utf8 is used by default in netbeans.
String line;
While (Line = BR. Readline ()) ! = Null ){
Sbcmd. append (line + " \ N " );
}
Return Sbcmd. tostring ();

// If the entire process is changed to this, it is clearer. Getinputstream is the most primitive byte stream,
// The byte stream returned by CMD is gb2312 dubyte encoding.
Inputstream in = P. getinputstream ();
Byte [] B = new byte [1000];
In. Read (B );
String MSG = new string (B, "gb2312"); // use gb2312 to explain the heap bytes, And Then assemble them into a normal string.
// If gb2312 is not written above, it is equivalent to utf8 assembly. The result is the same.
}

/**
* Connect to ADSL
*/
Public Static Boolean Connadsl (string adsltitle, string adslname, string adslpass) Throws Exception {
System. Out. println ( " Establishing connection. " );
String adslcmd = " Rasdial " + Adsltitle + " " + Adslname + " "
+ Adslpass;
String tempcmd = Executecmd (adslcmd );
// Determine whether the connection is successful
If (Tempcmd. indexof ( " Connected " ) > 0 ){
System. Out. println ( " The connection has been established successfully. " );
Return True ;
} Else {
System. Err. println (tempcmd );
System. Err. println ( " An error occurred while establishing the connection. " );
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 + " The connection does not exist! " );
Return False ;
} Else {
System. Out. println ( " Disconnected " );
Return True ;
}
}

Public Static Void Main (string [] ARGs) Throws Exception {
Connadsl ( " Broadband " , " Hzhz ********** " , " ****** " );
Thread. Sleep ( 1000 );
Cutadsl ( " Broadband " );
Thread. Sleep ( 1000 );
// Re-connect and assign a new IP Address
Connadsl ( " Broadband " , " Hzhz ********** " , " ****** " );
}
}

 

Execution result:

Java code
    1. Establishing connection.
    2. The connection has been established successfully.
    3. Disconnected
    4. Establishing connection.
    5. The connection has been established successfully.

Summary:
The most important thing to implement this function is that the bat command can support this function, which is similar to the previously written function of automatically setting IP addresses. it is inconvenient to implement these functions in Java, it seems that it is necessary to elegantly implement the behaviors related to the Windows operating system and learn windows programming.

 

 

Redial mechanism:

 

Java code
    1. While(! Connadsl ("Broadband","Hzhz **********","******")){
    2. Thread. Sleep (1000);

  • }
  • Related Article

    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.