1

I have a very inconvenient problem with a web server I'm writing for a Arduino Mega 2560 with a W5100 Ethernet Shield (with the "official" Ethernet lib). I wrote the server code and it works like a charm, BUT there is a condition, when the server won't accept all the TCP handshakes and resets the TCP connections: When I load a big webpage, the browser (Firefox and Chrome too) asks for the .htm first, then collects the links in the file and request them all simultaneously and that's when I got the problem:

The server gets multiple TCP connections but it only accepts the first, then dumps all the others (by TCP reset). After that the client sends them again and the problem starts again: the server accepts the first and drops the others. And so on, for example if the browser needs 3 files, it will take ~9 seconds, because of the retransmission delay. (3 + 6 seconds) Eventually the webpage will load, but a 10 second delay is not acceptable. Is there any way to cache the requests on the Ethernet Shield or to tell the browser to wait a bit between requests or any other way, to wait a bit for the Arduino to finish the previous request?

Snippet for the server, Wireshark and Chrome network data: https://gist.github.com/anonymous/181f359b70d69e4470931204c4ec175c

Thanks for the help!

G.

1
  • I am also having this on a Portenta H7. However, the requests are not being retried. Some assets are just never loading. Did you ever find a solution?
    – Marcel
    Commented Jun 30, 2023 at 15:42

1 Answer 1

1

I have received a request from the OP to move this to Network Engineering because it got no answers. I'm going to decline this for the moment because I doubt they will be familiar with the Arduino Ethernet shields.


The low-cost shields have a known limit on the number of connections they can handle simultaneously (no doubt limited by available RAM). From the page Arduino Ethernet Shield 2 we learn:

It supports up to eight simultaneous socket connections.

You don't seem to be getting that, which makes me wonder if it is a code issue. It might help to post your code. If you make code that "blocks" while handling the first connection you may not accept the other ones in a timely way, leading to a reset.

Please post a Minimal, Complete, and Verifiable example.

I did a post about a tiny web server for the Arduino which uses a state machine to handle incoming data without blocking.


You may also be able to help yourself by using the HTTP header that requests that the connection be kept open, eg.

Connection: keep-alive

That might stop the client opening new connections in quick succession.

5
  • Thank you for your answer, I'll write a working example of my server tomorrow, I'll leave a comment then. Your web server solution might be applicable for my problem, but I'd have to modify it and my server code heavily to make it queue requests, so it's kind of a last resort solution. The "keep-alive" header unfortunately did not solve the problem. Commented Feb 26, 2017 at 15:11
  • I asked for the move of my question because I was hoping I could get some networking (like TCP/IP or HTTP or client-side) tip to solve the problem without heavily modifying my server code and also because I had no answer, but that changed recently. :) Commented Feb 26, 2017 at 15:11
  • @NickGammon what do you mean you server is non-blocking? It only handles one client (one HTTP connection) at a time as far as I can tell from your code. I don't see how this solves the problem.
    – Marcel
    Commented Jun 30, 2023 at 16:10
  • @Marcel In the main loop, it gets a character at a time from the client and passes that to the "state machine" in the server code. It doesn't block. Until a request reaches a reasonable processing point it just keeps accepting new characters. A reasonable point might be when a GET parameter has been received, upon which it calls processGetArgument. Now since it doesn't block, you could have multiple instances of the server object, and feed multiple HTTP connections to them (one to each one).
    – Nick Gammon
    Commented Jul 1, 2023 at 1:09
  • @NickGammon Oh I see. Thanks for clarifying. The implementation seems a little involved though. Any plans to add something like this to the library?
    – Marcel
    Commented Jul 1, 2023 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.