[fpc-pascal] Ethernet Relays

Michael Van Canneyt michael at freepascal.org
Sun Sep 6 17:11:59 CEST 2020



On Sun, 6 Sep 2020, James Richters via fpc-pascal wrote:

> I've been using simpleget to control ethernet relays like this:
> S:=TFPHTTPCLIENT.SIMPLEGET('http://10.10.01.01/30000/15');
>
> But if the IP address does not exist on the network I get this error:
>
> An unhandled exception occurred at $0000000100032A4A:
> ESocketError: Connection to 10.10.01.01:80 timed out.
>
> And my program terminates.  Is there some way I can capture this error
> with my program so I can just put up a message about the problem and keep
> running?

Yes, catch the ESocketError in a try..except:

Try
   S:=TFPHTTPCLIENT.SIMPLEGET('http://10.10.01.01/30000/15');
except
   On E: ESocketError do
     Writeln('Could not connect to server');
end;

> Is there a way to change the amount of time before the timeout?

Yes, but then you need to create an instance, i.e. the Simple* commands
won't do that for you:

Instead, it will look like this:

uses fphttpclient;

Var
   C : TFPHTTPClient;

begin
   C:=TFPHTTPClient.Create(Nil);
   try
      try
        C.ConnectTimeout:=10; // Or whatever you think is good
        C.Get('http://10.10.01.01/30000/15');
      except
        On E: ESocketError do
          Writeln('Could not connect to server');
      end;
   finally
     C.Free;
   end; 
end;

Michael.


More information about the fpc-pascal mailing list