Page 1 of 1

socket.getaddrinfo takes too long to respond

Posted: Mon Aug 12, 2019 6:08 am
by akushva
I'm trying to use socket library in my application on ESP32. I'm trying to connect to a device via it's IP address using the command below-

sock.connect(socket.getaddrinfo(device_IP, device_port)[0][-1])

I noticed that it takes around 19-20 seconds whenever there is a failure in connection or I put wrong IP address, after which it responds with this error:

[Errno 113] EHOSTUNREACH

The timeout argument in socket library doesn't help it and I'm am unable to connect to a device when I configure the socket to be a non-blocking socket.
Is there a way so that I can reduce this waiting time down to 2-4 seconds.

Thanks.

Re: socket.getaddrinfo takes too long to respond

Posted: Tue Aug 13, 2019 8:19 am
by pythoncoder
Perhaps I'm missing something but I can't replicate this on the ESP32 reference board. The following code runs "instantly" whether or not a connection to my WiFi is running. The device does not exist on my network and a ping produces "Destination Host Unreachable".

Code: Select all

>>> usocket.getaddrinfo('192.168.0.55', 1234)[0][-1]
('192.168.0.55', 1234)
>>> 
You could create the tuple directly, but this is a hack and your approach is the correct one.

If you're doing a DNS lookup by name getaddrinfo will block for a period depending on the response time of your DNS server. This is a known limitation. The need for a nonblocking solution has been discussed but as far as I know has not been written.