C和MFC巧妙獲取外網(wǎng)IP的兩種實(shí)現(xiàn)方法
更新時(shí)間:2014年07月21日 17:41:00 投稿:shichen2014
這篇文章主要介紹了C和MFC巧妙獲取外網(wǎng)IP的兩種實(shí)現(xiàn)方法,功能非常的實(shí)用,需要的朋友可以參考下
本文以C與MFC的兩個(gè)實(shí)例詳述了取外網(wǎng)IP的兩種實(shí)現(xiàn)方法,具體實(shí)現(xiàn)代碼如下:
MFC語言實(shí)現(xiàn)獲取外網(wǎng)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] = '\0'; DeleteFile("c:\\1.ini"); continue; } } } } MessageBox(chIP);
C實(shí)現(xiàn)獲取外網(wǎng)IP:
#include <afxinet.h> void CLanChatDlg::GetNetIP() { SetDlgItemText(IDC_NET_IP,"正在獲取外網(wǎng)IP"); CString strsource; CString Address; CInternetSession mySession(NULL,0); CHttpFile* myHttpFile=NULL; Address="http://www.ip138.com/ip2city.asp";//ip138網(wǎng)頁 myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//讀取網(wǎng)絡(luò)地址 while(myHttpFile->ReadString(strsource)) { //循環(huán)讀取下載來的網(wǎng)頁文本 // AddToLog(strsource); int begin=0; begin=strsource.Find("[",0); if(begin!=-1)//如果找到"[", 則找"]" 中括號(hào)內(nèi)的文本則是 你的外網(wǎng)ip { int end=strsource.Find("]"); m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外網(wǎng)ip SetDlgItemText(IDC_NET_IP,m_internetip);//在左下角顯示外網(wǎng)ip } }
這兩個(gè)示例都是通過ip138網(wǎng)站來查詢外網(wǎng)IP的,感興趣的讀者也可以根據(jù)自己的喜好改動(dòng)一下代碼,使之更加完善。
您可能感興趣的文章:
- MFC中動(dòng)態(tài)創(chuàng)建控件以及事件響應(yīng)實(shí)現(xiàn)方法
- MFC創(chuàng)建模態(tài)對(duì)話框和非模態(tài)對(duì)話框的方法
- MFC設(shè)置對(duì)話框焦點(diǎn)的方法簡述
- MFC擴(kuò)展DLL中導(dǎo)出類和對(duì)話框的實(shí)現(xiàn)方法
- MFC程序?qū)ξ募奶幚矸椒?/a>
- VC MFC非模態(tài)對(duì)話框的實(shí)現(xiàn)方法
- MFC自定義消息的實(shí)現(xiàn)方法
- MFC中exe圖標(biāo)修改的方法
- 新舊MFC版本實(shí)現(xiàn)CEdit透明的2種方法的實(shí)例代碼
- MFC創(chuàng)建右鍵彈出菜單的方法
相關(guān)文章
C語言中break與continue的用法和區(qū)別詳解
當(dāng)我們使用while或for循環(huán)時(shí),如果想提前結(jié)束循環(huán)(在不滿足結(jié)束條件的情況下結(jié)束循環(huán)),可以使用break或continue關(guān)鍵字,這篇文章主要給大家介紹了關(guān)于C語言中break與continue的用法和區(qū)別的相關(guān)資料,需要的朋友可以參考下2021-10-10