I am writing a C application that starts as systemd service at boot (distro: Arch Linux) and which should connect to the server, since the application starts at boot, it ultimately happens that the network connection has not yet been established. This, of course, leads to the failure of the first function, which requires one, which in my case is getaddrinfo .
So, I thought that I would just write a loop that calls getaddrinfo repeatedly until it completes after the network is ready. Unfortunately, I found that getaddrinfo continues to work with name or service not known even after the connection is established.
I can ping the server by hostname, but getaddrinfo will not do this anyway. If I stop the application and start it again, everything will be fine. If the network connection is already established before the first call, getaddrinfo also works fine.
Apparently, if getaddrinfo failed once because the network was not ready, it would not be forever. He does not seem to know about the existing connection. When using an obsolete gethostbyname behavior will be the same.
What is the reason for this behavior? Is there a way to get getaddrinfo update internal variables (if any) or similar, which might explain why the function still thinks there is no connection? Is there another function that I would have to call earlier to check if the network is ready?
I would like to avoid the delay that waits for a while, waiting for the network to be connected afterwards. I would also prefer to test the connection from my application and not first run a bash script check and then run the application.
source share