Liberty BASIC Community Forum
« Checking if another machine is connected »

Welcome Guest. Please Login or Register.
Sep 19th, 2017, 1:50pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Checking if another machine is connected  (Read 517 times)
Mystic
Junior Member
ImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 97
xx Checking if another machine is connected
« Thread started on: Sep 11th, 2015, 4:01pm »

Thought I would share my snippet of code that I am using in my program that tests whether or not another computer is connected to the network (basically "pinging" the device).

This creates a batch file, runs the batch file piping the results to a data file, reads the data file to see if the ping was successful, then returns to results.

It is also printing some of the lines from the data file so I can "see" what it is reading.

I welcome all comments or other ideas...

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
 
User IP Logged

- Rick
Programming BASIC since the 80's starting with a TRS-80.
Rui Branco
Senior Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 370
xx Re: Checking if another machine is connected
« Reply #1 on: Feb 27th, 2016, 09:43am »

this is my way.
I took the example of Alyce "WaitForSingleObject" and build a silent ping function

Code:
global ProcessResult, PingResult, LogExists

callpingfunction = ping("google.pt")

print ProcessResult
print LogExists
print PingResult

end



Function ping(address$)

    SEEMASKNOCLOSEPROCESS = 64

    Struct s, cbSize as ulong, fMask as ulong, hwnd as ulong,_
        lpVerb$ as ptr, lpFile$ as ptr, lpParameters$ as ptr ,_
        lpDirectory$ as ptr, nShow as long, hInstApp as ulong,_
        lpIDList as long, lpClass as long, hkeyClass as ulong,_
        dwHotKey as ulong, hIcon as ulong, hProcess as ulong

    s.cbSize.struct=len(s.struct)
    s.fMask.struct=SEEMASKNOCLOSEPROCESS
    s.hwnd.struct=0
    s.lpVerb$.struct="open"
    s.lpFile$.struct="cmd.exe"
    s.lpParameters$.struct="/c ping " + address$ + "> log.txt"
    s.lpDirectory$.struct=DefaultDir$
    s.nShow.struct=_SW_HIDE

    calldll #shell32 , "ShellExecuteExA",_
        s as struct,_
        r as long

    if r<>0 then
        hProcess=s.hProcess.struct
    else
        ProcessResult=0
        exit function
    end if

    waitResult=-1
    while waitResult<>0
        calldll #kernel32, "WaitForSingleObject",_
            hProcess as long,_
            0 as long,_
            waitResult as long
    wend

    ProcessResult=1

    dim info$(10, 10)
    dim line$(3000)
    if fileExists(DefaultDir$, "log.txt") then
        open "log.txt" for input as #log
            while not(eof(#log))
                line input #log, line$(i)
                i=i+1
            wend
        close #log
        if left$(line$(3), 10) = "Reply from" then
                    PingResult=1
                else
                    PingResult=0
        end if
        LogExists=1
    else
        LogExists=0
    end if

End Function

Function fileExists(path$, filename$)
    files path$, filename$, info$()
    fileExists = val(info$(0, 0))
End Function


 
User IP Logged

https://plus.google.com/u/0/111683094753659065762/about
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls