C and MFC cleverly acquire two implementation methods of extranet IP _c language

Source: Internet
Author: User

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.

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.