fix bug when port reuse fail (#1037)

This commit is contained in:
GhostFlying
2017-01-05 23:26:47 -06:00
committed by Max Lv
parent 3b4af24fec
commit c807d2d6c4
+10 -9
View File
@@ -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) {