VB6之ICMP实现ping功能
代码备忘
'code by lichmama from cnblogs.com
Private Type IPAddr
ip1 As Byte
ip2 As Byte
ip3 As Byte
ip4 As Byte
End Type Private Type IP_OPTION_INFORMATION
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type Private Type ICMP_ECHO_REPLY
Address As IPAddr
Status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
ptrData As Long
Options As IP_OPTION_INFORMATION
Data As String *
End Type Private Const REQUEST_TIMEOUT = Private Declare Sub RtlZeroMemory Lib "KERNEL32" (dest As Any, ByVal numBytes As Long)
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
Private Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, _
ByVal DestinationAddress As Long, _
ByVal RequestData As String, _
ByVal RequestSize As Long, _
ByVal RequestOptions As Long, _
ReplyBuffer As ICMP_ECHO_REPLY, _
ByVal ReplySize As Long, _
ByVal timeout As Long) As Long Private Const WS_VERSION_REQD = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD =
Private Const SOCKET_ERROR = -
Private Const WSADescription_Len =
Private Const WSASYS_Status_Len = Private Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLength As Integer
hAddrList As Long
End Type Private Type WSADATA
wversion As Integer
wHighVersion As Integer
szDescription( To WSADescription_Len) As Byte
szSystemStatus( To WSASYS_Status_Len) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpszVendorInfo As Long
End Type Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Integer, _
lpwsadata As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname As String, _
ByVal HostLen As Long) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname As String) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, _
ByVal hpvSource As Long, _
ByVal cbCopy As Long) Private Function IPString2Long(ByVal ip As String) As Long
For Each Item In Split(ip, ".")
v = Hex(Item)
If Len(v) = Then v = "" & v
hex_ = v & hex_
Next
IPString2Long = CLng("&H" & hex_)
End Function Private Function GetIpAddressByHostName(ByVal hostname As String) As String
Dim lpwsadata As WSADATA
Call WSAStartup(WS_VERSION_REQD, lpwsadata) Dim hostent_addr As Long
Dim host As HOSTENT
Dim hostip_addr As Long
Dim temp_ip_addr() As Byte
Dim i As Integer
Dim ip_address As String
hostent_addr = gethostbyname(hostname)
If hostent_addr = Then
Exit Function
End If
Call RtlMoveMemory(host, hostent_addr, LenB(host))
Call RtlMoveMemory(hostip_addr, host.hAddrList, &)
Do
ReDim temp_ip_address( To host.hLength) As Byte
Call RtlMoveMemory(temp_ip_address(), hostip_addr, host.hLength) For i = To host.hLength
ip_address = ip_address & temp_ip_address(i) & "."
Next
ip_address = Mid$(ip_address, , Len(ip_address) - ) GetIpAddressByHostName = ip_address
GoTo EXIT__
'某些域名下可能有多个地址,但是这里获取首个地址就够了
Debug.Print ip_address ip_address = ""
host.hAddrList = host.hAddrList + LenB(host.hAddrList)
Call RtlMoveMemory(hostip_addr, host.hAddrList, &)
Loop While (hostip_addr <> ) EXIT__:
Erase temp_ip_address
Call WSACleanup
End Function Private Function Ping(ByVal ip As String, ReplyBuff As ICMP_ECHO_REPLY) As Long
Dim IcmpHandle As Long IcmpHandle = IcmpCreateFile()
If IcmpHandle Then
Dim addr As Long
Dim sendbuff As String
Dim timeout As Long timeout = 'set the timeout 1000ms
sendbuff = String(, &HFF)
addr = IPString2Long(ip)
Call RtlZeroMemory(ByVal VarPtr(ReplyBuff), Len(ReplyBuff))
Call IcmpSendEcho(IcmpHandle, addr, sendbuff, Len(sendbuff), &, ReplyBuff, Len(ReplyBuff), timeout)
Call IcmpCloseHandle(IcmpHandle)
Ping = ReplyBuff.Status
Else
'icmp initailize fail
Ping = -
End If
End Function Private Sub Command1_Click()
Dim ip As String
Dim ier As ICMP_ECHO_REPLY
ip = GetIpAddressByHostName("www.baidu.com")
Call Ping(ip, ier)
Debug.Print "Reply from " & ip & ": bytes=" & ier.DataSize & " times=" & ier.RoundTripTime & " ttl=" & ier.Options.Ttl
End Sub
Reply from 61.135.169.105: bytes= times= ttl=
Reply from 61.135.169.105: bytes= times= ttl=
Reply from 61.135.169.105: bytes= times= ttl=
VB6之ICMP实现ping功能的更多相关文章
- ICMP协议Ping命令的应用
ICMP的全称是 Internet Control Message Protocol ,它是TCP/IP协议族的一个子协议,属于网络层协议,用于在IP主机.路由器之间传递控制消息.从技术角度来讲,就是 ...
- Linux下实现ping功能
实现ping功能,就肯定要用到ping命令,那么在Linux下ping命令为: ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面&g ...
- Java实现ping功能的三种方法及Linux的区分
前大半部份转自:https://blog.csdn.net/futudeniaodan/article/details/52317650 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要 ...
- 你知道PING功能是怎么实现的吗
以太网的协议有层,而每层都包含有更多的协议.所谓协议,通俗的讲就是通信双方约定的规则. 今天我们介绍一些一个听起来陌生却有很常用的协议,ICMP协议. ICMP是(Internet Control ...
- 如何开放 Azure 虚拟机 Ping 功能
前言 文章<使用 PsPing & PaPing 进行 TCP 端口连通性测试>中提到,ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器,所以不能直接使用 Ping ...
- 【虚拟机-网络IP】如何开放 Azure 虚拟机 Ping 功能
前言 文章<使用 PsPing & PaPing 进行 TCP 端口连通性测试>中提到,ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器,所以不能直接使用 Ping ...
- TCP/IP学习笔记(4)------ICMP,ping,traceroute
IMCP协议介绍 当传送IP数据包发生错误--比如主机不可达,路由不可达等等,ICMP协议将会把错误信息封包,然后传送回给主机.给主机一个处理错误的机会,这 也就是为什么说建立在IP层以上的协议是可能 ...
- 7.ICMP与ping
ping是基于ICMP(Internet Control Message Protocol)协议工作的 ICMP报文封装在IP包里,作为“侦察兵”,非常轻巧. ICMP报文的类型有很多,最常用的是 ...
- 第7讲 | ICMP与ping:投石问路的侦察兵
第7讲 | ICMP与ping:投石问路的侦察兵 ping 是基于 ICMP 协议工作的.ICMP 全称 Internet Control Message Protocol,就是互联网控制报文协议. ...
随机推荐
- List集合数据太多进行分批,List的subList方法应用
List<String> mStrings=new ArrayList<>(); //初始化 for (int i = 0; i < 1020; i++) { mStri ...
- Dorado7与@ManyToMany的矛盾分析
在Dorado7开发时,通过UpdateAction提交数据,默认只会提交DataSet中DirtyTree,这样如果子对象数据未发生修改时是不会提交的,后台拿到的子对象为空.如果两对象之间,通过JP ...
- 重构了cxlt-vue2-toastr插件
距离上篇文章已经过去一个多月了,期间有很多想法,但时间真不是想挤就能挤出来的.其实这段时间我就做了一件事,一个小程序的项目,已上线半月有余,也迭代了几个版本,现在还在不断完善. 先说点题外话,我们做了 ...
- web工作过程
了解浏览器与服务器进行通信和访问的过程 打开浏览器-输入URL地址 当我们访问一个网页时,如http://www.baidu.com,这个网址包含四个部分的内容: 1.第一部分:协议类型:这里是htt ...
- Boosting决策树:GBDT
GBDT (Gradient Boosting Decision Tree)属于集成学习中的Boosting流派,迭代地训练基学习器 (base learner),当前基学习器依赖于上一轮基学习器的学 ...
- JavaScript 扫描枪使用(一)
JavaScript 扫描枪应用(一)com.js为主要的代码实现,test.html文件为测试的页面,其中包括了com.js文件中方法的调用.以下为测试成功代码:com.js //com.js /* ...
- Docker化tomcat 并且使用maven热部署
今天没事干 就把自己的tomcat docker化并且使用maven实现热部署 特别记录一下~ 1.首先进行centos7的安装 就不在记录了 注意的是最好使用centos7 因为docker 依赖 ...
- linux系统必学-部分链接
vi操作笔记 vim操作笔记 好网站,临时收藏 SEO十万个为什么 9RIA.com天地会--中国最大的专业Flash论坛 ===互联网=============================== ...
- 最全面的Java字节byte操作,处理Java基本数据的转换及进制转换操作工具,流媒体及java底层开发项目常用工具类
前言:用于处理Java基本数据的转换及进制转换操作工具 一.实现功能 1.int与byte互转 2.int与byte[]互转 3.short与byte互转 4.short与byte[]互转 5.16位 ...
- springMvc <form action="">提交跳转路径问题
表单提交后,action的URL写的是, login/login.do,每次跳转后都变成login/ login/login.do,很显然是相对路径没有搞清楚. 应该弄清楚相对路径,并在JSP代码中加 ...