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.
socket.getaddrinfo takes too long to respond
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: socket.getaddrinfo takes too long to respond
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".
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.
Code: Select all
>>> usocket.getaddrinfo('192.168.0.55', 1234)[0][-1]
('192.168.0.55', 1234)
>>>
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.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.