From c807d2d6c4805629cef89282742d1e20e9e3af42 Mon Sep 17 00:00:00 2001 From: GhostFlying Date: Fri, 6 Jan 2017 13:26:47 +0800 Subject: [PATCH] fix bug when port reuse fail (#1037) --- src/manager.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/manager.c b/src/manager.c index 54f54cc9..b85898c0 100644 --- a/src/manager.c +++ b/src/manager.c @@ -356,6 +356,8 @@ create_and_bind(const char *host, const char *port, int protocol) } } + is_port_reuse = 0; + for (/*rp = result*/; rp != NULL; rp = rp->ai_next) { listen_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (listen_sock == -1) { @@ -379,24 +381,23 @@ create_and_bind(const char *host, const char *port, int protocol) LOGI("%s port reuse enabled", protocol == IPPROTO_TCP ? "tcp" : "udp"); } is_port_reuse = 1; - } else { - is_port_reuse = 0; } s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen); if (s == 0) { /* We managed to bind successfully! */ + + if (!is_port_reuse) { + if (verbose) { + LOGI("close sock due to %s port reuse disabled", protocol == IPPROTO_TCP ? "tcp" : "udp"); + } + close(listen_sock); + } + break; } else { ERROR("bind"); } - - if (!is_port_reuse) { - if (verbose) { - LOGI("close sock due to %s port reuse disabled", protocol == IPPROTO_TCP ? "tcp" : "udp"); - } - close(listen_sock); - } } if (!result) {