Chapter 4. Software framework
When ESP32 & ESP8266 are used as TCP servers, how can the ports be used again immediately after they
are released?
• On both ESP32 and ESP8266, TCP ports are not immediately released after being closed. They remain in a
TIME_WAIT state for a certain period of time. During this period, binding a socket with the same port and
source address as before will fail. This is to ensure that you can receive the FIN signal sent by the server and
can close the connection successfully. In this state, the port cannot be immediately reused. To address this
issue, the socket option “SO_REUSEADDR”should be used, which allows the device to bind a TCP socket
with the same port and source address in the TIME-WAIT state.
• Therefore, a TCP server program can set the“SO_REUSEADDR”socket option before calling bind() to bind
the same port.
• Alternatively, the setsockopt() function can be used to set the SO_REUSEADDR option. Here is an example:
int reuse = 1;
if (setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
ESP_LOGE(TAG, "setsockopt(SO_REUSEADDR) failed");
return ESP_FAIL;
}
In the above code,“socket”means a socket that has already been created, and“reuse”is an integer variable
with a value of 1, indicating that the SO_REUSEADDR option is enabled. If the setsockopt() function returns
a negative value, it means that the setting has failed. Enabling the SO_REUSEADDR option allows the port
to be immediately reused after being closed. However, there are some potential risks. If another connection
uses the same port while it is still in the TIME_WAIT state, it may cause packet confusion. Thus, it’s better
to make choice based on actual situations.
After downloading the tcp_client example for an ESP32 module, I connected the module to the router via
Wi-Fi and performed a Ping test on the computer. Then the it shows high latency sometimes, what is the
reason?
When Wi-Fi is connected, Power Save mode will be turned on by default, which may cause high
Ping delay. To solve this issue, you can turn o Power Save mode to reduce the delay by calling
esp_wi_set_ps(WIFI_PS_NONE) after esp_wifi_start().
How can I set static IP when using ESP-IDF?
For details, please refer to static_ip example.
Does ESP32 have an LTE connection demo?
• Yes. For ESP-IDF v4.2 and later versions, please refer to the example/protocols/pppos_client demo.
• For ESP-IDF v5.0 and later versions, please refer to examples in the esp-protocols repo.
Will memory leak occur when ESP32 TCP repeatedly closes and rebuilds socket (IDF 3.3)?
In ESP-IDF v3.3, every time a socket is created, a lock will be assigned, given that this internal socket
array has not been assigned any lock before. This lock will not be reclaimed after the socket is released.
Thus, next time the same socket array is allocated, the previous lock will be used again. That is to say,
every time a new socket array is allocated and released, there will be one lock memory used. After all
socket arrays being allocated, there will be no memory leak any more.
Espressif Systems 94
Submit Document Feedback
Release master