程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> Visual Basic語言 >> VB綜合教程 >> PING一個IP地址(向它發送一個數據包並等待回應))

PING一個IP地址(向它發送一個數據包並等待回應))

編輯:VB綜合教程
本例演示了怎樣通過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. 下一頁:
Copyright © 程式師世界 All Rights Reserved