Ping Timeout

JoeCooper
07-15-2007, 02:45 PM
Hi,

i need some code in a module is best for this, that will check if a domain is up or not. eg ping google.com and return true or false (0 or 1)

thankyou

JoeCooper
07-15-2007, 04:54 PM
Private Type QOCINFO
dwSize As Long
dwFlags As Long
dwInSpeed As Long 'in bytes/second
dwOutSpeed As Long 'in bytes/second
End Type


Private Declare Function IsDestinationReachable Lib "SENSAPI.DLL" _
Alias "IsDestinationReachableA" (ByVal lpszDestination As String, _
ByRef lpQOCInfo As QOCINFO) As Long


Private Sub Form_Load()
Dim Ret As QOCINFO
Dim IP As String
Ret.dwSize = Len(Ret)
'Put desired IP
IP = "217.9.238.114"
If IsDestinationReachable(IP, Ret) = 0 Then
MsgBox "The destination cannot be reached!"
Else
MsgBox "The destination can be reached!" + vbCrLf + _
"The speed of data coming in from the destination is " + _
Format$(Ret.dwInSpeed / 1048576, "#.0") + " Mb/s," + vbCrLf + _
"and the speed of data sent to the destination is " + _
Format$(Ret.dwOutSpeed / 1048576, "#.0") + " Mb/s."
End If
End Sub


i found this code on pscode.com - how can i add a time out function. so it will timeout if it takes more than 300ms.

Turiya
07-15-2007, 08:23 PM
Using that specific API...in short, you can't.

There must be another API though...if only I had my API viewer on me, I swear there is one...somewhere...

DougT
07-16-2007, 01:35 AM
You could add a Timer to your Form and set a global boolean if it triggers. Set the Enabled property to False and the Interval property to 300mS then it goes something like:

Dim boTimeOut As Boolean

Private Sub Timer1_Timer()
boTimeOut = True
End Sub

Private Sub Form_Load()
Dim Ret As QOCINFO
Dim IP As String
Dim lngRet As Long
Ret.dwSize = Len(Ret)
'Put desired IP
IP = "217.9.238.114"
boTimout = False
Timer1.Enabled = True
lngRet = IsDestinationReachable(IP, Ret)
If lngRet = 0 Or boTimeout = True Then
MsgBox "The destination cannot be reached!"
Else
MsgBox "The destination can be reached!" + vbCrLf + _
"The speed of data coming in from the destination is " + _
Format$(Ret.dwInSpeed / 1048576, "#.0") + " Mb/s," + vbCrLf + _
"and the speed of data sent to the destination is " + _
Format$(Ret.dwOutSpeed / 1048576, "#.0") + " Mb/s."
End If
End Sub

tHuNd3r
07-16-2007, 08:52 AM
You can simulate then Ping using the API IcmpSendEcho()

http://allapi.mentalis.org/apilist/IcmpSendEcho.shtml


Const SOCKET_ERROR = 0
Private Type WSAdata
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Type Hostent
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Private Type IP_OPTION_INFORMATION
TTL As Byte
Tos As Byte
Flags As Byte
OptionsSize As Long
OptionsData As String * 128
End Type
Private Type IP_ECHO_REPLY
Address(0 To 3) As Byte
Status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
data As Long
Options As IP_OPTION_INFORMATION
End Type
Private Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" _
(ByVal HostName As String) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" _
(ByVal wVersionRequired&, _
lpWSAdata As WSAdata) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(hpvDest As Any, _
hpvSource As Any, _
ByVal cbCopy As Long)
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
Private Declare Function IcmpSendEcho Lib "ICMP" _
(ByVal IcmpHandle As Long, _
ByVal DestAddress As Long, _
ByVal RequestData As String, _
ByVal RequestSize As Integer, _
RequestOptns As IP_OPTION_INFORMATION, _
ReplyBuffer As IP_ECHO_REPLY, _
ByVal ReplySize As Long, _
ByVal TimeOut As Long) As Boolean
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Const HostName = "www.allapi.net"
Dim hFile As Long, lpWSAdata As WSAdata
Dim hHostent As Hostent, AddrList As Long
Dim Address As Long, rIP As String
Dim OptInfo As IP_OPTION_INFORMATION
Dim EchoReply As IP_ECHO_REPLY
Call WSAStartup(&H101, lpWSAdata)
If GetHostByName(HostName + String(64 - Len(HostName), 0)) <> SOCKET_ERROR Then
CopyMemory hHostent.h_name, _
ByVal GetHostByName(HostName + String(64 - Len(HostName), 0)), _
Len(hHostent)
CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
CopyMemory Address, ByVal AddrList, 4
End If
hFile = IcmpCreateFile()
If hFile = 0 Then
MsgBox "Unable to Create File Handle"
Exit Sub
End If
OptInfo.TTL = 255
If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, 2000) Then
rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3))
Else
MsgBox "Timeout"
End If
If EchoReply.Status = 0 Then
MsgBox "Reply from " + HostName + " (" + rIP + ") recieved after " + Trim$(CStr(EchoReply.RoundTripTime)) + "ms"
Else
MsgBox "Failure ..."
End If
Call IcmpCloseHandle(hFile)
Call WSACleanup
End Sub

tHuNd3r
07-16-2007, 08:54 AM
Use the ICMPSendEcho API:

http://allapi.mentalis.org/apilist/IcmpSendEcho.shtml

Regards,

00100b
07-16-2007, 09:41 AM
Two threads merged.

Please refrain from posting multiple threads concerning the same topic.

JoeCooper
07-18-2007, 04:03 PM
how do i change the timeout time? its like 10 seconds atm

Roger_Wgnr
07-18-2007, 05:52 PM
In this line If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, 2000) Then change the 2000 to the desired time out value.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum