根据域名获取IP地址

本文介绍了一种方法,通过使用Windows Sockets API,从指定的域名中获取其对应的IP地址,并展示了如何在代码中实现这一功能。
 1 $PBExportHeader$n_cst_ip.sru
 2 forward
 3 global type n_cst_ip from nonvisualobject
 4 end type
 5 type s_wsadata from structure within n_cst_ip
 6 end type
 7 type os_hostent from structure within n_cst_ip
 8 end type
 9 end forward
10 
11 type s_wsadata from structure
12     unsignedinteger        version
13     unsignedinteger         highversion 
14     character        discription[257]
15     character        systemstatus[129]
16     unsignedinteger        maxsockets
17     unsignedinteger        maxupddg
18     string         vendorinfo
19 end type
20 
21 type os_hostent from structure
22     long        hname
23     long        haliases
24     integer        haddrtype
25     integer        hlen
26     long        haddrlist
27 end type
28 
29 global type n_cst_ip from nonvisualobject autoinstantiate
30 end type
31 
32 type prototypes
33 FUNCTION  int  WSAStartup(  uint  UIVersionRequested,  ref  s_WSAData  lpWSAData  )    library  "wsock32.dll" 
34 FUNCTION  int  WSACleanup()  library  "wsock32.dll"  
35 FUNCTION  int  gethostname  (  ref  blob  name,  int  namelen  )  library  "wsock32.dll"  
36 FUNCTION  uLong  gethostbyname(ref blob  sHost)  Library  "wsock32.dll"  
37 FUNCTION  uLong  RtlMoveMemory(ref  os_hostent  hpvDest,long  hpvSource,long  cbCopy)  Library  "kernel32.dll"  
38 FUNCTION  uLong  RtlMoveMemory(ref  uLong  hpvDest,long  hpvSource,long  cbCopy)  Library  "kernel32.dll" 
39 end prototypes
40 
41 forward prototypes
42 public function boolean of_getdnsip (string as_dns, ref string as_ipadress[])
43 end prototypes
44 
45 public function boolean of_getdnsip (string as_dns, ref string as_ipadress[]);//函  数:of_GetDNSIp()
46 //功  能:得到指定域名的IP
47 //参  数:string as_DNS //域名
48 //        ref  string as_ipadress[] //返回的ip数组
49 //返回值:Boolean True/False   
50 //修改人:yyoinge
51 
52 string ls_IpAddress[]
53 int li_IP_Count
54 s_wsadata l_WSAData
55 ULong ll_addr, ll_IP, ll_ipaddr, ll_stringip  
56 int li_version = 257
57 blob{128} lb_HostName
58 ulong ll_addr_list
59 os_hostent lstr_hostent
60 
61 if wsastartup(li_version, l_WSAData)  =  0 then
62     lb_hostname = blob('www.baidu.com')
63     ll_addr  =  gethostbyname(lb_HostName)
64     RtlMoveMemory(lstr_hostent, ll_addr, 16)
65     ll_addr_list  =  lstr_hostent.haddrlist
66  
67     li_IP_Count  =  0 
68     Do While True  
69         RtlMoveMemory(ll_IPAddr, ll_addr_list, 4  )
70         If ll_IPAddr = 0 Then Exit  
71             li_IP_Count = li_IP_Count + 1
72             RtlMoveMemory(ll_StringIP,  ll_IPAddr, 1)
73             ls_IpAddress[li_IP_Count] = string(ll_StringIP) + "."
74             RtlMoveMemory(ll_StringIP, ll_IPAddr + 1, 1)
75             ls_IpAddress[li_IP_Count] = ls_IpAddress[li_IP_Count] + string(ll_StringIP) + "."
76             RtlMoveMemory(ll_StringIP, ll_IPAddr + 2, 1)
77             ls_IpAddress[li_IP_Count] = ls_IpAddress[li_IP_Count] + string(ll_StringIP)+ "."
78             RtlMoveMemory(ll_StringIP, ll_IPAddr + 3, 1)
79             ls_IpAddress[li_IP_Count] = ls_IpAddress[li_IP_Count] + string(ll_StringIP)
80             ll_addr_list = ll_addr_list + 4
81     Loop
82     WSACleanup() 
83 End if
84 
85 as_ipadress = ls_IpAddress
86 
87 return upperbound(as_ipadress) > 0 //取到IP则返回true
88 end function
89 
90 on n_cst_ip.create
91 call super::create
92 TriggerEvent( this, "constructor" )
93 end on
94 
95 on n_cst_ip.destroy
96 TriggerEvent( this, "destructor" )
97 call super::destroy
98 end on
1 程序中这样调用:
2 C/C++ coden_cst_ip ln
3 string ls_dsn = 'www.baidu.com',ls[]
4 if ln.of_getdnsip(ls_dsn, ls) then
5     long i
6     for i = 1 to upperbound(ls)
7         messagebox('提示', '域名【' + ls_dsn + '】对应的IP地址(' + string(i) + ')为:~r~n~r~n' + ls[i])
8     next
9 end if

 

转载于:https://www.cnblogs.com/cwbo-win/articles/3393967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值