That's a strange error.
This might not answer your question, but may push us towards figuring out what's going on.
The code snippet (copied from question) will open up a new stream
with each .getUrl()
call and will not close them. (I'm assuming this is intentional to create the socket exception?)
HttpClient userAgent = new HttpClient();
bool run = true;
while (run) {
try {
await userAgent.getUrl(Uri.parse('https://www.google.com'));
print('Number of api executed');
} catch (e) {
print(e);
if (e is SocketException) {
if ((e as SocketException).osError.errorCode == 8)
print('***** Exception Caught *****');
}
}
}
At some point, a limit (of open streams) is hit. I guess that magic number is 236 in your case.
So at that point, is when you're seeing the nodename or servname provided
exception?
(Btw, as an aside, I think that error is coming from the underlying host operating system's DNS service, although I'm not sure if it's due to the request spam, the number of open connections, etc. This may not be relevant info.)
So, if you used the HttpClient
in a typical way, making requests & closing those open streams, such as this:
var request = await userAgent.getUrl(Uri.parse('http://example.com/'));
var response = await request.close(); // ← close the stream
var body = await response.transform(utf8.decoder).join();
// ↑ convert results to text
// rinse, repeat...
... Are you still seeing the same nodename or servname provided
error pop up?
With this "typical usage" code immediately above, the userAgent
can be reused until a userAgent.close()
call is made (and the HttpClient is permanently closed.
Trying to use it again would throw a Bad State
exception).
I'd be interested to hear if the nodename error still occurs with this modified code.
Re: the second code snippet from the question.
In the catch block, the HttpClient
is closed, then a new HttpClient
is created. This effectively closes all the open streams that were opened in the try
block (and I assume, resetting the limit of open streams.)
If you adjusted the 2nd code example to use:
var req = await userAgent.getUrl(Uri.parse('https://www.google.com'));
userAgent.close(force: true);
userAgent = HttpClient();
print('Number of api executed');
Could you run that indefinitely?