From 5f77b45d24d7a51dbc3a5eaf8d1e13446b8078da Mon Sep 17 00:00:00 2001 From: steffen Date: Tue, 5 Mar 2019 20:45:27 +0000 Subject: [PATCH] some bug fixes --- Changelog | 4 ++++ tcp.cc | 16 +++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Changelog b/Changelog index 6ce3b9d..c674405 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,7 @@ +2019-03-04: +- fixed: TCP connect set up the sock struct too fast so threaded applications had some + problem if the connect went into a time out. + 2019-02-21: - fixed: TCP::Accept needed to set up the lenght of the sockaddr structure diff --git a/tcp.cc b/tcp.cc index 9578eb8..9d0fcef 100644 --- a/tcp.cc +++ b/tcp.cc @@ -167,8 +167,8 @@ int TCP::Connect() { hints.ai_flags = 0; hints.ai_protocol = 0; - s = getaddrinfo(remote_host.c_str(), remote_port.c_str(), &hints, &res); - if (s != 0) { + err = getaddrinfo(remote_host.c_str(), remote_port.c_str(), &hints, &res); + if (err != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); return 0; } @@ -177,11 +177,13 @@ int TCP::Connect() { // walk through all results until we could connect // for (rp = res; rp != NULL; rp = rp->ai_next) { - sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sock == -1) continue; - if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break; - close(sock); - sock = -1; + s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (s == -1) continue; + if (connect(s, rp->ai_addr, rp->ai_addrlen) != -1) { + sock = s; + break; + } + close(s); } freeaddrinfo(res);