Electronics > Microcontrollers

TCP Client/server best practice

(1/6) > >>

Retirednerd2020:
I am programming a small PLC to control a number (~5-6) devices on a wired ethernet LAN.  The PLC will be the (only) client.  The LAN is specifically set up for this purpose and no other traffic is expected on this network.  The application is simple.  Once every 5 seconds, poll the connected equipment for a small amount of data.  The data is <1k for each poll per device.  So, the traffic will be light.  Occasionally, maybe once every few hours, a small amount of set-up data is written to the equipment.  Probably less than 500 bytes or so.  Coordination between the connected equipment is not needed.  Timing is not very critical.  I can can have 8 connections at once with this PLC.  The system will not be expanded to more client devices.

I'm wondering which of these are best practice (and why).

A.  Connect to each, transfer the data, disconnect from each, repeat.  Repeat 5 seconds later.... for several days for a production run.

B. Connect to all of them forever (or at least for a period of a few days).  Then every 5 seconds, check the health of the connection, reconnect only if needed, transfer the data, repeat.

nctnico:
I'd go for B. Setting up a connection can be prone to memory leaks / dynamic resource allocation bugs.

Berni:
Both ways are perfectly valid.

Connecting each time has the benefit of starting with a fresh slate in terms of state every time so in general it is less bug prone, but means more work for the TCP/IP stack and more latency to do the handshake every time...etc

Connecting once and keeping a connection open is more efficient since upon needing to send data you simply throw a packet out there, but if your protocol involves some negotiation and state there is more of a possibility to get into some bad state and not recover. So it is a good idea to detect lockups and freshly reconnect when things go wrong, but that might introduce extra complexity...etc

Most of the world wide web works on opening individual connections for each thing (Like every file required to show a website opens a new TCP connection). But things like chat programs typically hold on to a open connection. The differences start to matter more once you get to a bigger scale and need to service millions of clients, for just 8 anything works fine.

Retirednerd2020:
Thanks!  I'll factor in your comments and decide.

madires:

--- Quote from: Berni on March 27, 2024, 07:52:00 pm ---Most of the world wide web works on opening individual connections for each thing (Like every file required to show a website opens a new TCP connection).

--- End quote ---

HTTP/2 supports request multiplexing over a single TCP connection since 2015. Unfortunately there are many late adopters despite a simple setup in most cases. For apache it's just loading a module plus setting a list of protocols.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod