Skip to content

What Is TCP/IP

TCP/IP is the protocol family (a set of rules) used by the Internet: devices exchange data according to these rules. It is organized in layers, each with its own job — like sending a package: the application layer writes the content (HTTP/MQTT data), the transport layer attaches the shipping label (TCP/UDP ports), the network layer writes the destination address (IP address), and the link layer does the actual transport (Wi-Fi/Ethernet).

In plain words: the IP address decides "who to send to", the port number decides "which program", and TCP/UDP decides "how it is delivered" — TCP is like registered mail (receipt, reliable, a bit slower), UDP is like regular mail (no guarantee, fast).

TCP vs. UDP

  • TCP (Transmission Control Protocol): connection-oriented. It establishes a connection with a three-way handshake first; data arrives in order and is retransmitted if lost. Suitable for reliable applications: file transfer, web pages, MQTT, remote login.

  • UDP (User Datagram Protocol): connectionless. Once sent, it is not tracked; delivery is not guaranteed and packets may arrive out of order. Suitable for real-time/broadcast scenarios: audio/video, games, device discovery (broadcast/multicast).

  • Port: a number from 0 to 65535; ports below 1024 are common service ports (HTTP 80, HTTPS 443, MQTT 1883).

Socket Programming Model

The SDK uses lwIP's BSD Socket API (socket / bind / connect / listen / accept / send / recv, etc.), almost the same as writing network programs on a PC.

Client: socket() creates the socket → connect() connects to the server → send/recv exchange data.

Server: socket()bind() binds the port → listen() listens → accept() accepts a connection → recv/send communicates with the client.

Network Pages in This Section

The experiment pages related to TCP/IP in this section all share the same Wi-Fi example skeleton:

PageProtocolRoleSDK example
TCP ClientTCPClient (connects to a server)examples/wifi/sta/wifi_tcp
TCP ServerTCPServer (waits for connections)examples/wifi/sta/wifi_tcp
UDP ClientUDPClientSelf-authored
UDP ServerUDPServer (echo)examples/wifi/sta/wifi_udp
UDP Broadcast / UDP MulticastUDPOne-to-manySelf-authored

FAQ

  • Q: Which is better, TCP or UDP? Neither is universally better: choose TCP for reliability, UDP for speed/broadcast. Use TCP when delivery must be confirmed (e.g. control commands); use UDP for real-time audio/video and device discovery.

  • Q: What happens if the port is wrong? A client cannot connect to the server or receive data; a server that fails to bind prints a bind error. Common ports: HTTP 80, HTTPS 443, MQTT 1883, MQTTS 8883.

  • Q: Why can the TCP server only handle one client? The example is a single-connection demo (it accepts one client and loops); in real products, create a task per connection to support multiple clients.

Have questions?

For any other questions, visit the unified Q&A and discussion board: Ai-Thinker Discussions

Released under the MIT License. Build Time 2026-09-11 14:52:23