首页
社区
课程
招聘
[旧帖] [原创]获取外网IP(C和mfc两种实现) 0.00雪花
2012-3-28 11:21 2682

[旧帖] [原创]获取外网IP(C和mfc两种实现) 0.00雪花

2012-3-28 11:21
2682
c:
# 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);

 

MFC:
#include <afxinet.h>
void CLanChatDlg::GetNetIP()
{
 
 SetDlgItemText(IDC_NET_IP,"正在获取外网IP");
 CString strsource;
 CString Address;
 CInternetSession mySession(NULL,0);
 CHttpFile* myHttpFile=NULL;
 
 Address="http://www.ip138.com/ip2city.asp";//ip138网页
 
 myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//读取网络地址

 while(myHttpFile->ReadString(strsource))
 {  //循环读取下载来的网页文本
  //  AddToLog(strsource);
  int begin=0;
  begin=strsource.Find("[",0);
  
  if(begin!=-1)//如果找到"[", 则找"]"  中括号内的文本则是 你的外网ip
  { int end=strsource.Find("]");
  m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外网ip
 
  SetDlgItemText(IDC_NET_IP,m_internetip);//在左下角显示外网ip
  }
 }


来到本论坛没多久,发现只能在此版交流,这个论坛里肯定很多人都会写,只是希望版主能给个和大家交流的机会

[培训]《安卓高级研修班(网课)》月薪三万计划,掌握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
点赞5
打赏
分享
最新回复 (5)
雪    币: 43
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
IBMLover 2012-3-28 11:28
2
0
顶一下,大体看了一下,这是一种思路吧
雪    币: 34
活跃值: (121)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
冰河之刃 2012-3-28 11:59
3
0
就是这种思路,下载网页文件,然后截取字符串。不错。。。
雪    币: 148
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
sdasda 2012-3-28 12:05
4
0
新手,只能帮忙顶一下。。。
雪    币: 124
活跃值: (27)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
cherryEx 1 2012-3-28 15:14
5
0
不太靠谱的方法,哪天ip138改变了内容格式呢,
有更靠谱的,你google 一下stun协议 STUN (Simple Traversal of UDP through NATs (Network Address Translation))
再google 可以找到几个Public STUN servers,很久了一直能用,比ip138靠谱N多倍哦,速度还快
雪    币: 35
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
sfjandsyj 2013-10-22 15:56
6
0
虽然说ip138可能会有各种问题,但这个想法还是很不错的。学习了。
游客
登录 | 注册 方可回帖
返回