Skip to content

Commit

Permalink
esp_http_server : Allow binding to same address and port upon restart…
Browse files Browse the repository at this point in the history
…ing server without delay

Issue : Restarting the server without 30sec delay between httpd_stop() and httpd_start() causes EADDRINUSE error
Resolution : Use setsockopt() to enable SO_REUSEADDR on listener socket

Closes #3381
  • Loading branch information
anurag-kar committed May 6, 2019
1 parent cc8652d commit 0757e01
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions components/esp_http_server/src/httpd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ static esp_err_t httpd_server_init(struct httpd_data *hd)
.sin6_port = htons(hd->config.server_port)
};

/* Enable SO_REUSEADDR to allow binding to the same
* address and port when restarting the server */
int enable = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) < 0) {
/* This will fail if CONFIG_LWIP_SO_REUSE is not enabled. But
* it does not affect the normal working of the HTTP Server */
ESP_LOGW(TAG, LOG_FMT("error enabling SO_REUSEADDR (%d)"), errno);
}

int ret = bind(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (ret < 0) {
ESP_LOGE(TAG, LOG_FMT("error in bind (%d)"), errno);
Expand Down

0 comments on commit 0757e01

Please sign in to comment.