LB Booster
Programming >> BASIC code examples >> Network "Ping" Check
http://lbb.conforums.com/index.cgi?board=code&action=display&num=1438707376

Network "Ping" Check
Post by Mystic on Aug 4th, 2015, 4:56pm

Was wondering if anyone has another way to do a ping check on a machine on a network?

I know I can generate a .bat file with the ping instructions, then run the .bat from my program piping it to a data file, then...whew...reading the data file to see if the ping was successful.

Thanks,
Re: Network "Ping" Check
Post by Richard Russell on Aug 4th, 2015, 7:06pm

on Aug 4th, 2015, 4:56pm, Mystic wrote:
Was wondering if anyone has another way to do a ping check on a machine on a network?

Windows InternetCheckConnection API:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa384346.aspx

Very easily called from LB or LBB.

Richard.

Re: Network "Ping" Check
Post by Mystic on Aug 4th, 2015, 7:14pm

Awesome!

Thanks Richard!
Re: Network "Ping" Check
Post by Mystic on Aug 4th, 2015, 7:17pm

Looks like this just checks if the computer you're on can connect to the internet.

I need to ping specific machines to see if they are connected...
Re: Network "Ping" Check
Post by Richard Russell on Aug 4th, 2015, 9:09pm

on Aug 4th, 2015, 7:17pm, Mystic wrote:
Looks like this just checks if the computer you're on can connect to the internet.
I need to ping specific machines to see if they are connected...

The MSDN page I linked to in my reply states explicitly: "If lpszUrl is non-NULL, the host value is extracted from it and used to ping that specific host". Do I assume you've tried it and it doesn't work?

Richard.
Re: Network "Ping" Check
Post by Mystic on Aug 4th, 2015, 11:11pm

on Aug 4th, 2015, 9:09pm, Richard Russell wrote:
Do I assume you've tried it and it doesn't work?


No assuming... I'll write some code and play with it. I was going by...

"Allows an application to check if a connection to the Internet can be established."

Thanks again...
Re: Network "Ping" Check
Post by Mystic on Sep 10th, 2015, 9:51pm

Here's my not so elegant solution I threw together.

I'm sure many of you will snicker at my coding, but it's working for now.

Code:
[pingMachine]
    ' Create a batch file with the ping information...
    success = 0
    pingReply = 0
    success$ = ""
    OPEN "gather.bat" FOR OUTPUT AS #batch
        PRINT #batch, "@echo off"
        pingLine$ = "ping " + addressUrl$ + " > pingresults.dat"
        PRINT #batch, pingLine$
    CLOSE #batch
    GOSUB [pauseProgramShort]
    RUN "gather.bat", HIDE
    GOSUB [pauseProgram]
' Printing out ping file for testing...
    OPEN "pingresults.dat" FOR INPUT AS #pingDisplay
        WHILE NOT(EOF(#pingDisplay)<>0)
            LINE INPUT #pingDisplay, printPingLine$
            IF LEFT$(printPingLine$, 10) = "Reply from" success = success + 1
            PRINT printPingLine$
        WEND
    CLOSE #pingDisplay
    IF success >= 3 THEN
        pingReply = 1
        success$ = addressUrl$ + " SUCCESS"
        PRINT success$
        PRINT "=================================================================="
    END IF
    PRINT
    ' If there is no reply let the program know that there is activity during this hour...
    IF pingReply = 0 THEN activity = 1
RETURN