代码备忘

  1 'code by lichmama from cnblogs.com
  2 Private Type IPAddr
  3     ip1 As Byte
  4     ip2 As Byte
  5     ip3 As Byte
  6     ip4 As Byte
  7 End Type
  8
  9 Private Type IP_OPTION_INFORMATION
 10     Ttl As Byte
 11     Tos As Byte
 12     Flags As Byte
 13     OptionsSize As Byte
 14     OptionsData As Long
 15 End Type
 16
 17 Private Type ICMP_ECHO_REPLY
 18     Address As IPAddr
 19     Status As Long
 20     RoundTripTime As Long
 21     DataSize As Integer
 22     Reserved As Integer
 23     ptrData As Long
 24     Options As IP_OPTION_INFORMATION
 25     Data As String * 250
 26 End Type
 27
 28 Private Const REQUEST_TIMEOUT = 11010
 29
 30 Private Declare Sub RtlZeroMemory Lib "KERNEL32" (dest As Any, ByVal numBytes As Long)
 31 Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
 32 Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
 33 Private Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, _
 34     ByVal DestinationAddress As Long, _
 35     ByVal RequestData As String, _
 36     ByVal RequestSize As Long, _
 37     ByVal RequestOptions As Long, _
 38     ReplyBuffer As ICMP_ECHO_REPLY, _
 39     ByVal ReplySize As Long, _
 40     ByVal timeout As Long) As Long
 41
 42 Private Const WS_VERSION_REQD = &H101
 43 Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF&
 44 Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
 45 Private Const MIN_SOCKETS_REQD = 1
 46 Private Const SOCKET_ERROR = -1
 47 Private Const WSADescription_Len = 256
 48 Private Const WSASYS_Status_Len = 128
 49
 50 Private Type HOSTENT
 51     hName As Long
 52     hAliases As Long
 53     hAddrType As Integer
 54     hLength As Integer
 55     hAddrList As Long
 56 End Type
 57
 58 Private Type WSADATA
 59     wversion As Integer
 60     wHighVersion As Integer
 61     szDescription(0 To WSADescription_Len) As Byte
 62     szSystemStatus(0 To WSASYS_Status_Len) As Byte
 63     iMaxSockets As Integer
 64     iMaxUdpDg As Integer
 65     lpszVendorInfo As Long
 66 End Type
 67
 68 Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
 69 Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Integer, _
 70     lpwsadata As WSADATA) As Long
 71 Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
 72 Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname As String, _
 73     ByVal HostLen As Long) As Long
 74 Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname As String) As Long
 75 Private Declare Sub RtlMoveMemory Lib "KERNEL32" (hpvDest As Any, _
 76     ByVal hpvSource As Long, _
 77     ByVal cbCopy As Long)
 78
 79 Private Function IPString2Long(ByVal ip As String) As Long
 80     For Each Item In Split(ip, ".")
 81         v = Hex(Item)
 82         If Len(v) = 1 Then v = "0" & v
 83         hex_ = v & hex_
 84     Next
 85     IPString2Long = CLng("&H" & hex_)
 86 End Function
 87
 88 Private Function GetIpAddressByHostName(ByVal hostname As String) As String
 89     Dim lpwsadata As WSADATA
 90     Call WSAStartup(WS_VERSION_REQD, lpwsadata)
 91
 92     Dim hostent_addr As Long
 93     Dim host As HOSTENT
 94     Dim hostip_addr As Long
 95     Dim temp_ip_addr() As Byte
 96     Dim i As Integer
 97     Dim ip_address As String
 98     hostent_addr = gethostbyname(hostname)
 99     If hostent_addr = 0 Then
100         Exit Function
101     End If
102     Call RtlMoveMemory(host, hostent_addr, LenB(host))
103     Call RtlMoveMemory(hostip_addr, host.hAddrList, 4&)
104     Do
105         ReDim temp_ip_address(1 To host.hLength) As Byte
106         Call RtlMoveMemory(temp_ip_address(1), hostip_addr, host.hLength)
107
108         For i = 1 To host.hLength
109                ip_address = ip_address & temp_ip_address(i) & "."
110         Next
111         ip_address = Mid$(ip_address, 1, Len(ip_address) - 1)
112
113         GetIpAddressByHostName = ip_address
114         GoTo EXIT__
115         '某些域名下可能有多个地址,但是这里获取首个地址就够了
116         Debug.Print ip_address
117
118         ip_address = ""
119         host.hAddrList = host.hAddrList + LenB(host.hAddrList)
120         Call RtlMoveMemory(hostip_addr, host.hAddrList, 4&)
121     Loop While (hostip_addr <> 0)
122
123 EXIT__:
124     Erase temp_ip_address
125     Call WSACleanup
126 End Function
127
128 Private Function Ping(ByVal ip As String, ReplyBuff As ICMP_ECHO_REPLY) As Long
129     Dim IcmpHandle As Long
130
131     IcmpHandle = IcmpCreateFile()
132     If IcmpHandle Then
133         Dim addr As Long
134         Dim sendbuff As String
135         Dim timeout As Long
136
137         timeout = 1000 'set the timeout 1000ms
138         sendbuff = String(32, &HFF)
139         addr = IPString2Long(ip)
140         Call RtlZeroMemory(ByVal VarPtr(ReplyBuff), Len(ReplyBuff))
141         Call IcmpSendEcho(IcmpHandle, addr, sendbuff, Len(sendbuff), 0&, ReplyBuff, Len(ReplyBuff), timeout)
142         Call IcmpCloseHandle(IcmpHandle)
143         Ping = ReplyBuff.Status
144     Else
145         'icmp initailize fail
146         Ping = -1
147     End If
148 End Function
149
150 Private Sub Command1_Click()
151     Dim ip As String
152     Dim ier As ICMP_ECHO_REPLY
153     ip = GetIpAddressByHostName("www.baidu.com")
154     Call Ping(ip, ier)
155     Debug.Print "Reply from " & ip & ": bytes=" & ier.DataSize & " times=" & ier.RoundTripTime & " ttl=" & ier.Options.Ttl
156 End Sub

  

Reply from 61.135.169.105: bytes=32 times=31 ttl=55
Reply from 61.135.169.105: bytes=32 times=29 ttl=55
Reply from 61.135.169.105: bytes=32 times=28 ttl=55

转载于:https://www.cnblogs.com/lichmama/p/3826565.html

VB6之ICMP实现ping功能相关推荐

  1. Linux系统实现ICMP ping功能,并计算时延

    <Linux实现ICMP PING代码> <C语言实现ICMP协议,并进行PING测试> <本文源代码下载> 目录 源代码 icmpping.c icmpping. ...

  2. 【计算机网络】网络层 : ICMP 协议 ( ICMP 差错报文 | 差错报文分类 | ICMP 询问报文 | ICMP 应用 | Ping | Traceroute )

    文章目录 一.ICMP 协议 二.ICMP 协议 简介 三.ICMP 五种差错报告报文 四.ICMP 差错报文形成 五.ICMP 差错报文 不发送 情形 六.ICMP 询问报文 七.ICMP 应用 一 ...

  3. C语言实现ICMP协议Ping命令

    From: http://www.360doc.com/content/12/0429/19/1317564_207540510.shtml 大部分人用ping命令只是作为查看另一个系统的网络连接是否 ...

  4. java ping 实现的_java实现ping功能

    一.纯Java实现ICMP的ping命令 import java.io.*; import java.net.*; import java.nio.channels.*; import java.ut ...

  5. ICMP协议Ping命令的应用

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

  6. FPGA纯verilog实现UDP通信,三速网自协商仲裁,动态ARP和Ping功能,提供工程源码和技术支持

    目录 1.前言 2.我这里已有的UDP方案 3.UDP详细设计方案 MAC层发送 MAC发送模式 ARP发送 IP层发送 IP发送模式 UDP发送 MAC层接收 ARP接收 IP层接收 UDP接收 S ...

  7. C++ 实现 ping 功能 域名(URL)解析实际 IP地址

    1.简述 一般情况下,我们想知道在当前电脑设备环境下,某一个网址能不能访问,最简单的方法是win + R 键 ,输入cmd,召唤cmd命令行程序,然后直接用ping命令 + 网址 来看返回的结果,那么 ...

  8. 网络协议 5 - ICMP 与 ping:投石问路的侦察兵

    网络协议 5 - ICMP 与 ping:投石问路的侦察兵 原文:网络协议 5 - ICMP 与 ping:投石问路的侦察兵     日常开发中,我们经常会碰到查询网络是否畅通以及域名对应 IP 地址 ...

  9. c语言实现icmp协议ping命令,利用ICMP协议实现ping命令

    一.实现原理 ping利用ICMP协议包来侦测另一个主机是否可达.Ping的原理是使用了类型码为8的ICMP回送请求包,收到请求的主机则用类型码为0的ICMP回应报文.如果应答包和请求包的标示号.序号 ...

最新文章

  1. Call to undefined function mysql_connect()
  2. Android开发--图形图像与动画(二)--Animation实现图像的 渐变、缩放、位移、旋转
  3. 通过SSIS的“查找”组件进行不同数据源之间数据的合并操作
  4. 毛笔笔锋算法IOS版
  5. MySQL中的+作用,进行字符串的拼接用concat
  6. python网络编程---TCP服务器
  7. PopTheBubble —测量媒体偏差的产品创意
  8. java 栈空间_初学JAVA——栈空间堆空间的理解
  9. 文献阅读 | Epigenetics in ENS development and Hirschsprung disease
  10. maven导入模块后重新导入有两个一样的_Testng在Maven中配置Reportng中文乱码解决及笔记记录...
  11. Oracle RAC的五大优势及其劣势
  12. [JZOJ100026]图--倍增
  13. 小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_33、SpringBoot2.x整合Mybatis3.x注解实战...
  14. 后台管理系统下载excel文件
  15. 计算机技术需要学单片机吗,手把手教你学51单片机
  16. 零基础学模拟电路--3.同相放大器、反相放大器、加法器、减法器、积分器、微分器
  17. Expandable Button
  18. 将macOS Big Sur降级到Catalina或Mojave的方法步骤
  19. Jmeter进行稳定性测试
  20. React - 函数作为子组件

热门文章

  1. sql server 日志清理语句
  2. 优先队列与Heap的小结
  3. jquery 搜索框自动提示
  4. ueditor分布式部署
  5. 第一次正面交火!版主说博文里有广告!
  6. Bootstrap3.0入门学习系列规划[持续更新]
  7. LeetCode 5381. 查询带键的排列
  8. 个性化推荐算法-协同过滤
  9. win7+GPU运行py-faster-rcnn
  10. Web前端Javascript笔记(5)事件