Ping a couple of computers lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Roman78 · 2015-11-05 09:12 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 · 2015-11-05 11:26 Here you go: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  PingSend: TPINGSend;begin  PingSend := TPINGSend.Create;  try    PingSend.Timeout := 750;    if PingSend.Ping('lazarus.freepascal.org') = True then    begin      TLC254.Font.Color := $00AA00;      TLC254.Caption := 'Reply from in: ' + IntToStr(PingSend.PingTime) + ' ms';    end    else    begin      TLC254.Font.Color := $0000FF;      TLC254.Caption := 'No response in: ' + IntToStr(PingSend.Timeout) + ' ms';    end;   finally     PingSend.Free;   end;end; ---------------------------------------------------------------------- Roman78 · 2015-11-05 12:15 Thanks  It's working. Actually i don't understand everything you did there.  Still have to learn a lot. ---------------------------------------------------------------------- balazsszekely · 2015-11-05 12:18 You're welcome!  QuoteActually i don't understand everything you did there.Feel free to ask more questions if you like. ---------------------------------------------------------------------- Roman78 · 2015-11-05 12:40 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 · 2015-11-05 12:59 @Roman78 QuoteI 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. QuoteBut, 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. ---------------------------------------------------------------------- jma_sp · 2015-11-05 13:59 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. ---------------------------------------------------------------------- Roman78 · 2015-11-05 16:22 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. ---------------------------------------------------------------------- Roman78 · 2015-11-06 05:32 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var   Pingsend: TPINGSend;   i: integer;   ip,KFZ: string;begin  i:=0;  while i < 11 do   begin   i:=i+1;   ip:= '192.168.100.' + IntToStr(i);   KFZ:='KFZ' + IntToStr(i);   Label1.caption:= (ip);   Label2.caption:= (kfz);   pingSend := TPINGSend.Create;     Try       PingSend.Timeout :=750;     if PingSend.Ping(ip) = True then KFZ1.Font.Color:=($00AA00) Else KFZ1.Font.Color:=($0000FF);     finally       PingSend.Free;     end;   end;end; ---------------------------------------------------------------------- christian1987 · 2015-11-06 05:50 Application.ProcessMessages; Put that after each colour change. ---------------------------------------------------------------------- JZS · 2015-11-06 06:14 Quote from: Roman78 on November 06, 2015, 06:32:18 amNow 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var   Pingsend: TPINGSend;   i: integer;   ip,KFZ: string;begin  i:=0;  while i < 11 do   begin   i:=i+1;   ip:= '192.168.100.' + IntToStr(i);   KFZ:='KFZ' + IntToStr(i);   Label1.caption:= (ip);   Label2.caption:= (kfz);   pingSend := TPINGSend.Create;     Try       PingSend.Timeout :=750;     if PingSend.Ping(ip) = True then KFZ1.Font.Color:=($00AA00) Else KFZ1.Font.Color:=($0000FF);     finally       PingSend.Free;     end;   end;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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var  Pingsend: TPINGSend;  i: integer;  ip, KFZ: string;begin  pingSend := TPINGSend.Create;    // create the instance once, use it as much as you like, and finally set it free  Try    PingSend.Timeout :=750;    i:= 1;    while i <= 10 do      begin        ip:= '192.168.100.' + IntToStr(i);        KFZ:= 'KFZ' + IntToStr(i);        Label1.caption:= ip; //(ip);   no need for prantheses        Label2.caption:= kfz;         if PingSend.Ping(ip) then  //(no need for "= True" or "= False", it is a Boolean comparison in anyway)          begin            KFZ1.Font.Color:= $00AA00;        end        else          begin            KFZ1.Font.Color:=($0000FF);            Listbox1.Items.Add(KFZ+'>'+ip); //you can have another listbox for successful tryouts.          end;         Application.ProcessMessages;  // to see the changes, but if the loop executes faster than you can catch, you won't see anything       inc(i); //looks better than i:= i+1;      end;  finally    PingSend.Free;  end;end; ---------------------------------------------------------------------- Roman78 · 2015-11-06 07:13 Thanks  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 · 2015-11-06 07:32 Quote from: Roman78 on November 06, 2015, 08:13:02 amIt'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. QuoteAnd. 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. ---------------------------------------------------------------------- Roman78 · 2015-11-06 08:43 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 · 2015-11-06 08:50 Yes you can: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if (i = 4) or (i = 7) thenPlease always do a quick google search. If you cannot find an answer ask here. Iit's much faster this way. ---------------------------------------------------------------------- molly · 2015-11-06 08:54 Quote from: Roman78 on November 06, 2015, 09:43:52 amAh. that make sense. Thanks. Too much regarding the other suggestion ?  No problemo. New form, 5 labels one button, code looking like this (will hopefully get you going). I cheated though by combining the two mentioned solutions into one. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    Label2: TLabel;    Label3: TLabel;    Label4: TLabel;    Label5: TLabel;    procedure Button1Click(Sender: TObject);  private  { private declarations }  public    { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm}  { TForm1 } procedure TForm1.Button1Click(Sender: TObject);Var  i : Integer;  LabelArray : Array[1..5] of TLabel;begin  // Temp disable button  (Sender as TButton).Enabled := false;   // Fill array with labels  For i := Low(labelArray) to High(labelArray) do  begin    LabelArray[i] := FindComponent('Label' + IntToStr(i)) as TLabel;  end;   For i := 1 to 5 do  begin    // Do some other stuff     // Change colour based on result from other stuff done above    // As for an example: use odd/even    if Odd(i)    then LabelArray[i].Font.Color:= clGreen    else LabelArray[i].Font.Color:= clRed;     // Make sure GUI gets updated between changes    Application.ProcessMessages;     // Perform some delay in order to humanly be able to follow GUI changes    Sleep(3000);  end;   // Restore default colour  For i := 1 to 5    do LabelArray[i].Font.Color:= clDefault;   // Enable button  (Sender as TButton).Enabled := True;end; end.  QuoteIs 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 Again several solutions available, depending on the exact circumstances. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i If  (  (i = 4) or   (i =7) or  (i = 12) )then  Or Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Case i of  1,2,3,4: begin {do something when i = 1,2,3 or 4} end;  5 : begin  {do something when i = 5} end;  else begin {do something if i didn't match any of the above values } end;end; ---------------------------------------------------------------------- jma_sp · 2015-11-06 12:15 OK, if you download the lastest version of synapse, actually 40 from: http://synapse.ararat.cz/doku.php/download Uncompress it. It contains two simple demo / examples: source\demo\scan It have an example of multithreaded ping, fast for a Class C Network, best that a simple thread. , best option. Ignore .dpr for delphi project file, read .pas. The other is testping.pas \source\demo\FreePascal ---------------------------------------------------------------------- nomorelogic · 2015-11-06 14:39 Quote from: Roman78 on November 06, 2015, 09:43:52 amAh. 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 in this case I test "i" as included in "set of ..." if i in [4,7,12] then .... ---------------------------------------------------------------------- christian1987 · 2015-11-06 14:56 So easy: Lab:= Form1.FindComponent('KFZ'+IntToStr(I)) as TLabel; ---------------------------------------------------------------------- christian1987 · 2015-11-06 15:44 Lab:=KFZ1.parent. FindComponent('KFZ'+IntToStr(I)) as TLabel;