Java implements Dynamic IP address switching (ADSL dial-up Internet access) and adsl dial-up Internet access

Source: Internet
Author: User

Java implements Dynamic IP address switching (ADSL dial-up Internet access) and adsl dial-up Internet access

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

The java program calls the rasdial command:

Java code
  1. Package com. sesame. network;
  2. Import java. io. BufferedReader;
  3. Import java. io. InputStreamReader;
  4. Public class ConnectNetWork {
  5. /**
  6. * Run the CMD command and return the String.
  7. */
  8. Public static String executeCmd (String strCmd) throws Exception {
  9. Process p = runtime.getruntime(cmd.exe c ("cmd/c" + strCmd );
  10. StringBuilder sbCmd = new StringBuilder ();
  11. BufferedReader br = new BufferedReader (new InputStreamReader (p
  12. . GetInputStream ()));
  13. String line;
  14. While (line = br. readLine ())! = Null ){
  15. SbCmd. append (line + "\ n ");
  16. }
  17. Return sbCmd. toString ();
  18. }
  19. /**
  20. * Connect to ADSL
  21. */
  22. Public static boolean connAdsl (String adslTitle, String adslName, String adslPass) throws Exception {
  23. System. out. println ("establishing connection .");
  24. String adslCmd = "rasdial" + adslTitle + "" + adslName + ""
  25. + AdslPass;
  26. String tempCmd = executeCmd (adslCmd );
  27. // Determine whether the connection is successful
  28. If (tempCmd. indexOf ("connected")> 0 ){
  29. System. out. println ("Connection established successfully .");
  30. Return true;
  31. } Else {
  32. System. err. println (tempCmd );
  33. System. err. println ("failed to establish a connection ");
  34. Return false;
  35. }
  36. }
  37. /**
  38. * Disconnect ADSL
  39. */
  40. Public static boolean cutAdsl (String adslTitle) throws Exception {
  41. String cutAdsl = "rasdial" + adslTitle + "/disconnect ";
  42. String result = executeCmd (cutAdsl );
  43. If (result. indexOf ("no connection ")! =-1 ){
  44. System. err. println (adslTitle + "the connection does not exist! ");
  45. Return false;
  46. } Else {
  47. System. out. println ("disconnected ");
  48. Return true;
  49. }
  50. }
  51. Public static void main (String [] args) throws Exception {
  52. ConnAdsl ("Broadband", "hzhz **********","******");
  53. Thread. sleep (1000 );
  54. CutAdsl ("Broadband ");
  55. Thread. sleep (1000 );
  56. // Re-connect and assign a new IP Address
  57. ConnAdsl ("Broadband", "hzhz **********","******");
  58. }
  59. }

 


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.


How does the dynamic IP address change when the ADSL dial-up accesses the Internet?

The IP address is changed only after, for example, A. B .C.D. C and D are changed.
Do you want to change the QQ password or forget the QQ password statement?
If you know your QQ password, you only need to answer your security questions.
If you forget your QQ password, you need to answer the questions you have logged on to, the passwords you used before, the time when you applied for the QQ number for the first time, and so on.

You don't have to change the QQ password because you have doubts !!

How can I change the Internet IP address for adsl dial-up access?

Cannot be modified. You are using the dynamic IP address automatically assigned to you by China Telecom. You cannot modify it yourself. If you change the IP address, you can switch the adsl cat again to automatically obtain the new IP address.

It cannot be modified by individuals. It is easy to understand that if the IP addresses on the public network can be modified freely by hundreds of millions of people around the world, frequent Ip address conflicts may occur, no one can use the internet normally.

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.