(Ping) Esta función comprueba la conexión de Red existente entre 2 máquinas, así como la velocidad de conexión. Muy útil para comprobar la existencia, o disponibilidad, de un servidor en Internet o en nuestra red privada.
http://descarga.e-mision.net/API29.zip
Este proyecto realiza la misma función que el comando "Ping" del Sistema Operativo. Informa del resultado de la conexión y del tiempo de respuesta (en milisegundos).
Este seria el código a insertar en un formulario. La función necesita la información proporcionada por Tx_Ping (dirección IP) y Tx_Enviar (texto a enviar). La estructura devuelta se muestra en el array de TextBox "Tx_Datos".
Option Explicit
Private Sub Command1_Click()
Dim EnvioICMP As ICMP_ECHO_REPLY
Dim Res As Long
Me.Caption = "Realizando Ping a la dirección " & Tx_Ping.Text
' Se llama a la función IP pasándole como parámetro la dirección IP
' el texto que queremos enviar
' y una estructura donde almacenar los datos
Res = Ping(Tx_Ping.Text, Tx_Enviar.Text, EnvioICMP)
' Muestra los resultados almacenados en la estructura
Tx_Datos(0).Text = DescripcionCodigoRespuesta(EnvioICMP.status)
Tx_Datos(1).Text = EnvioICMP.Address
Tx_Datos(2).Text = EnvioICMP.RoundTripTime & " ms"
Tx_Datos(3).Text = EnvioICMP.DataSize & " bytes"
If Left$(EnvioICMP.Data, 1) <> Chr$(0) Then
Tx_Datos(4).Text = Left$(EnvioICMP.Data, InStr(EnvioICMP.Data, Chr$(0)) - 1)
Else
Tx_Datos(4).Text = ""
End If
Tx_Datos(5).Text = EnvioICMP.DataPointer
Me.Caption = "Ping"
End Sub
Además crear un módulo donde poner el resto del código.
Option Explicit
Public Const IP_STATUS_BASE = 11000
Public Const IP_SUCCESS = 0
Public Const IP_BUF_TOO_SMALL = (11000 + 1)
Public Const IP_DEST_NET_UNREACHABLE = (11000 + 2)
Public Const IP_DEST_HOST_UNREACHABLE = (11000 + 3)
Public Const IP_DEST_PROT_UNREACHABLE = (11000 + 4)
Public Const IP_DEST_PORT_UNREACHABLE = (11000 + 5)
Public Const IP_NO_RESOURCES = (11000 + 6)
Public Const IP_BAD_OPTION = (11000 + 7)
Public Const IP_HW_ERROR = (11000 + 8)
Public Const IP_PACKET_TOO_BIG = (11000 + 9)
Public Const IP_REQ_TIMED_OUT = (11000 + 10)
Public Const IP_BAD_REQ = (11000 + 11)
Public Const IP_BAD_ROUTE = (11000 + 12)
Public Const IP_TTL_EXPIRED_TRANSIT = (11000 + 13)
Public Const IP_TTL_EXPIRED_REASSEM = (11000 + 14)
Public Const IP_PARAM_PROBLEM = (11000 + 15)
Public Const IP_SOURCE_QUENCH = (11000 + 16)
Public Const IP_OPTION_TOO_BIG = (11000 + 17)
Public Const IP_BAD_DESTINATION = (11000 + 18)
Public Const IP_ADDR_DELETED = (11000 + 19)
Public Const IP_SPEC_MTU_CHANGE = (11000 + 20)
Public Const IP_MTU_CHANGE = (11000 + 21)
Public Const IP_UNLOAD = (11000 + 22)
Public Const IP_ADDR_ADDED = (11000 + 23)
Public Const IP_GENERAL_FAILURE = (11000 + 50)
Public Const MAX_IP_STATUS = 11000 + 50
Public Const IP_PENDING = (11000 + 255)
Public Const PING_TIMEOUT = 200
Public Const WS_VERSION_REQD = &H101
Public Const WS_VERSION_MAJOR = WS_VERSION_REQD &H100 And &HFF&
Public Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Public Const MIN_SOCKETS_REQD = 1
Public Const SOCKET_ERROR = -1
Public Const MAX_WSADescription = 256
Public Const MAX_WSASYSStatus = 128
Public Type ICMP_OPTIONS
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type
Dim ICMPOPT As ICMP_OPTIONS
Public Type ICMP_ECHO_REPLY
Address As Long
status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
DataPointer As Long
Options As ICMP_OPTIONS
Data As String * 250
End Type
Public Type HOSTENT
hName As Long
hAliases As Long
hAddrType As Integer
hLen As Integer
hAddrList As Long
End Type
Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type
Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, ByVal DestinationAddress As Long, _
ByVal RequestData As String, ByVal RequestSize As Integer, ByVal RequestOptions As Long, _
ReplyBuffer As ICMP_ECHO_REPLY, ByVal ReplySize As Long, ByVal Timeout As Long) As Long
Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Declare Function gethostname Lib "WSOCK32.DLL" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal szHost As String) As Long
Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Function DescripcionCodigoRespuesta(Respuesta As Long) As String
' He traducido las cadenas más normales que pueden surgir
Dim TextoRespuesta As String
Select Case Respuesta
Case IP_SUCCESS: TextoRespuesta = "IP Localizada"
Case IP_BUF_TOO_SMALL: TextoRespuesta = "IP Buffer demasiado pequeño"
Case IP_DEST_NET_UNREACHABLE: TextoRespuesta = "ip dest net unreachable"
Case IP_DEST_HOST_UNREACHABLE: TextoRespuesta = "ip dest host unreachable"
Case IP_DEST_PROT_UNREACHABLE: TextoRespuesta = "ip dest prot unreachable"
Case IP_DEST_PORT_UNREACHABLE: TextoRespuesta = "ip dest port unreachable"
Case IP_NO_RESOURCES: TextoRespuesta = "IP Sin recursos"
Case IP_BAD_OPTION: TextoRespuesta = "ip bad option"
Case IP_HW_ERROR: TextoRespuesta = "ip hw_error"
Case IP_PACKET_TOO_BIG: TextoRespuesta = "ip packet too_big"
Case IP_REQ_TIMED_OUT: TextoRespuesta = "IP TimeOut. No se ha encontrado respuesta."
Case IP_BAD_REQ: TextoRespuesta = "ip bad req"
Case IP_BAD_ROUTE: TextoRespuesta = "ip bad route"
Case IP_TTL_EXPIRED_TRANSIT: TextoRespuesta = "ip ttl expired transit"
Case IP_TTL_EXPIRED_REASSEM: TextoRespuesta = "ip ttl expired reassem"
Case IP_PARAM_PROBLEM: TextoRespuesta = "ip param_problem"
Case IP_SOURCE_QUENCH: TextoRespuesta = "ip source quench"
Case IP_OPTION_TOO_BIG: TextoRespuesta = "ip option too_big"
Case IP_BAD_DESTINATION: TextoRespuesta = "ip bad destination"
Case IP_ADDR_DELETED: TextoRespuesta = "ip addr deleted"
Case IP_SPEC_MTU_CHANGE: TextoRespuesta = "ip spec mtu change"
Case IP_MTU_CHANGE: TextoRespuesta = "ip mtu_change"
Case IP_UNLOAD: TextoRespuesta = "ip unload"
Case IP_ADDR_ADDED: TextoRespuesta = "ip addr added"
Case IP_GENERAL_FAILURE: TextoRespuesta = "IP Error general"
Case IP_PENDING: TextoRespuesta = "ip pending"
Case PING_TIMEOUT: TextoRespuesta = "TimeOut. No se ha encontrado respuesta."
Case Else: TextoRespuesta = "Mensage respuesta desconocido"
End Select
DescripcionCodigoRespuesta = TextoRespuesta
End Function
Function HiByte(ByVal wParam As Integer)
HiByte = wParam &H100 And &HFF&
End Function
Function LoByte(ByVal wParam As Integer)
LoByte = wParam And &HFF&
End Function
Function Ping(DirIP As String, TextoEnviar As String, EnvioICMP As ICMP_ECHO_REPLY) As Long
Dim hPort As Long
' Se Inicia el Socket
Call IniciandoSocket
' Se crea el puerto para enviar el ICMP
hPort = IcmpCreateFile()
' Se realiza la llamada
If IcmpSendEcho(hPort, ConvertirTextoAEntero(DirIP), TextoEnviar, Len(TextoEnviar), 0, EnvioICMP, Len(EnvioICMP), _
PING_TIMEOUT) Then
' Respuesta correcta.
Ping = EnvioICMP.RoundTripTime
Else
Ping = EnvioICMP.status * -1
End If
' Se cierra el puerto
Call IcmpCloseHandle(hPort)
' Se cierra el socket
Call SocketsCleanup
End Function
Function ConvertirTextoAEntero(ByVal tmp As String) As Long
Dim I As Integer
Dim Parts(1 To 4) As String
I = 0
' Extrae las 4 partes de la IP en un array
While InStr(tmp, ".") > 0
I = I + 1
Parts(I) = Mid(tmp, 1, InStr(tmp, ".") - 1)
tmp = Mid(tmp, InStr(tmp, ".") + 1)
Wend
I = I + 1
Parts(I) = tmp
If I <> 4 Then
ConvertirTextoAEntero = 0
Exit Function
End If
' Construye la cadena en Hexadecimal para realizar la llamada
ConvertirTextoAEntero = Val("&H" & Right("00" & Hex(Parts(4)), 2) & Right("00" & Hex(Parts(3)), 2) & _
Right("00" & Hex(Parts(2)), 2) & Right("00" & Hex(Parts(1)), 2))
End Function
Function SocketsCleanup() As Boolean
Dim X As Long
' Se cierra el socket
X = WSACleanup()
If X <> 0 Then
MsgBox "Se ha producido el error " & Trim$(Str$(X)) & " al cerrar el Windows Sockets.", vbExclamation
SocketsCleanup = False
Else
SocketsCleanup = True
End If
End Function
Function IniciandoSocket() As Boolean
Dim WSAD As WSADATA
Dim X As Integer
Dim szLoByte As String, szHiByte As String
' Se inicia el Socket para comunicaciones
X = WSAStartup(WS_VERSION_REQD, WSAD)
If X <> 0 Then
MsgBox "El entorno de Windows Sockets para 32 bit Windows no responde."
IniciandoSocket = False
Exit Function
End If
If LoByte(WSAD.wVersion) < WS_VERSION_MAJOR Or (LoByte(WSAD.wVersion) = WS_VERSION_MAJOR And _
HiByte(WSAD.wVersion) < WS_VERSION_MINOR) Then
szHiByte = Trim$(Str$(HiByte(WSAD.wVersion)))
szLoByte = Trim$(Str$(LoByte(WSAD.wVersion)))
MsgBox "El entorno de Windows Sockets para 32 bit Windows no puede trabajar con la versión Windows Sockets " & _
szLoByte & "." & szHiByte, vbExclamation
IniciandoSocket = False
Exit Function
End If
If WSAD.wMaxSockets < MIN_SOCKETS_REQD Then
MsgBox "Esta aplicación requiere como minimo de " & Trim$(Str$(MIN_SOCKETS_REQD)) & " sockets.", vbExclamation
IniciandoSocket = False
Exit Function
End If
IniciandoSocket = True
End Function
Posted
jue, abr 15 1999 19:50
by
Maverick