|
|
|
@ -160,6 +160,7 @@ int TCP::Connect(string hostport, int defaultport) {
|
|
|
|
|
int TCP::Connect() {
|
|
|
|
|
int err, s;
|
|
|
|
|
struct addrinfo hints, *res, *rp;
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
|
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6
|
|
|
|
@ -178,6 +179,12 @@ int TCP::Connect() {
|
|
|
|
|
//
|
|
|
|
|
for (rp = res; rp != NULL; rp = rp->ai_next) {
|
|
|
|
|
s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
|
|
|
|
|
|
// setup timeout to max 2 secs
|
|
|
|
|
timeout.tv_sec = 2;
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
|
|
|
|
|
|
|
|
|
|
if (s == -1) continue;
|
|
|
|
|
if (connect(s, rp->ai_addr, rp->ai_addrlen) != -1) {
|
|
|
|
|
sock = s;
|
|
|
|
|