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

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

2012-3-28 11:21
3254
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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://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
  }
 }</nbuflen;></urlmon.h></urlmon.h></windows.h>



[注意]看雪招聘,专注安全领域的专业人才平台!

收藏
免费 6
支持
分享
赞赏记录
参与人
雪币
留言
时间
伟叔叔
为你点赞~
2024-5-31 04:16
心游尘世外
为你点赞~
2024-4-28 01:00
飘零丶
为你点赞~
2024-3-25 00:26
QinBeast
为你点赞~
2024-3-15 00:33
shinratensei
为你点赞~
2024-1-28 03:14
PLEBFE
为你点赞~
2023-3-7 00:45
最新回复 (5)
雪    币: 43
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
顶一下,大体看了一下,这是一种思路吧
2012-3-28 11:28
0
雪    币: 34
活跃值: (121)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
就是这种思路,下载网页文件,然后截取字符串。不错。。。
2012-3-28 11:59
0
雪    币: 148
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
新手,只能帮忙顶一下。。。
2012-3-28 12:05
0
雪    币: 128
活跃值: (27)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
5
不太靠谱的方法,哪天ip138改变了内容格式呢,
有更靠谱的,你google 一下stun协议 STUN (Simple Traversal of UDP through NATs (Network Address Translation))
再google 可以找到几个Public STUN servers,很久了一直能用,比ip138靠谱N多倍哦,速度还快
2012-3-28 15:14
0
雪    币: 35
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
虽然说ip138可能会有各种问题,但这个想法还是很不错的。学习了。
2013-10-22 15:56
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册