代码备忘

 '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功能的更多相关文章

  1. ICMP协议Ping命令的应用

    ICMP的全称是 Internet Control Message Protocol ,它是TCP/IP协议族的一个子协议,属于网络层协议,用于在IP主机.路由器之间传递控制消息.从技术角度来讲,就是 ...

  2. Linux下实现ping功能

    实现ping功能,就肯定要用到ping命令,那么在Linux下ping命令为: ping [-dfnqrRv][-c<完成次数>][-i<间隔秒数>][-I<网络界面&g ...

  3. Java实现ping功能的三种方法及Linux的区分

    前大半部份转自:https://blog.csdn.net/futudeniaodan/article/details/52317650 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要 ...

  4. 你知道PING功能是怎么实现的吗

    以太网的协议有层,而每层都包含有更多的协议.所谓协议,通俗的讲就是通信双方约定的规则. 今天我们介绍一些一个听起来陌生却有很常用的协议,ICMP协议. —ICMP是(Internet Control ...

  5. 如何开放 Azure 虚拟机 Ping 功能

    前言 文章<使用 PsPing & PaPing 进行 TCP 端口连通性测试>中提到,ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器,所以不能直接使用 Ping ...

  6. 【虚拟机-网络IP】如何开放 Azure 虚拟机 Ping 功能

    前言 文章<使用 PsPing & PaPing 进行 TCP 端口连通性测试>中提到,ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器,所以不能直接使用 Ping ...

  7. TCP/IP学习笔记(4)------ICMP,ping,traceroute

    IMCP协议介绍 当传送IP数据包发生错误--比如主机不可达,路由不可达等等,ICMP协议将会把错误信息封包,然后传送回给主机.给主机一个处理错误的机会,这 也就是为什么说建立在IP层以上的协议是可能 ...

  8. 7.ICMP与ping

    ping是基于ICMP(Internet Control Message Protocol)协议工作的   ICMP报文封装在IP包里,作为“侦察兵”,非常轻巧. ICMP报文的类型有很多,最常用的是 ...

  9. 第7讲 | ICMP与ping:投石问路的侦察兵

    第7讲 | ICMP与ping:投石问路的侦察兵 ping 是基于 ICMP 协议工作的.ICMP 全称 Internet Control Message Protocol,就是互联网控制报文协议. ...

随机推荐

  1. 贴片晶振跟随WIFI渗透到我们的生活之中

    在用WIFI上网的时候很多时候会遇到上网速度不是慢就是卡,甚至是连接不上更气人的就是有时候会识别不了.我最常遇到的就是连接不上去,玩着玩着就玩不了了.起初我不知道这是什么原因,但是从事晶振行业之后我慢 ...

  2. Oracle CDC简介及异步在线日志CDC部署示例

    摘要 最近由于工作需要,花时间研究了一下Oracle CDC功能和LogMiner工具,希望能找到一种稳定.高效的技术来实现Oracle增量数据抽取功能.以下是个人的部分学习总结和部署实践. 1. O ...

  3. python之路 序列化 pickle,json

    运行代码,毫不留情地得到一个TypeError: Traceback (most recent call last): ... TypeError: <__main__.Student obje ...

  4. ArrayList实现分组功能

    这边只用2个属性来进行分组 定义Object有key与value属性 按照key的不同对于arrayList进行分组 List<Object> tempList=new ArrayList ...

  5. Eclipse debug 调试快捷键

    F3  跳到光标所在的类或方法(按Ctrl+鼠标左键同样可以实现这一功能) F5  进到函数的内部 F6  单步调试 F7  从函数中退出 F8  调到下一个断点(不能使用时应该是和有道词典的快捷键冲 ...

  6. DDD理论学习系列(6)-- 实体

    DDD理论学习系列--案例及目录 1.引言 实体对应的英语单词为Entity.提到实体,你可能立马就想到了代码中定义的实体类.在使用一些ORM框架时,比如Entity Framework,实体作为直接 ...

  7. python os模块学习

    一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的. 二.常用方法 1.os.name 输出字符串指示正在使用的平台.如果是wi ...

  8. 提高java编程质量 - (五)switch语句break不能忘以及default不同位置的用法

    先看一段代码: public class Test{ public static void main(String[] args){ System.)); } } public static Stri ...

  9. 前端基于JQgrid实现自定义列头展示

    先上效果图   因为公司项目的需要,并且公司只有我这一个能写js的前端,这个自定义展示jqgrid列选项的需求依然是交由我写,辣么就分享一下我的工作成果. //初始化函数 multiSelectCol ...

  10. 常用linux小工具介绍

    1.ctags(Generate tag files for source code)是vim下方便代码阅读的工具.尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM. ctags 最先是 ...