yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

7. TCP Server Experiment

TCP Server Experiment


TCP Server Experiment1. TCP Server Overview2. Core Concepts2.1 TCP Server Communication Model2.2 Configuration Parameters2.3 Core API2.4 Command Set3. Hardware Dependencies3.1 Pin Mapping4. Project Architecture and Flow5. Source Code Explained5.1 Creating the Server Socket5.2 Accepting New Connections5.3 Polling Connected Clients5.4 Command Processing6. Operating Procedures6.1 Environment Preparation6.2 Flashing and Running6.3 Expected Results6.4 Serial Output Example7. Common Problems


1. TCP Server Overview

Unlike the client, TCP (Transmission Control Protocol) server listens on a specified port and waits for clients to actively connect. In this experiment the ESP32-S3 acts as a TCP Server, and the PC network debugging assistant connects as a Client, achieving one-to-many bidirectional send/receive — supporting up to 3 clients connecting simultaneously; the server can send a welcome message to each client and process commands.

Typical application scenarios:

Application TypeExample
Device managementMultiple PCs monitor one ESP32 device at the same time
HTTP serverBrowser accesses the ESP32 web page
Data relayReceives data from multiple clients and aggregates/forwards it

2. Core Concepts

2.1 TCP Server Communication Model

                    listen :8080
┌──────────┐    TCP connection 1   ┌──────────┐
│ 电脑 1    │ ─── connect() ----→   │          │
└──────────┘                       │ ESP32-S3 │
┌──────────┐    TCP connection 2   │ (Server) │
│ 电脑 2    │ ─── connect() ----→   │          │
└──────────┘                       └──────────┘
  • bind(("0.0.0.0", port)) binds the port; "0.0.0.0" means listening on all network interfaces
  • listen(n) sets the maximum waiting queue
  • accept() blocks waiting for a client connection and returns (conn, addr)

2.2 Configuration Parameters

ParameterDescriptionValue Used in This Experiment
LISTEN_PORTESP32 listen port8080
MAX_CLIENTSMax simultaneous connections3

2.3 Core API

​x
import socket
​
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("0.0.0.0", 8080))       # bind port
server.listen(3)                       # start listening
conn, addr = server.accept()           # accept client
conn.sendall(b"hello")                 # send to client
data = conn.recv(512)                  # receive from client
conn.close()                           # close client connection

2.4 Command Set

The client (PC network assistant) sends the following text commands; the ESP32 server dispatches them in process_command() after receiving them:

CommandFunctionReply Content
PINGConnectivity checkPONG
TIMEQuery runtime statusRuntime (s) + free memory (KB)
INFOQuery device infoFree memory + total memory + usage + Flash capacity
OtherEchoReceived: {original message}

Command matching is case-sensitive, fully consistent with the TCP client experiment.


3. Hardware Dependencies

Same as the TCP client — ESP32-S3 built-in WiFi + PC network assistant (TCP Client mode).

3.1 Pin Mapping

None — pure network communication using built-in WiFi.


4. Project Architecture and Flow

xxxxxxxxxx
Script execution (single-file .py)
  |
  |- wifi_connect(): connect to WiFi (STA mode)
  |- create Server Socket -> bind(port) -> listen(3)
  |
  |- while True:
       |- accept(): non-blocking attempt to accept new connections
       |    |- new connection -> send welcome message -> add to clients[]
       |
       |- poll existing clients:
       |    |- recv_msg() -> process_command() -> send_msg()
       |    |- disconnected -> close() -> remove from clients[]
       |
       |- sleep(0.1s)

5. Source Code Explained

Full source code: Source Code -> MicroPython -> 4.Network Protocols -> TCP_Server -> TCP_Server.py

5.1 Creating the Server Socket

bind("0.0.0.0", port) listens on all network interfaces; listen(3) allows 3 queued connections; settimeout(1) makes accept() non-blocking.

xxxxxxxxxx
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.settimeout(1)                         # non-blocking accept
​
server.bind(("0.0.0.0", LISTEN_PORT))        # bind
server.listen(3)                               # start listening
​
ip = network.WLAN(network.STA_IF).ifconfig()[0]
print(f"[TCP] Listening on {ip}:{LISTEN_PORT}")

5.2 Accepting New Connections

accept() returns (conn, addr); new connections get a 0.5s timeout and are added to the clients list.

xxxxxxxxxx
clients = []                                  # (conn, addr) list
​
while True:
    try:
        conn, addr = server.accept()          # non-blocking accept
        conn.settimeout(0.5)
        clients.append((conn, addr))
        print(f"[TCP] New client connected: {addr[0]}:{addr[1]}")
        send_msg(conn, f"Welcome to the ESP32-S3 server", addr)
    except OSError:
        pass                                  # timeout, no connection, continue

5.3 Polling Connected Clients

Iterates through all connected clients, checking each one with recv for incoming data. After disconnection, clients are removed from the list in reverse order to avoid index confusion.

xxxxxxxxxx
dead_conns = []
for i, (conn, addr) in enumerate(clients):
    msg = recv_msg(conn, addr)
    if msg is False:                          # disconnected
        conn.close()
        dead_conns.append(i)
    elif msg:
        reply = process_command(msg)          # command dispatch
        if reply:
            send_msg(conn, reply, addr)
​
for i in reversed(dead_conns):                # delete in reverse to avoid index confusion
    del clients[i]

5.4 Command Processing

Same process_command() as the TCP client — PING -> PONG, TIME -> runtime + memory, INFO -> device info.


6. Operating Procedures

6.1 Environment Preparation

Thonny IDE steps: See MicroPython -> 1. Before Development -> 3.Thonny IDE.

  1. Open the network debugging assistant网络调试助手图标 on the PC, select TCP Client mode
  2. The target IP is the ESP32-S3's IP (printed on the serial port), and the target port is 8080

6.2 Flashing and Running

  1. Modify WIFI_SSID / WIFI_PASS in the code

    配置区

  2. Run TCP_Server.py and note the IP printed on the serial port

    对应IP号

绑定主机

6.3 Expected Results

After the PC network assistant connects to the ESP32, it receives a welcome message; sending PING returns PONG.

网络助手pin pong调试

6.4 Serial Output Example

xxxxxxxxxx
===== ESP32-S3 TCP Server =====
[WiFi] Connecting to 'Yahboom2' ...
[WiFi] Connected! IP: 192.168.11.229
[TCP] Listening on 192.168.11.229:8080
[TCP] New client: 192.168.11.183:49837
[Sent -> 192.168.11.183] Welcome! ESP32-S3 Server | clients: 1
[TCP] New client: 192.168.11.183:49840
[Sent -> 192.168.11.183] Welcome! ESP32-S3 Server | clients: 2
[Recv <- 192.168.11.183] TIME
[Sent -> 192.168.11.183] ESP32-S3 status
  uptime: 360s
  Free memory: 8116 KB
[Recv <- 192.168.11.183] INFO
[Sent -> 192.168.11.183] ESP32-S3 Server
  Free memory: 8114 KB
  Total memory:   8127 KB
  Usage:   0.2%
  Flash:    16 MB

串口打印


7. Common Problems

SymptomCauseSolution
Client cannot connectFirewall blocking or wrong IPCheck that the IP is the value printed by the ESP32, disable the firewall
bind failsPort already in useUse another port (e.g. 8081)
No log after client disconnectsrecv() returns empty but disconnection is not detectedThe code detects disconnection via recv() == b"", which is normal
TCP Client Experiment
HTTP Client Experiment

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文