EthernetClient.connect() blocking issue

Hello everyone,

I have an Ethernet Shield for my Arduino Mega 2560 and am using the EthernetClient class to connect to a client.

The issue that I am experiencing is that the connect() method can block for seconds at a time, interfering with a requirement that I service a response from another computer connected to my Arduino within a very fast period of time.

What happens currently is that if a connect() call is being made, the loop() method is blocked for the time it takes to connect, and this is exacerbated if I use an IP address that is not assigned to anything. The blocking could go on for 10 or more seconds.

Is there some way to dig deeper into the EthernetClient class to allow for some sort of callback that would be called on either success or failure of a connect, allowing my loop() function to not block?

Here is the datasheet for the w5100.

There are a couple registers you may be interested in modifying. Take a look at the bottom of page 22 and top of page 23. Registers RTR (Retry Time Register) and RCR (Retry Count Register).

Then look at the /libraries/Ethernet/utility/w5100.h file at the bottom. Those are the functions you use to change them.

I think I would start with the RCR register. According to the docs, it will attempt to connect 8 times before returning fail. Maybe you want just one attempt per call.

Thanks for the tip, SurferTim. Good stuff. I looked and indeed there are C++ methods for setting those registers. Although I would still like the flexibility of longer timeouts, I can live with this for now. I'll delve into the source a bit more to see if there is anyway to make the connect work in an asynchronous fashion.

Thanks again for the info.

No problem. I figured it would be best to start you at the lowest level of the library code. If you look at the source, you should be able to find a function you can modify so you can put a hook in to check and break if not ready, rather than wait.

edit: I'd be interested in seeing that when you finish it. :slight_smile:

This was a problem I fought for a long time. There is a (relatively) simple solution here:

Or, you can do something similar directly in the connect logic in the library. That will make upgrading the IDE tougher when you want to move forward though. I modified the library back in version 21 to support a timeout on the connect(), and it drove me nuts when I wanted to move to IDE 1.0+

There is a similar problem with the DNS lookup, but the solution is different. When you run into that problem, the answer is on this board somewhere as well.