|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
ping an IP address
hi
i know theres lots of examples of how to do this on the net but most are far to complicated for what i need or use dll's or activeX controls. I have used Winsock to display my localIP, i simply then want to ping this IP. is this possible with Winsock or do i need to add other components such as activeX etc ? and what syntax do i need to use ping ? thanks |
|
#2
|
||||
|
||||
|
hey rix.. I just use this. It's a little function that uses an API that I picked up somewhere.
Code:
Private Declare Function GetRTTAndHopCount Lib "iphlpapi.dll" _
(ByVal lDestIPAddr As Long, _
ByRef lHopCount As Long, _
ByVal lMaxHops As Long, _
ByRef lRTT As Long) As Long
Public Function SimplePing(sIPadr As String) As Boolean
Dim lIPadr As Long
Dim lHopsCount As Long
Dim lRTT As Long
Dim lMaxHops As Long
Dim lResult As Long
Const SUCCESS = 1
lMaxHops = 4
lIPadr = inet_addr(sIPadr)
SimplePing = (GetRTTAndHopCount(lIPadr, lHopsCount, lMaxHops, lRTT) = SUCCESS)
End Function
When you call it, you just use something like this... Code:
blnPinged = simpleping("255.255.255.0")
__________________
Fisherman "Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction." - A.Einstein |
|
#3
|
|||
|
|||
|
Hi Fisherman, what's the inet_addr function?
|
|
#4
|
||||
|
||||
|
my bad, Doug - it's another Private Function using winsock
Code:
Private Declare Function inet_addr Lib "wsock32.dll" _
(ByVal cp As String) As Long
|
|
#5
|
|||
|
|||
|
Thanks! Seems to work just fine.
|
|
#6
|
|||
|
|||
|
This is whole ping function:
Code:
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()
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
Last edited by cleverpig : December 17th, 2003 at 06:39 PM. |
|
#7
|
|||
|
|||
|
Try using the [ code ] ... [ /code ] tags around your code, it will preserve indents and make the code more readable
![]() http://forums.devshed.com/misc.php?s=&action=bbcode |
|
#8
|
||||
|
||||
|
just curious, CP, have you tested the code I posted? I'm curious to know what the differences are... I guess I could test it!(there's a novel idea)
|
|
#9
|
|||
|
|||
|
Yes,I have modified it with add
Code:
.. |
|
#10
|
|||
|
|||
|
thanks all and sorry bout the delay . . .
im still having problems though, im using your function Fisherman Code:
Private Sub Command1_Click()
Dim result As Boolean
result = SimplePing("80.my IP etc")
Text1.Text = result
End Sub
this always returns false, even when i disable my firewall - any ideas ? |
|
#11
|
||||
|
||||
|
are you trying to ping an intranet or internet server?
|
|
#12
|
||||
|
||||
|
try stepping through the code with a breakpoint and determine after the call to
Code:
SimplePing = (GetRTTAndHopCount(lIPadr, lHopsCount, lMaxHops, lRTT) = SUCCESS) Determine the value of SimplePing. If it pinged successfully, then it should be True. I just tested it on my box as you have it set up, and it works fine. Have you tried pinging from DOS to see what happens? |
|
#13
|
|||
|
|||
|
thanks, thats reminded me to check the obvious. . .and sorry, it works just fine
2 days ago i was using ping and now it doesn't work !! i didnt expect a computer that could connect to the internet would be unable to ping itself, the DNS server or any other IPs i put in. But since i can ping the loop back address i have established that the code works fine ![]() solve one probem and create a new one . . . thanks mt8 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > ping an IP address |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
Linear Mode |