Tech notes 8 min read

TCP is not arcane either: shake hands first, then send bytes in order

Device connectivity always comes back to TCP. Nothing mysterious: confirm both sides are there, confirm both can send and receive, number every byte, resend what is lost, reorder what arrives out of sequence, and slow down when the receiver is busy. TCP guarantees delivery, not understanding. Making sense of the message is the platform's job.

TCP is not arcane either: shake hands first, then send bytes in order

The device screen says “TCP connected”, yet the platform still shows no data. On site people often say: the network is up, why can't we receive anything?

That is half right. A TCP connection only means two machines can move bytes between them. Whether those bytes can be understood is another layer.

Put TCP in plain words

TCP is like two people on a phone call, with a few extra rules: confirm the other side is there, confirm both can speak and hear, then start; number every sentence and repeat what was not heard; slow down when the other side is busy.

A “connection” is not a real wire. It is a state each side keeps: who I am connected to, what I have sent, what I have received, how much more I can take. When the states agree, the connection exists; when they do not, it breaks.

Three-way handshake: confirm both sides are there

First, the initiator says: I want to connect, my sequence number is X.

Second, the other side replies: received. I want to connect too, my sequence number is Y, and I am ready to receive your X+1.

Third, the initiator replies: got your Y, I am starting.

These are SYN, SYN-ACK and ACK. Three steps are not politeness; they let both sides confirm: I can send and you can receive; you can send and I can receive. With one less step, a late duplicate request could be mistaken for a new connection.

Reliable does not mean “never lost”; it means “lost is noticed and repaired”

TCP treats data as a continuous stream of bytes, each with a number. When the receiver gets a segment, it acknowledges: I have up to this byte. If no acknowledgement arrives, the sender retransmits; out-of-order data is reordered by number; duplicates are dropped.

The receiver has a limited buffer and tells the sender how much more it can take — that is the window. When the network is congested, the sender slows down instead of retransmitting harder. Together these mechanisms make reliable delivery.

But reliability has a precondition: the connection is still there. Pull the cable or crash the process, and TCP reports an error or times out; it does not pretend success.

A lot of packet loss is created by the receiving end

In device integration, TCP reliability only guarantees that bytes arrive in order. It does not guarantee that those bytes happen to form one complete message. When we process a TCP byte stream, the order is: find the protocol header, then keep reading according to the length defined in that header; if the buffer does not have enough bytes, wait for the next batch instead of deciding early; once a full frame is assembled, verify the checksum; only if the checksum fails do we drop that segment and look for the next header.

Many implementations do the opposite: find the header, read the length, take a slice immediately, and if the length does not match, throw it away and look for the header again. But TCP may have delivered only half a frame, so of course the length does not match. That throw may discard a frame that could have been completed, and the next header search can easily lose alignment. The symptom is occasional packet loss and data that does not add up.

We do not decide before the frame is complete. Wait first, verify second, discard only when verification fails. The direct benefit: with jitter, split packets and partial frames, far less data that could have been reassembled gets thrown away — fewer cases of “the device sent it, but the platform is missing one”.

The precondition is that the header can be located and the length field is trustworthy. If a device message has no length field, or the length definition is ambiguous, it still has to be handled according to the vendor manual.

Closing takes both sides

Closing is not hanging up at once. Each direction closes separately: one side says “I have no more data”, the other acknowledges; when the other side finishes, it acknowledges again. That is the four-way close.

That is why packet captures show a lingering state after close. It is not a dirty shutdown; both sides are waiting for the final acknowledgement.

IP finds the machine, the port finds the program

The IP address finds the machine; the port finds the program on it. Web uses 80 and 443, MQTT uses 1883 and 8883, Modbus TCP uses 502, SSH uses 22.

“Port open” only means you can reach the door. It does not mean the program behind it will receive you, let alone understand your message. Firewalls, NAT and port mapping — if any layer blocks, the connection never forms.

TCP only carries bytes, not “messages”

This is the easiest thing to miss in device integration: TCP is a byte stream and does not preserve message boundaries.

The sender writes 100 bytes in three calls; the receiver may read all 100 at once, or 40 first and 60 later. The sender writes two messages; the receiver may get both glued together. This is sticky/split packets, not a TCP bug — it is how TCP works.

So the application must define: how long a message is, where it ends, how it is checked. Common approaches are a fixed header plus a length field, or a delimiter, plus CRC or a checksum. Without that agreement, TCP can be up and the data still cannot be assembled into a complete message.

What TCP can do

Web, APIs, databases, file transfer, remote login and MQTT mostly run on TCP. Devices sending collected data to the cloud often use TCP too.

It suits cases where order and completeness matter more than speed: an order cannot lose a line, an alarm cannot arrive out of order, a configuration table cannot be half missing.

It does not suit everything: voice and video care about latency and can tolerate a lost frame, so they usually use UDP or QUIC; one-to-many broadcast and multicast are not TCP's job. It is not encryption; if logins, payments or device commands need secrecy, add TLS.

On site, TCP is up but no data arrives — what to check

First the connection: right IP and port, is the server listening, did the firewall and NAT let it through.

Then the handshake: did the three-way handshake complete, did TLS fail, is the certificate still valid.

Then the message: what format is the device sending, are the header, length and checksum right, which protocol is the platform using to parse it.

Finally the business layer: is the heartbeat on time, was the device kicked by the server, did the parsed data land on the right point.

Keep that order. If the connection never formed, changing parser rules means nothing; if it formed but the message does not match, restarting a hundred times will not help.

TCP is the road, the protocol is the language

HG IoT supports TCP and standard MQTT access. On the TCP side, the device reports in an agreed message format, the platform parses it, then maps it into the device model through a point table.

Once the data is in, you get real-time values, history curves, online/offline records, threshold alarms, remote IO and OTA. Accounts can run on public cloud, or deploy privately with Docker and keep the data in your own server room.

The boundary also needs to be clear: TCP only delivers bytes. What the message looks like depends on the device vendor's communication manual. We do not reverse-engineer protocols or modify device hardware; whether a device can be connected starts with a message sample and a point table.

Many projects stall not because TCP is down, but because the road is open and the language does not match. Get one sentence right before talking about a hundred devices.

Start with one device

No need to connect the whole plant at once. Pick one device, capture a raw message, confirm the header, length, checksum and field meanings, then parse it into one correct value on the platform.

Once one value is right, copying the rest means something.

If you have a TCP device at hand, send the model, communication manual and a message sample. We will first see whether the road works, then discuss the schedule.

A similar project to scope?

Send the scenario, the scale and the systems you already run. We will come back with an executable plan and a time estimate — no contract needed first.