Recent

Author Topic: Ping a couple of computers  (Read 19045 times)

Roman78

  • New member
  • *
  • Posts: 9
Ping a couple of computers
« on: November 05, 2015, 10:12:26 am »
Just learning a bit programming whit Lazarus, more like learning by doing.

So i wanted to make a simple program that just pings some computers and that changes the colour of a TLabel. Red=offline and green is Online. Very simple. But i'm struggling whit the time to refresh. And somehow the colour.

I installed synapse to do this.

if pinghost('192.168.100.254') = 0 then TLC254.Font.Color:=($00AA00) Else TLC254.Font.Color:=($0000FF);

So if it's 0 than it's online (-1 is offline) and should be green, but sometimes it's blue. Don't know why jet.

But the bigger problem is the time to check if it is online. Is it possible to cut down the "time-out" times . So lets say, if a computer is not responding whit in some milliseconds, just presume it's offline. The documentation of Synapse is very short. And I don't have any experience whit the type of documentation, it's more try and error at this point.

The reason is it has to check about 70 computers. I planned to make an array whit 3 digits that are the equivalent of the last 3 digits from the ip. but that is in the further state of this.  First i want a quicker response.

balazsszekely

  • Guest
Re: Ping a couple of computers
« Reply #1 on: November 05, 2015, 12:26:31 pm »
Here you go:
Code: Pascal  [Select][+][-]
  1. var
  2.   PingSend: TPINGSend;
  3. begin
  4.   PingSend := TPINGSend.Create;
  5.   try
  6.     PingSend.Timeout := 750;
  7.     if PingSend.Ping('lazarus.freepascal.org') = True then
  8.     begin
  9.       TLC254.Font.Color := $00AA00;
  10.       TLC254.Caption := 'Reply from in: ' + IntToStr(PingSend.PingTime) + ' ms';
  11.     end
  12.     else
  13.     begin
  14.       TLC254.Font.Color := $0000FF;
  15.       TLC254.Caption := 'No response in: ' + IntToStr(PingSend.Timeout) + ' ms';
  16.     end;
  17.    finally
  18.      PingSend.Free;
  19.    end;
  20. end;      
  21.  

Roman78

  • New member
  • *
  • Posts: 9
Re: Ping a couple of computers
« Reply #2 on: November 05, 2015, 01:15:10 pm »
Thanks  :D

It's working. Actually i don't understand everything you did there.  Still have to learn a lot.

balazsszekely

  • Guest
Re: Ping a couple of computers
« Reply #3 on: November 05, 2015, 01:18:22 pm »
You're welcome!  :)
Quote
Actually i don't understand everything you did there.
Feel free to ask more questions if you like.

Roman78

  • New member
  • *
  • Posts: 9
Re: Ping a couple of computers
« Reply #4 on: November 05, 2015, 01:40:06 pm »
Don't want to ask to much. But... I see a variable of the type TPingSend and than the create and free stuff is what I don't understand. I

But, Lazarus is based on Pascal/Delphi. I had Turbo Pascal at school back in 1997. And I have Borland Pascal 4 and 5 and also some books, make it sense to read those? And can I use those as reference? Or is it a big difference between those two?

balazsszekely

  • Guest
Re: Ping a couple of computers
« Reply #5 on: November 05, 2015, 01:59:55 pm »
@Roman78

Quote
I see a variable of the type TPingSend and than the create and free stuff is what I don't understand.
TPingSend is a class(declared in unit "pingsend.pas"). In order to use it we create an instance of the class. To avoid memory leaks the created instance will then be freed at the end.

Quote
But, Lazarus is based on Pascal/Delphi. I had Turbo Pascal at school back in 1997. And I have Borland Pascal 4 and 5 and also some books, make it sense to read those? And can I use those as reference? Or is it a big difference between those two?
It's based on freepascal not pascal/delphi. I would use the online documentation.
« Last Edit: November 05, 2015, 02:05:27 pm by GetMem »

jma_sp

  • Full Member
  • ***
  • Posts: 154
  • El conocimiento si ocupa lugar.
Re: Ping a couple of computers
« Reply #6 on: November 05, 2015, 02:59:02 pm »
Hello Roman78:

The reference can help (also available into the downloadable synapse):

http://synapse.ararat.cz/doc/help/pingsend.TPINGSend.html
http://synapse.ararat.cz/doc/help/pingsend.html

Best with latest versión of Ararat Synapse (see licensing and conditions before use).

You need this unit and maybe others related (see errors in compiling that indicate who) or look at hierarchy( http://synapse.ararat.cz/doc/help/pingsend.TPINGSend.html#@Hierarchy).

Avoid continous ping because it take some time and with a lot of addresses it can do that your application do not respond to events (as buttons or.....) until it return from the call to ping. I don´t know who to create extra threads for pings...

Ping also return ping times (in local network normaly <=1 ms).

And best use FreePascal / Lazarus, Turbo Pascal versións 4 & 5 are too old (Turbo Pascal was great, i think the last version i used was 7.0). Once you begin developing with FreePascal you discover the advantages, best support for network, registry, graphics (under Windows / Linux), not only initgraph ....., best adapted for fast computers, 32/64 bits, optimized code for processor/memory....).


Sorry for my english, i can´t express as clear as i need.
Devuan Beowulf 3.0( JWM/ROX/iDesk) - Puppy Linux,  Haiku OS,.ReactOS 0.4.xx  - FreeDos .

Roman78

  • New member
  • *
  • Posts: 9
Re: Ping a couple of computers
« Reply #7 on: November 05, 2015, 05:22:33 pm »
Olla jma_sp,

I also found those reference, but I don't understand all of it. That is because the lack of experience. I have to start with some basics of FreePascal/Lazarus, there for I would like a book instead of internet documentation, so I can read that while I'm in bed. Preferably in German or Dutch. My English is also not the best, although I watch Dr. Who every Saturday.  8)



Roman78

  • New member
  • *
  • Posts: 9
Re: Ping a couple of computers
« Reply #8 on: November 06, 2015, 06:32:18 am »
Now I'm facing a new problem. I just made a loop to ping the first 10 ip's.

192.168.100.1 - 192.168.100.10

Therefore I made a for do loop, but the values won't change till the loop is finished. Than i changed it into a while loop, but result is that same. I remember the same issue back whit pascal, but I don't remember the solution.

I would like to see the label1 and label2 counting up.

And than I put the i into the KFZ string, so the value of KFZ is KFZ1,  KFZ2 and so on. But I can't use that whit the Font.Color. Also 'KFZ'+IntToStr(i).Font.Color is not working. Also tried some other options, but always get this error message: unit1.pas(69,59) Error: Illegal qualifier

Is there a way to put a string into a qualifier? I didn't found anything here: http://www.freepascal.org/docs-html/rtl/system/stringfunctions.html

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.    Pingsend: TPINGSend;
  4.    i: integer;
  5.    ip,KFZ: string;
  6. begin
  7.   i:=0;
  8.   while i < 11 do
  9.    begin
  10.    i:=i+1;
  11.    ip:= '192.168.100.' + IntToStr(i);
  12.    KFZ:='KFZ' + IntToStr(i);
  13.    Label1.caption:= (ip);
  14.    Label2.caption:= (kfz);
  15.    pingSend := TPINGSend.Create;
  16.      Try
  17.        PingSend.Timeout :=750;
  18.        if PingSend.Ping(ip) = True then KFZ1.Font.Color:=($00AA00) Else KFZ1.Font.Color:=($0000FF);
  19.      finally
  20.        PingSend.Free;
  21.      end;
  22.    end;
  23. end;  

christian1987

  • New Member
  • *
  • Posts: 30
Re: Ping a couple of computers
« Reply #9 on: November 06, 2015, 06:50:05 am »
Application.ProcessMessages;

Put that after each colour change.

JZS

  • Full Member
  • ***
  • Posts: 205
Re: Ping a couple of computers
« Reply #10 on: November 06, 2015, 07:14:54 am »
Now I'm facing a new problem. I just made a loop to ping the first 10 ip's.

192.168.100.1 - 192.168.100.10

Therefore I made a for do loop, but the values won't change till the loop is finished. Than i changed it into a while loop, but result is that same. I remember the same issue back whit pascal, but I don't remember the solution.

I would like to see the label1 and label2 counting up.

And than I put the i into the KFZ string, so the value of KFZ is KFZ1,  KFZ2 and so on. But I can't use that whit the Font.Color. Also 'KFZ'+IntToStr(i).Font.Color is not working. Also tried some other options, but always get this error message: unit1.pas(69,59) Error: Illegal qualifier

Is there a way to put a string into a qualifier? I didn't found anything here: http://www.freepascal.org/docs-html/rtl/system/stringfunctions.html

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.    Pingsend: TPINGSend;
  4.    i: integer;
  5.    ip,KFZ: string;
  6. begin
  7.   i:=0;
  8.   while i < 11 do
  9.    begin
  10.    i:=i+1;
  11.    ip:= '192.168.100.' + IntToStr(i);
  12.    KFZ:='KFZ' + IntToStr(i);
  13.    Label1.caption:= (ip);
  14.    Label2.caption:= (kfz);
  15.    pingSend := TPINGSend.Create;
  16.      Try
  17.        PingSend.Timeout :=750;
  18.        if PingSend.Ping(ip) = True then KFZ1.Font.Color:=($00AA00) Else KFZ1.Font.Color:=($0000FF);
  19.      finally
  20.        PingSend.Free;
  21.      end;
  22.    end;
  23. end;  

When having a loop (for/while no matters) the code executes the loop and then reflect the changes on the interface. Hence you see the last piece of code executed, in the last cycle of the loop.

In case you want to update the interface in the loop, use "Application.ProcessMessages;". But if the loop executes fast you need some delay method to see the changes. Probably in your case, the timeout is enough delay, not sure though if it is enough to see the result in every cycle.

Also, I think it would be better if you have a Listbox that lists the IPs that do not respond:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   Pingsend: TPINGSend;
  4.   i: integer;
  5.   ip, KFZ: string;
  6. begin
  7.   pingSend := TPINGSend.Create;    // create the instance once, use it as much as you like, and finally set it free
  8.   Try
  9.     PingSend.Timeout :=750;
  10.     i:= 1;
  11.     while i <= 10 do
  12.       begin
  13.         ip:= '192.168.100.' + IntToStr(i);
  14.         KFZ:= 'KFZ' + IntToStr(i);
  15.         Label1.caption:= ip; //(ip);   no need for prantheses
  16.         Label2.caption:= kfz;
  17.  
  18.         if PingSend.Ping(ip) then  //(no need for "= True" or "= False", it is a Boolean comparison in anyway)
  19.           begin
  20.             KFZ1.Font.Color:= $00AA00;
  21.           end
  22.         else
  23.           begin
  24.             KFZ1.Font.Color:=($0000FF);
  25.             Listbox1.Items.Add(KFZ+'>'+ip); //you can have another listbox for successful tryouts.
  26.           end;
  27.  
  28.         Application.ProcessMessages;  // to see the changes, but if the loop executes faster than you can catch, you won't see anything
  29.  
  30.         inc(i); //looks better than i:= i+1;
  31.       end;
  32.   finally
  33.     PingSend.Free;
  34.   end;
  35. end;
I use recent stable release

Roman78

  • New member
  • *
  • Posts: 9
Re: Ping a couple of computers
« Reply #11 on: November 06, 2015, 08:13:02 am »
Thanks  :D

It's working. But is there a reason to put the Try statement before the loop?

And. It's not quite clear here, but on my Form1 I have ten TLabels (KFZ1, KFZ2, KFZ3 and so on). They are by default red, and I want to change that colour. How do I put the i-variable into the KFZ string so that the .Font.Color is working?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Ping a couple of computers
« Reply #12 on: November 06, 2015, 08:32:15 am »
It's working. But is there a reason to put the Try statement before the loop?
Yes, see also here.

If, for whatever reason, something goes wrong inside the try block (e.g. an exception occurs) you are certain that the finally part is always being executed. In your particular case that means it will always free up the resources being used by TPingSend object.

Quote
And. It's not quite clear here, but on my Form1 I have ten TLabels (KFZ1, KFZ2, KFZ3 and so on). They are by default red, and I want to change that colour. How do I put the i-variable into the KFZ string so that the .Font.Color is working?
There are several options available.

One is putting all your labels (or beter said: pointers to your labels) inside a array that uses an index that matches your i variable.

Or in case names always start with the same letters + number you might be able to use a solution using FindComponent.
« Last Edit: November 06, 2015, 08:39:42 am by molly »

Roman78

  • New member
  • *
  • Posts: 9
Re: Ping a couple of computers
« Reply #13 on: November 06, 2015, 09:43:52 am »
Ah. that make sense.

Thanks.

Is there a possibility to put more condition into a "If" statement?

Like:  If i = 4 or 7 or 12 then

instead of
if i = 4 then
if i = 7 then
if i = 12 then

balazsszekely

  • Guest
Re: Ping a couple of computers
« Reply #14 on: November 06, 2015, 09:50:18 am »
Yes you can:
Code: Pascal  [Select][+][-]
  1. if (i = 4) or (i = 7) then
Please always do a quick google search. If you cannot find an answer ask here. Iit's much faster this way. :)

 

TinyPortal © 2005-2018