当前位置:主页   - 电脑 - 程序设计 - VB
PING一个IP地址(向它发送一个数据包并等待回应))
来源:网络转载   作者:未知   更新时间:2009-01-14
收藏此页】    【字号    】    【打印】    【关闭
本例演示了怎样通过API的调用向一个IP地址发送一个包的数据并等待回音。
新建一个工程,添加一个标准模块,写入以下代码:

OptionExplicit

PublicConstIP_STATUS_BASE=11000
PublicConstIP_SUCCESS=0
PublicConstIP_BUF_TOO_SMALL=(11000 1)
PublicConstIP_DEST_NET_UNREACHABLE=(11000 2)
PublicConstIP_DEST_HOST_UNREACHABLE=(11000 3)
PublicConstIP_DEST_PROT_UNREACHABLE=(11000 4)
PublicConstIP_DEST_PORT_UNREACHABLE=(11000 5)
PublicConstIP_NO_RESOURCES=(11000 6)
PublicConstIP_BAD_OPTION=(11000 7)
PublicConstIP_HW_ERROR=(11000 8)
PublicConstIP_PACKET_TOO_BIG=(11000 9)
PublicConstIP_REQ_TIMED_OUT=(11000 10)
PublicConstIP_BAD_REQ=(11000 11)
PublicConstIP_BAD_ROUTE=(11000 12)
PublicConstIP_TTL_EXPIRED_TRANSIT=(11000 13)
PublicConstIP_TTL_EXPIRED_REASSEM=(11000 14)
PublicConstIP_PARAM_PROBLEM=(11000 15)
PublicConstIP_SOURCE_QUENCH=(11000 16)
PublicConstIP_OPTION_TOO_BIG=(11000 17)
PublicConstIP_BAD_DESTINATION=(11000 18)
PublicConstIP_ADDR_DELETED=(11000 19)
PublicConstIP_SPEC_MTU_CHANGE=(11000 20)
PublicConstIP_MTU_CHANGE=(11000 21)
PublicConstIP_UNLOAD=(11000 22)
PublicConstIP_ADDR_ADDED=(11000 23)
PublicConstIP_GENERAL_FAILURE=(11000 50)
PublicConstMAX_IP_STATUS=11000 50
PublicConstIP_PENDING=(11000 255)
PublicConstPING_TIMEOUT=200
PublicConstWS_VERSION_REQD=&H101
PublicConstWS_VERSION_MAJOR=WS_VERSION_REQD\&H100And&HFF&
PublicConstWS_VERSION_MINOR=WS_VERSION_REQDAnd&HFF&
PublicConstMIN_SOCKETS_REQD=1
PublicConstSOCKET_ERROR=-1

PublicConstMAX_WSADescription=256
PublicConstMAX_WSASYSStatus=128

PublicTypeICMP_OPTIONS
TtlAsByte
TosAsByte
FlagsAsByte
OptionsSizeAsByte
OptionsDataAsLong
EndType

DimICMPOPTAsICMP_OPTIONS

PublicTypeICMP_ECHO_REPLY
AddressAsLong
statusAsLong
RoundTripTimeAsLong
DataSizeAsInteger
ReservedAsInteger
DataPointerAsLong
OptionsAsICMP_OPTIONS
DataAsString*250
EndType

PublicTypeHOSTENT
hNameAsLong
hAliasesAsLong
hAddrTypeAsInteger
hLenAsInteger
hAddrListAsLong
EndType

PublicTypeWSADATA
wVersionAsInteger
wHighVersionAsInteger
szDescription(0ToMAX_WSADescription)AsByte
szSystemStatus(0ToMAX_WSASYSStatus)AsByte
wMaxSocketsAsInteger
wMaxUDPDGAsInteger
dwVendorInfoAsLong
EndType

PublicDeclareFunctionIcmpCreateFileLib"icmp.dll"()AsLong
PublicDeclareFunctionIcmpCloseHandleLib"icmp.dll"(ByValIcmpHandleAsLong)AsLong
PublicDeclareFunctionIcmpSendEchoLib"icmp.dll"(ByValIcmpHandleAsLong,ByValDestinationAddressAsLong,ByValRequestDataAsString,ByValRequestSizeAsInteger,ByValRequestOptionsAsLong,ReplyBufferAsICMP_ECHO_REPLY,ByValReplySizeAsLong,ByValTimeoutAsLong)AsLong
PublicDeclareFunctionWSAGetLastErrorLib"WSOCK32.DLL"()AsLong
PublicDeclareFunctionWSAStartupLib"WSOCK32.DLL"(ByValwVersionRequiredAsLong,lpWSADATAAsWSADATA)AsLong
PublicDeclareFunctionWSACleanupLib"WSOCK32.DLL"()AsLong
PublicDeclareFunctiongethostnameLib"WSOCK32.DLL"(ByValszHostAsString,ByValdwHostLenAsLong)AsLong
PublicDeclareFunctiongethostbynameLib"WSOCK32.DLL"(ByValszHostAsString)AsLong
PublicDeclareSubRtlMoveMemoryLib"kernel32"(hpvDestAsAny,ByValhpvSourceAsLong,ByValcbCopyAsLong)

PublicFunctionGetStatusCode(statusAsLong)AsString

DimmsgAsString

SelectCasestatus
CaseIP_SUCCESS:msg="ipsuccess"
CaseIP_BUF_TOO_SMALL:msg="ipbuftoo_small"
CaseIP_DEST_NET_UNREACHABLE:msg="ipdestnetunreachable"
CaseIP_DEST_HOST_UNREACHABLE:msg="ipdesthostunreachable"
CaseIP_DEST_PROT_UNREACHABLE:msg="ipdestprotunreachable"
CaseIP_DEST_PORT_UNREACHABLE:msg="ipdestportunreachable"
CaseIP_NO_RESOURCES:msg="ipnoresources"
CaseIP_BAD_OPTION:msg="ipbadoption"
CaseIP_HW_ERROR:msg="iphw_error"
CaseIP_PACKET_TOO_BIG:msg="ippackettoo_big"
CaseIP_REQ_TIMED_OUT:msg="ipreqtimedout"
CaseIP_BAD_REQ:msg="ipbadreq"
CaseIP_BAD_ROUTE:msg="ipbadroute"
CaseIP_TTL_EXPIRED_TRANSIT:msg="ipttlexpiredtransit"
CaseIP_TTL_EXPIRED_REASSEM:msg="ipttlexpiredreassem"
CaseIP_PARAM_PROBLEM:msg="ipparam_problem"
CaseIP_SOURCE_QUENCH:msg="ipsourcequench"
CaseIP_OPTION_TOO_BIG:msg="ipoptiontoo_big"
CaseIP_BAD_DESTINATION:msg="ipbaddestination"
CaseIP_ADDR_DELETED:msg="ipaddrdeleted"
CaseIP_SPEC_MTU_CHANGE:msg="ipspecmtuchange->


其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明