With the rapid INTERNET worldwide popularity, the number of Internet more and more. Among them, the vast majority of ordinary telephone line through dial-up. We know that each Internet computer, whether the manner in which the Internet is assigned one or more independent of IP addresses is exactly the same. For the dial-up Internet users, usually by each in its dial-up ISP to dynamically assign a IP address, this address may vary each time (mainly due to full use of limited resources). Then, by some means we can check at any time convenient to know when your Internet IP address? The answer is yes. Here we use the C + + BUILDER preparation of a small program to achieve this functionality. (Note: This program can also run the LAN)
---- First of all, the FILE menu with BCB's New Application to create a new project, named Ipcheck.bpr.
---- Then, in the form of five labels to add FORM1 (LABEL) and two buttons (BUTTON), as shown.
---- Next, double-click the form's OnCreate event, in which to add the following procedures:
void __fastcall TForm1:: FormCreate (Tobject * Sender)
(
WSAData wsaData;
if (WSAStartup (MAKEWORD (1,1), & wsaData)! = 0)
(File / / initialize WINSOCK call
MessageBox (NULL, "Wrong WinSock
Version "," Error ", MB_OK);
return;
)
Refresh1Click (Sender); file / / program start, the tone Detecting IP address
)
Then double-click the Refresh button, add the following procedure in which the
void __fastcall TForm1:: Refresh1Click (Tobject * Sender)
file / / refresh the IP address
(
char HostName [80];
LPHOSTENT lpHostEnt;
struct in_addr addr [2];
file / / this procedure assume that the host is not multi-homed host, or at most
/ / A network card and a dynamic IP
for (int I = 0; I <2; I + +) (
memset (& addr [I], 0, sizeof (in_addr));
file / / clear on the in_addr structure of 0, in order to facilitate the back fill
)
if (gethostname (HostName, sizeof (HostName)) == SOCKET_ERROR)
(/ / Get the host name
MessageBox (NULL, "Can''t getting local host name.", "Error", MB_OK);
return;
)
Label3-> Caption = HostName;
lpHostEnt = gethostbyname (HostName); / / get the host name used to be the host structure
if (! lpHostEnt) (
MessageBox (NULL, "Yow! Bad host lookup.", "Error", MB_OK);
return;
)
for (int I = 0; lpHostEnt-> h_addr_list [I]! = 0; I + +)
file / / from the host address to get IP address table
(
memcpy (& addr [I], lpHostEnt-> h_addr_list [I], sizeof (in_addr));
)
Label4-> Caption = inet_ntoa (addr [0]);
Label5-> Caption = inet_ntoa (addr [1]);
)
Then double-click the Refresh button, add the following procedure in which the
void __fastcall TForm1:: Button2Click (Tobject * Sender)
(
WSACleanup (); file / / release WINSOCK call
Close ();
)
---- Finally, do not forget to head in the process with # include
---- Well, the program is complete, you can run the compiled. The program compiled by the Chinese WIN95/NT4.0