Computing.Net > Forums > Programming > VB Ping

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB Ping

Reply to Message Icon

Original Message
Name: santy
Date: March 3, 2002 at 23:28:35 Pacific
Subject: VB Ping
Comment:

I have a program to open a file from remote machine in the LAN. I would like to test if the remote machine is alive before trying to open the file. How do I use ping command to achive this? Do I have to write my own ping program or this? Can't I use existing ping program?

Thanks in anticipation
Santy


Report Offensive Message For Removal


Response Number 1
Name: Vik
Date: March 6, 2002 at 07:24:41 Pacific
Reply: (edit)

just type: ping xxxx.xxxx.xxxx.xxxx
from your command line.
where xxxx..xx is the ip adres your remote machine uses.

example: ping 127.0.0.1 is your local machine.
if the answer is reply from then it's ok.



Report Offensive Follow Up For Removal

Response Number 2
Name: Vik
Date: March 8, 2002 at 03:13:03 Pacific
Reply: (edit)

This was probably not the answer you were waiting for :=)
I was just kidding.
OK ready.
I found you a solution.
Hopefully you can use it in your app.


Report Offensive Follow Up For Removal

Response Number 3
Name: Vik.
Date: March 8, 2002 at 03:15:05 Pacific
Reply: (edit)

Copy-paste this to a separate module (ping.bas)*******************************

Option Explicit

Private Const IP_SUCCESS As Long = 0
Private Const IP_STATUS_BASE As Long = 11000
Private Const IP_BUF_TOO_SMALL As Long = (11000 + 1)
Private Const IP_DEST_NET_UNREACHABLE As Long = (11000 + 2)
Private Const IP_DEST_HOST_UNREACHABLE As Long = (11000 + 3)
Private Const IP_DEST_PROT_UNREACHABLE As Long = (11000 + 4)
Private Const IP_DEST_PORT_UNREACHABLE As Long = (11000 + 5)
Private Const IP_NO_RESOURCES As Long = (11000 + 6)
Private Const IP_BAD_OPTION As Long = (11000 + 7)
Private Const IP_HW_ERROR As Long = (11000 + 8)
Private Const IP_PACKET_TOO_BIG As Long = (11000 + 9)
Private Const IP_REQ_TIMED_OUT As Long = (11000 + 10)
Private Const IP_BAD_REQ As Long = (11000 + 11)
Private Const IP_BAD_ROUTE As Long = (11000 + 12)
Private Const IP_TTL_EXPIRED_TRANSIT As Long = (11000 + 13)
Private Const IP_TTL_EXPIRED_REASSEM As Long = (11000 + 14)
Private Const IP_PARAM_PROBLEM As Long = (11000 + 15)
Private Const IP_SOURCE_QUENCH As Long = (11000 + 16)
Private Const IP_OPTION_TOO_BIG As Long = (11000 + 17)
Private Const IP_BAD_DESTINATION As Long = (11000 + 18)
Private Const IP_ADDR_DELETED As Long = (11000 + 19)
Private Const IP_SPEC_MTU_CHANGE As Long = (11000 + 20)
Private Const IP_MTU_CHANGE As Long = (11000 + 21)
Private Const IP_UNLOAD As Long = (11000 + 22)
Private Const IP_ADDR_ADDED As Long = (11000 + 23)
Private Const IP_GENERAL_FAILURE As Long = (11000 + 50)
Private Const MAX_IP_STATUS As Long = (11000 + 50)
Private Const IP_PENDING As Long = (11000 + 255)
Private Const PING_TIMEOUT As Long = 500
Private Const WS_VERSION_REQD As Long = &H101
Private Const MIN_SOCKETS_REQD As Long = 1
Private Const SOCKET_ERROR As Long = -1
Private Const INADDR_NONE As Long = &HFFFFFFFF
Private Const MAX_WSADescription As Long = 256
Private Const MAX_WSASYSStatus As Long = 128

Private Type ICMP_OPTIONS
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type

Public Type ICMP_ECHO_REPLY
Address As Long
status As Long
RoundTripTime As Long
DataSize As Long
'Reserved As Integer
DataPointer As Long
Options As ICMP_OPTIONS
Data As String * 250
End Type

Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To MAX_WSADescription) As Byte
szSystemStatus(0 To MAX_WSASYSStatus) As Byte
wMaxSockets As Long
wMaxUDPDG As Long
dwVendorInfo As Long
End Type

Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
Private Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, ByVal DestinationAddress As Long, ByVal RequestData As String, ByVal RequestSize As Long, ByVal RequestOptions As Long, ReplyBuffer As ICMP_ECHO_REPLY, ByVal ReplySize As Long, ByVal Timeout As Long) As Long
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired As Long, lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal szHost As String, ByVal dwHostLen As Long) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal szHost As String) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (xDest As Any, xSource As Any, ByVal nbytes As Long)
Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal s As String) As Long

Public Function GetStatusCode(status As Long) As String

Dim msg As String

Select Case status
Case IP_SUCCESS: msg = "ip found"
Case INADDR_NONE: msg = "invalid ip format"
Case IP_BUF_TOO_SMALL: msg = "ip error, too small?"
Case IP_DEST_NET_UNREACHABLE: msg = "ip unreachable"
Case IP_DEST_HOST_UNREACHABLE: msg = "ip host unreachable"
Case IP_DEST_PROT_UNREACHABLE: msg = "ip target unreachable"
Case IP_DEST_PORT_UNREACHABLE: msg = "ip target port blocked"
Case IP_NO_RESOURCES: msg = "ip too few resources"
Case IP_BAD_OPTION: msg = "ip wrong option"
Case IP_HW_ERROR: msg = "ip hardwareerror"
Case IP_PACKET_TOO_BIG: msg = "ip datapacket too big"
Case IP_REQ_TIMED_OUT: msg = "ip request timed out"
Case IP_BAD_REQ: msg = "ip unknown error"
Case IP_BAD_ROUTE: msg = "ip invalid route"
Case IP_TTL_EXPIRED_TRANSIT: msg = "ip packets died"
Case IP_TTL_EXPIRED_REASSEM: msg = "ip packets died"
Case IP_PARAM_PROBLEM: msg = "ip error on pinging"
Case IP_SOURCE_QUENCH: msg = "ip error - check system"
Case IP_OPTION_TOO_BIG: msg = "ip too big"
Case IP_BAD_DESTINATION: msg = "ip target unreachable"
Case IP_ADDR_DELETED: msg = "ip address couldn´t be found"
Case IP_SPEC_MTU_CHANGE: msg = "ip error"
Case IP_MTU_CHANGE: msg = "ip unknown host-error"
Case IP_UNLOAD: msg = "ip can not be resolved"
Case IP_ADDR_ADDED: msg = "ip address unknown on host"
Case IP_GENERAL_FAILURE: msg = "ip common error"
Case IP_PENDING: msg = "ip wrong"
Case PING_TIMEOUT: msg = "ping timeout"
Case Else: msg = "unknown host error"
End Select

GetStatusCode = CStr(status) & " [ " & msg & " ]"

End Function

Public Function ping(sAddress As String, _
sDataToSend As String, _
ECHO As ICMP_ECHO_REPLY) As Long

Dim hPort As Long
Dim dwAddress As Long

dwAddress = inet_addr(sAddress)

If dwAddress INADDR_NONE Then
hPort = IcmpCreateFile()
If hPort Then
Call IcmpSendEcho(hPort, _
dwAddress, _
sDataToSend, _
Len(sDataToSend), _
0, _
ECHO, _
Len(ECHO), _
PING_TIMEOUT)

ping = ECHO.status
Call IcmpCloseHandle(hPort)
End If
Else:
ping = INADDR_NONE
End If
End Function

Public Sub SocketsCleanup()
If WSACleanup() 0 Then
MsgBox "Windows Socket Error on Socket Close", vbExclamation
End If
End Sub

Public Function SocketsInitialize() As Boolean
Dim WSAD As WSADATA
SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
End Function
*******************************************


Report Offensive Follow Up For Removal

Response Number 4
Name: Vik
Date: March 8, 2002 at 03:25:08 Pacific
Reply: (edit)

Copy paste this to a form:
and add the components to it with the correct names!!
*******************************************

Option Explicit
'this is the ping button
Private Sub Command1_Click()

Dim ECHO As ICMP_ECHO_REPLY
Dim pos As Long
Dim success As Long
'text1=ipadres; text2=message
If SocketsInitialize() Then
success = ping((Text1.Text),(Text2.Text), ECHO)
'pingstatus
Text4(0).Text = GetStatusCode(success)
'pingtime
Text4(2).Text = ECHO.RoundTripTime & " ms"
'packetsize
Text4(3).Text = ECHO.DataSize & " bytes"

If Left$(ECHO.Data, 1) Chr$(0) Then
pos = InStr(ECHO.Data, Chr$(0))
'received text
Text4(4).Text = Left$(ECHO.Data, pos - 1)
End If

SocketsCleanup
Else
MsgBox "Socket initialisation failure!!"
End If
End Sub
*******************************************
Good luck.
Let me know if it worked.


Report Offensive Follow Up For Removal

Response Number 5
Name: Anon
Date: March 12, 2002 at 03:37:05 Pacific
Reply: (edit)

Or you could just use

Shell ("ping xxx.xxx.xxx.xx")


Report Offensive Follow Up For Removal


Response Number 6
Name: Vik
Date: March 17, 2002 at 13:24:04 Pacific
Reply: (edit)

Yeh Anon,

If it was just that simple.
With your construction you can't gather the reponse information you got from the ping command within your program. With my code you can get lot's of info and use it in your code. Very nice to automatically test your connenction.


Report Offensive Follow Up For Removal

Response Number 7
Name: Allan - Surfraider..
Date: July 2, 2002 at 08:57:11 Pacific
Reply: (edit)

Works like a treat. Cheers


Report Offensive Follow Up For Removal

Response Number 8
Name: David
Date: July 9, 2002 at 22:17:14 Pacific
Reply: (edit)

ok... I'm an amateur... but I cannot get vb6 to deal with the following 2 lines...

If Left$(ECHO.Data, 1) Chr$(0) Then


If dwAddress INADDR_NONE Then

please help me make vb understand this..

Thanks!


Report Offensive Follow Up For Removal

Response Number 9
Name: Anton
Date: July 17, 2002 at 03:50:43 Pacific
Reply: (edit)

Just two typo's, should be:

If Left$(ECHO.Data, 1) Chr$(0) Then

If dwAddress INADDR_NONE Then

The code works excellent, have just incorporated it into my own version of NetSend



Report Offensive Follow Up For Removal

Response Number 10
Name: Anton
Date: July 17, 2002 at 03:56:27 Pacific
Reply: (edit)

You won't believe it! The test for "Not equal" just vanished after submitting the reply, somehow it just got lost!

Replace *Not equal* with the appropriate VB symbol:

If Left$(ECHO.Data, 1) *Not equal* Chr$(0) Then

If dwAddress *Not equal* INADDR_NONE Then



Report Offensive Follow Up For Removal

Response Number 11
Name: Marco
Date: July 17, 2002 at 06:02:54 Pacific
Reply: (edit)

Of course also the line

If WSACleanup() 0 Then

in the bas module should be changed to

If WSACleanup() *Not equal* 0 Then


Thank you.


Report Offensive Follow Up For Removal

Response Number 12
Name: Paulo
Date: July 24, 2002 at 09:19:07 Pacific
Reply: (edit)

Hi!
I have tried this sample code to send ping commands. The problem is that I can’t make it work with several ECHO Requests in the same PING command (like it is possible to do in DOS). I have tried to modify the code in several ways but no luck! I have to perform several tests regarding Round Trip Delay to a network access. Due to the larger time for the first ECHO Request, this test must be performed with at least 10 ECHO Requests in the same PING command. So you can have an idea here is a sample of the corresponding DOS command
(ping www.yahoo.com -l 32 -n 4 -w 1500)
This means that I am making a PING to Yahoo.com with 32 bytes of data, with 4 ECHO requests and a timeout of 1500 ms.
The answer should be:

Pinging www.yahoo.akadns.net [64.58.76.179] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Can anyone tell me how can I do this?

Thanks


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software