Forum > Networking and Web Programming

TIdTCPClient.connect how to check if server is running

(1/1)

Dzandaa:
Hi everybody,

Working with Lazarus Pascal 2.012 FPC 2.3.0 on Windows, Linux and MacOS

I'm using Indy10 to connect to a custom made ESP32 based WiFi camera.

using
Client: TIdTCPClient;


Everything is working fine, but I have to use a TIdIcmpClient  to check that the camera server is online before using Client.Connect or
it crashes.

The problem in Linux is that TIdICMPClient requires root access and I don't want that :).

Any other Idea?

Thank you.




Remy Lebeau:

--- Quote from: Dzandaa on June 14, 2022, 11:57:19 am ---I'm using Indy10 to connect to a custom made ESP32 based WiFi camera.
...
Everything is working fine, but I have to use a TIdIcmpClient  to check that the camera server is online before using Client.Connect or it crashes.

--- End quote ---

Crashes how, exactly?  TIdTCPClient.Connect() raises an exception if it can't connect to the server. That is not a crash, unless your code is not handling the exception properly.

ICMP will tell you only whether the device is reachable on the network.  It will not tell you if the device has a running TCP server that can be connected to.  You still have to call TIdTCPClient.Connect() regardless.  So, your best option is to simply wrap the TIdTCPClient.Connect() call in a try..except block to catch the exception, and then move on with your code logic as needed.


--- Quote from: Dzandaa on June 14, 2022, 11:57:19 am ---The problem in Linux is that TIdICMPClient requires root access and I don't want that :).

--- End quote ---

Known issue: https://github.com/IndySockets/Indy/issues/122

The workaround is to use ICMP over a SOCK_DGRAM socket, which does not require root access, rather than a SOCK_RAW socket:

Unprivileged ICMP sockets on Linux.

ICMP sockets (linux)

Indy doesn't have any high-level components that allow you to create such a socket (all uses of SOCK_DGRAM are limited to IPPROTO_UDP), but you can probably cobble something together using Indy's TIdSocketHandle and/or TIdStack... classes directly.

Also, there are caveats with this DGRAM+ICMP approach, ie:

- only ICMP echo requests are supported
- may not be implemented uniformly across different Linux distributions
- ports are used as message identifiers
- may require a call to sysctl to enable: https://lkml.org/lkml/2011/5/18/305

Dzandaa:
Hi again,

Here is part of my code running in a thread


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// **************************************// ***** Initiate Camera Connection *****// **************************************function TCamThread.CamConnect(): boolean;begin try  TaskClient.Connect; except  on E: Exception do  begin   ShowMessage('Cannot connect: ' + E.Message);   exit(false);  end; end;     ... exit(True);end; 
I tested in debug mode and it never reach ShowMessage

And the crash:


--- Quote ---Project ESP32Cam raised exception class 'EIDNotASocket' with message:
Socket Error # 1003Socket operation on non-socket

In file 'System\IdStack.pas' at line 942

--- End quote ---

I don't have enough documentation and examples on Indy10 and the manual is not available (http://ww2.indyproject.org : server not found)

an example would be welcome :)


Remy Lebeau:

--- Quote from: Dzandaa on June 14, 2022, 07:29:42 pm ---Here is part of my code running in a thread

--- End quote ---

ShowMessage() is not thread-safe. And where is TaskClient defined and initialized before CamConnect() is called? Can you provide an example that actually compiles?


--- Quote from: Dzandaa on June 14, 2022, 07:29:42 pm ---I tested in debug mode and it never reach ShowMessage

And the crash:


--- Quote ---Project ESP32Cam raised exception class 'EIDNotASocket' with message:
Socket Error # 1003Socket operation on non-socket

--- End quote ---

--- End quote ---

Again, that is not a crash.  That is just a normal exception.  And your code is clearly running inside the debugger, which is catching the exception first.  Are you instructing the debugger to continue execution so the exception is passed back to your application for handling?


--- Quote from: Dzandaa on June 14, 2022, 07:29:42 pm ---In file 'System\IdStack.pas' at line 942

--- End quote ---

That is the line of code that is actually raising the exception, in response to a socket function returning an ENOTSOCK error code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TIdStack.RaiseSocketError(AErr: integer);begin  ...  if AErr = Id_WSAENOTSOCK then begin    ...    raise EIdNotASocket.CreateError(AErr, WSTranslateSocketErrorMsg(AErr)); // <-- line 942  end;  ...end;
But, what does the call stack look like that is leading up to that error?  It is very unusual for TIdTCPClient.Connect() to encounter that particular error, unless you are misusing the TIdTCPClient object, which we can't see since you did not provide enough code yet.


--- Quote from: Dzandaa on June 14, 2022, 07:29:42 pm ---the manual is not available (http://ww2.indyproject.org : server not found)

--- End quote ---

https://www.indyproject.org/2021/02/10/links-to-old-indy-website-pages-are-currently-broken/

Dzandaa:
Hello,

Thank you Remy,

My program works now, in fact everything was OK (except ShowMessages which was just a test)
Effectively, I stopped the program when the debugger showed me the error ;)
I didn't know the debugger showed the error before jumping into except.

when I pass the error, the exception works fine. And in the release version, no problem.
Thank you very much.

Navigation

[0] Message Index

Go to full version