Two ways to dynamically allocate IP when dial-up Internet access

zhaozj2021-02-16  47

Two ways to dynamically allocate IP when dial-up Internet access

1: Use the socket function. Get all local IP before dialing, record it into the list of strings. After the dial is successful, it will pass all local IPs again, and the IP comparisons have been recorded in the list list. The newly added IP address is dynamically assigned IP when dialing.

2: Use and RAS about the API. It can be convenient to use and RAS-related APIs in the VC. But there will be errors that are different from the version in C Builder 6. Therefore, the key in the BCB is to specify the size of the function.

The following code compiled in the Win2000 C Builder 6 environment.

Add 2 Edit, 3 Button in Form;

/ / -------------------------------------------------------------------------------------------- ---------------------------

#ifndef unit1H # Define unit1H // ------------------------------------------------------------------------------------------------------------------------------------ --------------------------------- # include #include #include < STDCTRLS.HPP> #include // ------------------------------------- -------------------------------------- Class TFORM1: PUBLIC TFORM {__ Published: // Ide- managed Components TEdit * Edit1; TButton * Button1; TButton * Button2; TEdit * Edit2; TButton * Button3; void __fastcall Button1Click (TObject * Sender); void __fastcall Button2Click (TObject * Sender); void __fastcall Button3Click (TObject * Sender); private : // user declarations tstringlist * m_slip; public: // user declarations __fastcall tform1 (tComponent * Owner);}; // ----------------------- -------------------------------------------------- --Extern package tform1 * form1; // ---------------------------------------- ----------------------------------- # Endif

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include #pragma HDRSTOP

#include "unit1.h" #include #include // --------------------------- ------------------------------------------------ # Pragma Package (smart_init) #pragma resource "* .dfm" TFORM1 * FORM1; // ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------__ fastcall tform1 :: tform1 (tcomponent * Owner: TFORM (OWNER) {m_slip = new tstringlist ();} // ------------------------------- ------------------------------------------- void __fastcall tform1 :: button1click (Tobject * sender) {Word WVersionRequested; CHAR ChOSTNAME [64] = {0}; hostent * phost = null; Ansistring PSIP; INDEX; WVERSIONREQUESTED = Makeword (2, 0);

IF (0! = WSAStartup (WVersionRequested, & WSADATA) {wsacleanup (); showMessage ("Socket version number is wrong);}

IF (0 == gethostname (chOSTNAME, 64)) {phost = gethostByname (chOSTNAME); if (phost! = null) {for (int i = 0; phost-> h_addr_list [i]! = null; i ) {IN_ADDR * p = (in_addr *) (phost-> h_addr_list [i]); psip = inet_ntoa (* p); m_slip-> add (psip); // IP address deposit into string list}} else {wsacleanup (); ShowMessage ("Take IP Address Failed");}} else {showMessage ("Take the machine name failed");}

WSACLEANUP ();

} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Void __fastcall tform1 :: button2click (Tobject * sender) {rasconn rasconn; // activity connection DWORD BUFFSIZE // Active connection accounts for the memory size; DWORD connUM; // Event connection number rasconn.dwsize = 692; // must specify a connection of memory size; buffsize = 692; raspppip rip; Ansistring modemip; zeromeMory (& Rip, Sizeof )); DWORD SIZE; SIZE = 40; rip.dwsize = 40;

DWORD dwReturn = RasEnumConnections (& RASconn, & BuffSize, & ConnNum); if (dwReturn == 0) {if (ConnNum> 0) {// ShowMessage ( "dial-up connection is not!"); If ((RasGetProjectionInfo (RASconn.hrasconn, RASP_PppIp, & rip, & size))! = 0) {ShowMessage ("Take IP Address Failure");} else {edit2-> text = rip.szipaddress;}} else {showMessage ("no dial-up connection!");}}} else showmessage ("RaseNumConnections function failed!");

} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- Void __fastcall tform1 :: button3click (TOBJECT * Sender) {Word WvelSionRequested; Wsadata Wsadata; Char Chostname [64 ] = {0}; hostent * phost = null; ansistring psip; int index; wversionRequested = MakeWord (2, 0);

IF (0! = WSAStartup (WVersionRequested, & WSADATA) {wsacleanup (); showMessage ("Socket version number is wrong);}

IF (0 == gethostname (chOSTNAME, 64)) {phost = gethostByname (chOSTNAME); if (phost! = null) {for (int i = 0; phost-> h_addr_list [i]! = null; i ) {IN_ADDR * p = (in_addr *) (phost-> h_addr_list [i]); PSIP = INET_NTOA (* P); if (m_slip-> indexof (psip) <0) {edit1-> text = psip;}}} else { Washanup (); showMessage ("Take IP address failed");}} else {showMessage ("Take the machine name failed");} wsacleanup (); delete m_slip;} // ----------- -------------------------------------------------- ----------------

First click on Button1 before the Internet, and then click Butto3 to get the dial IP.

转载请注明原文地址:https://www.9cbs.com/read-23379.html

New Post(0)