Checking if a computer is ON on the network

General FreeBASIC programming questions.
xbgtc
Posts: 157
Joined: Oct 14, 2007 5:40
Location: Australia

Checking if a computer is ON on the network

Postby xbgtc » Feb 15, 2019 4:57

Is there any code to check if a computer is ON and OFF on the network?

Currently i'm using

Code: Select all

if CHDIR(computer1)then...

and that works great but has one problem. when you shutdown the computer, after about 5secs it will hang at that check for 36sec and 2sec later (the delay for checks) will hang again for 44secs and then will be ok for consequent checks, so trying to get around this problem as this code is in a thread but it somehow still affects the main program which is waiting for input hence unresponsive for those above times - thanks.
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Checking if a computer is ON on the network

Postby jj2007 » Feb 15, 2019 9:13

Not sure how to do this in FB, but it works:

Code: Select all

  Print Launch$("cmd.exe /C ping -n 1 MyOtherNotebook")
If the "other" notebook is present, it takes milliseconds, otherwise about 3 seconds. There is a thread here, but it doesn't seem to apply to Windows.
xbgtc
Posts: 157
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Checking if a computer is ON on the network

Postby xbgtc » Feb 15, 2019 10:55

thanks but I'm aware of ping but wanna see if there's a coding way rather than running external programs.
srvaldez
Posts: 2134
Joined: Sep 25, 2005 21:54

Re: Checking if a computer is ON on the network

Postby srvaldez » Feb 15, 2019 11:38

assuming your OS is Windows, perhaps you could use the IsNetworkAlive function https://docs.microsoft.com/en-us/window ... tworkalive
I am not a Windows API expert, so this is only an Idea that you may want to consider
I ran winapifamily.h through FBfrog, and this is the result

Code: Select all

#pragma once

#include once "winpackagefamily.bi"

#define _INC_WINAPIFAMILY
const WINAPI_FAMILY_PC_APP = 2
const WINAPI_FAMILY_PHONE_APP = 3
const WINAPI_FAMILY_SYSTEM = 4
const WINAPI_FAMILY_SERVER = 5
const WINAPI_FAMILY_DESKTOP_APP = 100
const WINAPI_FAMILY_APP = WINAPI_FAMILY_PC_APP
const WINAPI_FAMILY = WINAPI_FAMILY_DESKTOP_APP
const WINAPI_PARTITION_DESKTOP = WINAPI_FAMILY = WINAPI_FAMILY_DESKTOP_APP
const WINAPI_PARTITION_APP = ((WINAPI_FAMILY = WINAPI_FAMILY_DESKTOP_APP) orelse (WINAPI_FAMILY = WINAPI_FAMILY_PC_APP)) orelse (WINAPI_FAMILY = WINAPI_FAMILY_PHONE_APP)
const WINAPI_PARTITION_PC_APP = (WINAPI_FAMILY = WINAPI_FAMILY_DESKTOP_APP) orelse (WINAPI_FAMILY = WINAPI_FAMILY_PC_APP)
const WINAPI_PARTITION_PHONE_APP = WINAPI_FAMILY = WINAPI_FAMILY_PHONE_APP
const WINAPI_PARTITION_SYSTEM = (WINAPI_FAMILY = WINAPI_FAMILY_SYSTEM) orelse (WINAPI_FAMILY = WINAPI_FAMILY_SERVER)
const WINAPI_PARTITION_PHONE = WINAPI_PARTITION_PHONE_APP
#define WINAPI_FAMILY_PARTITION(Partitions) (Partitions)
#define _WINAPI_DEPRECATED_DECLARATION __declspec(deprecated("This API cannot be used in the context of the caller's application type."))

and likewise for sensapi.h

Code: Select all

#pragma once

#include once "winapifamily.bi"

#define __SENSAPI_H__
const NETWORK_ALIVE_LAN = &h00000001
const NETWORK_ALIVE_WAN = &h00000002
const NETWORK_ALIVE_AOL = &h00000004
const NETWORK_ALIVE_INTERNET = &h00000008

type tagQOCINFO
   dwSize as DWORD
   dwFlags as DWORD
   dwInSpeed as DWORD
   dwOutSpeed as DWORD
end type

type QOCINFO as tagQOCINFO
type LPQOCINFO as tagQOCINFO ptr
#define IsDestinationReachable IsDestinationReachableA
'' TODO: BOOL APIENTRY IsDestinationReachableA( LPCSTR lpszDestination, LPQOCINFO lpQOCInfo );
'' TODO: BOOL APIENTRY IsDestinationReachableW( LPCWSTR lpszDestination, LPQOCINFO lpQOCInfo );
'' TODO: BOOL APIENTRY IsNetworkAlive( LPDWORD lpdwFlags );
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Checking if a computer is ON on the network

Postby jj2007 » Feb 15, 2019 12:16

xbgtc wrote:thanks but I'm aware of ping but wanna see if there's a coding way
There is: IcmpSendEcho
dodicat
Posts: 5990
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Checking if a computer is ON on the network

Postby dodicat » Feb 15, 2019 12:31

You want it in FreeBASIC code, then use the freeBASIC website to tell you.
(As a bonus you get an IP address)

Code: Select all

 

#include "win\winsock2.bi"

function GetSiteAddress(stuff as string="www.freebasic.net") as string
   dim as WSADATA  _wsadate
   dim as  in_addr addr
   dim as hostent ptr res
   dim as integer i = 0
  WSAStartup(MAKEWORD(2,2),@_wsadate)
   res = gethostbyname(stuff)
   if res then
   print stuff
   while (res->h_addr_list[i] <> 0)
     addr.s_addr = *(cast (ulong ptr,res->h_addr_list[i]))
   print "IP Address: ";*inet_ntoa(addr)
      i+=1
   wend
   WSACleanup()
else
   print "website error?"
end if
if stuff="www.freebasic.net" and res=0 then return "offline"
if stuff="www.freebasic.net" and res<>0 then return "online"
end function

print GetSiteAddress
print
print GetSiteAddress ("www.freepascal.org")
sleep
 
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Checking if a computer is ON on the network

Postby jj2007 » Feb 15, 2019 12:36

dodicat wrote:You want it in FreeBASIC code, then use the freeBASIC website to tell you.
(As a bonus you get an IP address)
Hey, that works much better than IcmpSendEcho, even for my 'TheOtherNotebook'! Thanxalot, dodicat...
xbgtc
Posts: 157
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Checking if a computer is ON on the network

Postby xbgtc » Feb 15, 2019 15:04

thanks for your efforts jj2007 :)

@dodicat
thanks for that code!

i had already gone through winsock.bi but didn't think it was suitable as it didn't work as I used a LAN address eg. \\ComputerName\C Drive\ but should of just used "ComputerName" and it works fine by the code you supplied - thanks!
badidea
Posts: 1584
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Checking if a computer is ON on the network

Postby badidea » Feb 15, 2019 15:31

Dodicat's program modified for non-Windows use as well:

Code: Select all

#ifdef __FB_WIN32__
   #INCLUDE once "win/winsock2.bi"
#Else
   #INCLUDE once "crt/netdb.bi"
   #INCLUDE once "crt/sys/socket.bi"
   #INCLUDE once "crt/netinet/in.bi"
   #INCLUDE once "crt/arpa/inet.bi"
   #INCLUDE once "crt/unistd.bi"
#EndIf

function GetSiteAddress(stuff as string="www.freebasic.net") as string
   dim as  in_addr addr
   dim as hostent ptr res
   dim as integer i = 0
   
   #ifdef __FB_WIN32__
      dim as WSADATA  _wsadate
      If( WSAStartup(MAKEWORD(2, 2), @_wsadate) <> 0 ) Then Return 1
   #EndIf
   
   res = gethostbyname(stuff)
   if res then
      print stuff
      while (res->h_addr_list[i] <> 0)
         addr.s_addr = *(cast (ulong ptr, res->h_addr_list[i]))
         print "IP Address: "; *inet_ntoa(addr)
         i+=1
      wend
      
      #ifdef __FB_WIN32__
         WSACleanup()
      #EndIf
   else
      print "website error? "; stuff
   end if
   if stuff="www.freebasic.net" and res=0 then return "offline"
   if stuff="www.freebasic.net" and res<>0 then return "online"
end function

print GetSiteAddress
print
print GetSiteAddress ("www.freepascal.org")
print GetSiteAddress ("www.notawebsite123.456")
print "end"
sleep
xbgtc
Posts: 157
Joined: Oct 14, 2007 5:40
Location: Australia

Re: Checking if a computer is ON on the network

Postby xbgtc » Feb 15, 2019 23:29

NO GOOD!

I have shortened the code (I hate bloat)

Code: Select all

#include "win\winsock2.bi"

function CO(comp as string) as byte
    dim as WSADATA  giger
    dim as hostent ptr cp
    WSAStartup(MAKEWORD(2,2),@giger)
    cp=gethostbyname(comp)
    if cp then return 1 else return 0
    WSACleanup()
end function

do
    if CO("C128") then ?"Franco:ON" else ?"Franco:OFF"
    if CO("CASPER") then ?"Abbo:ON" else ?"Abbo:OFF"
    if CO("LAPTOP") then ?"Lappy:ON" else ?"Lappy:OFF"
    sleep 2000
loop while inkey=""

and it works fine when computers are ON or OFF BUT... when they are shutdown they still report ON :(

May have to add code to my program to just fake the OFF and skip checks for about 90secs after it gets a shutdown signal or just don't worry about it as it's only annoying when testing and in actual program use you don't really message after you shutdown a computer.
marcov
Posts: 2793
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Checking if a computer is ON on the network

Postby marcov » Feb 20, 2019 10:36

jj2007 wrote:Not sure how to do this in FB, but it works:

Code: Select all

  Print Launch$("cmd.exe /C ping -n 1 MyOtherNotebook")
If the "other" notebook is present, it takes milliseconds, otherwise about 3 seconds. There is a thread here, but it doesn't seem to apply to Windows.


(if you know that it is local, you can reduce the timeout with -w <ms>)
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Checking if a computer is ON on the network

Postby jj2007 » Feb 20, 2019 10:56

marcov wrote:(if you know that it is local, you can reduce the timeout with -w <ms>)
Thanks. I had tested that before and saw no effect. Now I retried, and got it to work (no idea what was different...). Interesting:

- without any wait interval (no "-w"), the answer is "host cannot be reached", but the modem responds "no packet lost"
- with a -w 5000, the answer is again "host cannot be reached", "no packets lost"
- with e.g. a -w 500, the answer is "request exceeded timeout", and the modem says "100% of packets lost"

The actual messages will look different, this is my translation - I am running an Italian Windows version.

Based on these observations, I would recommend to use cmd.exe /C ping -w 100 -n 1 NameOfLocalComputer. If speed is an issue, -w 10 or even -w 1 may work.

If the other machine is online, the answer is for all variants, including a "-w 1", roughly like this:

Code: Select all

Esecuzione di Ping TheOtherNotebook.blabla.telecomitalia.it [192...] con 32 byte di dati:
Risposta da 192...: byte=32 durata<1ms TTL=128

Statistiche Ping per 192...:
    Pacchetti: Trasmessi = 1, Ricevuti = 1,
    Persi = 0 (0% persi),
Tempo approssimativo percorsi andata/ritorno in millisecondi:
    Minimo = 0ms, Massimo =  0ms, Medio =  0ms
However, "-w 0" reports a "general error".

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest