b1daa872c111d286d607213ce0ba97d74133b9dd galt Thu Sep 12 22:24:24 2013 -0700 net.c: Adding read and write timeouts to the tcp open. One site was connecting but then failing to send a response and hanging indefinitely. diff --git src/lib/net.c src/lib/net.c index 2804ac4..84fbf79 100644 --- src/lib/net.c +++ src/lib/net.c @@ -175,30 +175,45 @@ else { warn("TCP non-blocking connect() error %d - %s", errno, strerror(errno)); close(sd); return -1; } } // Set to blocking mode again if (setSocketNonBlocking(sd, FALSE) < 0) { close(sd); return -1; } +//Set read and write timeouts +struct timeval timeout; +timeout.tv_sec = (long) (msTimeout/1000); +timeout.tv_usec = (long) (((msTimeout/1000)-timeout.tv_sec)*1000000); + +if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) + { + warn("setsockopt failed setting socket receive timeout\n"); + } + +if (setsockopt(sd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) + { + warn("setsockopt failed setting socket send timeout\n"); + } + return sd; } int netConnect(char *hostName, int port) /* Start connection with a server. */ { return netConnectWithTimeout(hostName, port, DEFAULTCONNECTTIMEOUTMSEC); // 10 seconds connect timeout } int netMustConnect(char *hostName, int port) /* Start connection with server or die. */ { int sd = netConnect(hostName, port);