Skip to content

Commit af0cdc3

Browse files
committed
Merge branch 'feature/httpd_support_reuse_addr' into 'master'
feat(httpd): Allow binding to same address and port upon restarting server without delay See merge request sdk/ESP8266_RTOS_SDK!1675
2 parents 35375c1 + d93c5bb commit af0cdc3

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

components/esp_http_server/src/httpd_main.c

+10
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ static esp_err_t httpd_server_init(struct httpd_data *hd)
262262
.sin_port = htons(hd->config.server_port)
263263
};
264264
#endif /* CONFIG_LWIP_IPV6 */
265+
266+
/* Enable SO_REUSEADDR to allow binding to the same
267+
* address and port when restarting the server */
268+
int enable = 1;
269+
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0) {
270+
/* This will fail if CONFIG_LWIP_SO_REUSE is not enabled. But
271+
* it does not affect the normal working of the HTTP Server */
272+
ESP_LOGW(TAG, LOG_FMT("error in setsockopt SO_REUSEADDR (%d)"), errno);
273+
}
274+
265275
int ret = bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
266276
if (ret < 0) {
267277
ESP_LOGE(TAG, LOG_FMT("error in bind (%d)"), errno);

0 commit comments

Comments
 (0)