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 :
- Syntax: rasdial connection name Username Password
- Example: rasdial my broadband hzhz1234567890 dfdfdfdfdf
Network disconnection
Java code
- Syntax: rasdial connection name/disconnect
- 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
- Establishing connection.
- The connection has been established successfully.
- Disconnected
- Establishing connection.
- 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
- While(! Connadsl ("Broadband","Hzhz **********","******")){
- Thread. Sleep (1000);
}