From ab10b042beb4af2640709477883975eadf2eee15 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 22 May 2023 19:13:48 -0300 Subject: [PATCH] fix bind issue in W --- mongoose.c | 3 +-- src/sock.c | 3 +-- test/unit_test.c | 6 ++++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mongoose.c b/mongoose.c index b954d240..e67d70fd 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4554,8 +4554,7 @@ bool mg_open_listener(struct mg_connection *c, const char *url) { (char *) &on, sizeof(on))) != 0) { // "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE" MG_ERROR(("setsockopt(SO_EXCLUSIVEADDRUSE): %d %d", on, MG_SOCK_ERR(rc))); -#endif -#if defined(SO_REUSEADDR) && (!defined(LWIP_SOCKET) || SO_REUSE) +#elif defined(SO_REUSEADDR) && (!defined(LWIP_SOCKET) || SO_REUSE) } else if ((rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on))) != 0) { // 1. SO_REUSEADDR semantics on UNIX and Windows is different. On diff --git a/src/sock.c b/src/sock.c index d51f69e2..49624f9e 100644 --- a/src/sock.c +++ b/src/sock.c @@ -198,8 +198,7 @@ bool mg_open_listener(struct mg_connection *c, const char *url) { (char *) &on, sizeof(on))) != 0) { // "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE" MG_ERROR(("setsockopt(SO_EXCLUSIVEADDRUSE): %d %d", on, MG_SOCK_ERR(rc))); -#endif -#if defined(SO_REUSEADDR) && (!defined(LWIP_SOCKET) || SO_REUSE) +#elif defined(SO_REUSEADDR) && (!defined(LWIP_SOCKET) || SO_REUSE) } else if ((rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on))) != 0) { // 1. SO_REUSEADDR semantics on UNIX and Windows is different. On diff --git a/test/unit_test.c b/test/unit_test.c index e1c992d5..c5f90974 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -2137,10 +2137,12 @@ static void eh5(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { static void test_http_chunked_case(mg_event_handler_t s, mg_event_handler_t c, int req_count, const char *expected) { + char url[100]; struct mg_mgr mgr; - const char *url = "http://127.0.0.1:12344"; uint32_t i, crc = 0, expected_crc = mg_crc32(0, expected, strlen(expected)); struct mg_connection *conn; + static uint16_t port = 32344; // To prevent bind errors on Windows + mg_snprintf(url, sizeof(url), "http://127.0.0.1:%d", port++); mg_mgr_init(&mgr); mg_http_listen(&mgr, url, s, NULL); conn = mg_http_connect(&mgr, url, c, &crc); @@ -2799,7 +2801,7 @@ static void test_poll(void) { int count = 0, i; struct mg_mgr mgr; mg_mgr_init(&mgr); - mg_http_listen(&mgr, "http://127.0.0.1:12346", ph, &count); + mg_http_listen(&mgr, "http://127.0.0.1:42346", ph, &count); // To prevent bind errors on Windows for (i = 0; i < 10; i++) mg_mgr_poll(&mgr, 0); ASSERT(count == 10); mg_mgr_free(&mgr);