TCP vs. UDP Compared With a Local Socket Test
TCP sets up a connection and guarantees ordered delivery; UDP sends independent datagrams with no handshake. A timed socket log shows the difference.
TCP opens a connection with a handshake, numbers every byte, acknowledges what arrives and retransmits what is lost, so data arrives complete and in order. UDP just sends independent datagrams: no handshake, no acknowledgment, no retransmission. TCP suits web pages, email and file transfers; UDP suits DNS lookups, voice, video calls and games, where a late packet is worse than a missing one.
Comparison
| TCP | UDP | |
|---|---|---|
| Connection | yes, three-way handshake first | none |
| Delivery | guaranteed or the connection fails | best effort, may be lost |
| Order | preserved | not guaranteed |
| Duplicates | removed | possible |
| Flow and congestion control | yes | no (the application decides) |
| Header size | 20–60 bytes | 8 bytes |
| Data unit | byte stream | separate messages (datagrams) |
| Typical uses | HTTP/1.1 and HTTP/2, SSH, SMTP | DNS, VoIP, games, QUIC (HTTP/3) |
What we saw in a socket test
We wrote a short Node.js script that first talks to a TCP server and then sends UDP datagrams, all over the loopback interface, and timestamps every event. This is the unedited output:
4.57 ms TCP client: connect() called
6.46 ms TCP server: connection accepted
6.59 ms TCP client: 'connect' event (handshake complete), sending
6.87 ms TCP server: received "hello"
7.13 ms TCP client: received "ack"
7.38 ms TCP client: closed
7.68 ms UDP sender: send() to 9102 with no handshake
8.07 ms UDP sender: send callback, error=none
8.17 ms UDP receiver: got "hello" (no connection, no ack)
109.60 ms UDP sender: send() to 9103 where nothing listens
109.97 ms UDP sender: send callback, error=none (datagram silently dropped)
313.93 ms connected UDP socket: send() to 9103
314.79 ms connected UDP socket: 'error' event ECONNREFUSED (from ICMP port unreachable)
-
TCPStep 1. Connect before sending
The client could not send until the 'connect' event fired, about 2 ms later. In between, the operating systems exchanged SYN, SYN-ACK and ACK. Our script logs the result of that handshake, not the packets themselves.
-
TCPStep 2. Data and reply
“hello” arrived and the server answered “ack” on the same connection. Our “ack” is application data; TCP's own acknowledgments are invisible to the program and happen underneath.
-
TCPStep 3. Orderly close
Both sides closed the connection with FIN segments. If one side had been killed instead, the other would see a reset; the related errors section links to that case.
-
UDPStep 4. Send immediately
The UDP sender called send() with no setup, and the receiver got the datagram 0.5 ms later. Nothing was acknowledged; the sender learned only that the datagram left.
-
UDPStep 5. Send to a closed port
Sending to port 9103, where nothing listens, also reported no error. The datagram was dropped. An unconnected UDP socket has no way to be told.
-
UDPStep 6. Connected UDP gets the hint
A UDP socket that called connect() first did receive ECONNREFUSED: the kernel turned the ICMP “port unreachable” reply into an error on that socket. It is a hint, not a delivery guarantee, and it arrives only if ICMP gets back.
Why TCP feels slower and UDP feels faster
On loopback, the handshake cost about 2 ms. On a real network it costs one full round trip before any data moves, and TLS usually adds at least one more. TCP also holds back later data while it waits for a lost segment to be retransmitted (head-of-line blocking), so one lost packet delays everything behind it. That is the right behaviour for a file and the wrong one for a voice call, where the listener would rather skip 20 ms of audio than hear everything late.
UDP applications choose their own trade-offs. DNS simply retries a query after a timeout. Games send the latest state many times per second, so a lost update is replaced by the next one. QUIC, the transport under HTTP/3, builds reliability and encryption on top of UDP while avoiding head-of-line blocking between streams.
Choosing between them
- Need every byte, in order? TCP.
- Short request/response that can be retried? UDP is fine (DNS).
- Real-time media or game state? UDP, with application-level handling of loss.
- Behind restrictive firewalls? TCP on port 443 gets through most often.
Related errors
Browser errors often name what happened at the TCP level. A refused connection means nothing was listening, as in the closed-port case above; the ERR_CONNECTION_REFUSED guide reproduces it. An abrupt TCP reset mid-connection is covered in the ERR_CONNECTION_RESET guide, and latency, jitter and loss over UDP in what ping means in gaming.
Sources
- RFC 9293: Transmission Control Protocol (TCP) — IETF
- RFC 768: User Datagram Protocol — IETF
- RFC 1122: Requirements for Internet Hosts — IETF. ICMP port unreachable for UDP
How we made this
Measured. We ran a Node.js script that opened a TCP connection and sent UDP datagrams over the loopback interface, logging each socket event with a high-resolution timestamp. The log below is the verbatim output. This is a socket-level test, not a packet capture: the handshake packets themselves were not recorded.
Performed by Claude, an AI assistant working in the BinConvert repository (macOS on Apple Silicon, Node.js 25.8.2, loopback interface), November 16, 2026.
This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; the socket test was re-run; event order and errors matched. Review: automated test suite and source check, November 16, 2026.
Spotted an error? Tell us and we will correct it.