In this paper, two examples of C and MFC are used to detail the two implementation methods of IP, the implementation code is as follows:
MFC language implementation Gets the extranet IP:
# include <windows.h>
# include <urlmon.h>
# pragma comment (lib, "URLMON.lib")
void Main ()
{
urldownloadtofile (NULL, "http://www.ip138.com/ip2city.asp", "Ip.txt", 0,null);
System ("Notepad ip.txt");
}
#include <urlmon.h>
#pragma comment (lib, "Urlmon.lib")
char Buf[max_path] = {0};
Char chtempip[128];
Char chip[64];
Urldownloadtofile (0, "http://www.ip138.com/ip2city.asp", "C:\\1.ini", 0,null);
FILE *fp=fopen ("C:\\1.ini", "R");
if (FP!= NULL)
{
fseek (FP, 0, Seek_set);
Fread (BUF,1,256,FP);
Fclose (FP);
char* iindex = strstr (buf, "[");
if (iindex)
{
sprintf (Chtempip, "%s", iindex);
int nbuflen = strlen (CHTEMPIP);
for (int i =0; i<nbuflen; i++)
{
Chip[i] = chtempip[i+1];
if (chtempip[i] = = ' = ')
{
chip[i-1] = ';
DeleteFile ("C:\\1.ini");
Continue
}
}}} MessageBox (ChIP);
C implementation to obtain the extranet IP:
#include <afxinet.h>
void Clanchatdlg::getnetip ()
{
setdlgitemtext (idc_net_ip, "getting extranet IP");
CString strsource;
CString address;
CInternetSession mysession (null,0);
chttpfile* Myhttpfile=null;
Address= "http://www.ip138.com/ip2city.asp";//ip138 page
myhttpfile= (chttpfile*) Mysession.openurl (address); Read the network address while
(myhttpfile->readstring (strsource))
{//loop read the downloaded page text
//Addtolog (strsource);
int begin=0;
Begin=strsource. Find ("[", 0);
if (begin!=-1)//If found "[", then look for "]" in parentheses in the text is your extranet IP
{int end=strsource. Find ("]");
M_internetip=strsource. Mid (begin+1,end-begin-1);//Extract the extranet IP
setdlgitemtext (idc_net_ip,m_internetip);//In the lower left corner of the network IP
}
}
These two examples are through the ip138 Web site to query the extranet IP, interested readers can also according to their own preferences to change the code to make it more perfect.