Checking the IP address of dial-up Internet

Source: Internet
Author: User

With the rapid popularization of the Internet in the world, the number of Internet access is more and more. Among them, the majority of people through the ordinary telephone line dial-up Internet. We know that every computer that is connected to the Internet, regardless of the way it is used, is assigned one or more independent IP addresses. For dial-up users, it is generally the case that their ISP dynamically assigns an IP address each time they dial up to the Internet, and this address may be different each time (for the most part to make the most of the limited resources). So, can we use some way to easily check the IP address of our Internet? The answer is yes. Here we will use C++builder to develop a small program to achieve this function. (Note: This program in the local area network also can run)

First, create a new project with BCB's File menu, named IPCHECK.BPR.

Then, add five labels (label) and two buttons (button) on the form FORM1, as shown in the figure.

Next, double-click the OnCreate event for the form, adding the following program:

void __fastcall tform1::formcreate (tobject *sender)
{
Wsadata Wsadata;
if (WSAStartup (Makeword (1,1), &wsadata)!=0)
{//initialization of Winsock calls
MessageBox (NULL, "wrong WinSock Version", "Error", MB_OK);
return;
}
Refresh1click (Sender); When the program starts, it checks the IP address
}
Double-click the Refresh button and add the following program
void __fastcall Tform1::refresh1click (tobject *sender)
Refreshing IP addresses
{
Char hostname[80];
Lphostent lphostent;
struct IN_ADDR addr[2];
This program assumes that the host is not a multihomed host, that is, a maximum of one network card and a dynamic IP
for (int i=0; i< 2; i++) {
memset (&addr[i],0,sizeof (in_addr));
IN_ADDR structure 0, to facilitate the completion of the following
}
if (GetHostName (hostname,sizeof (HostName)) ==socket_error)
{//Get this host name
MessageBox (NULL, "Can" t getting local host name. "," Error ", MB_OK);
return;
}
label3->caption=hostname;
Lphostent=gethostbyname (HostName)//using the resulting host name to obtain 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++)
Get an IP address from the host 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]);
}
Double-click the Refresh button and add the following program
void __fastcall Tform1::button2click (tobject *sender)
{
WSACleanup (); Release Winsock call
Close ();
}

Finally, don't forget to add #include<winsock.h> to the head of the program. Oh.

Well, the program is done, and it can be run after compiling. This procedure compiles and passes in the Chinese win95/nt4.0.

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.