From 980b8cef6b3fa8c2d0d428731d1f8164cadf91ca Mon Sep 17 00:00:00 2001 From: Todd White Date: Thu, 13 Aug 2026 21:52:25 -0400 Subject: [PATCH] Set the send timeout the way Winsock expects Winsock takes SO_SNDTIMEO as a DWORD of milliseconds rather than a struct timeval, and its optval is const char* rather than const void*. Casting the timeval compiles and then sets the timeout from the wrong bytes, so the value is built per platform. --- Source/CFSocket.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/CFSocket.c b/Source/CFSocket.c index ff83842..7e84b52 100644 --- a/Source/CFSocket.c +++ b/Source/CFSocket.c @@ -581,16 +581,28 @@ CFSocketSendData (CFSocketRef s, CFDataRef address, CFDataRef data, struct sockaddr* addr = NULL; socklen_t len; int err; +#if defined(_WIN32) + DWORD tv; +#else struct timeval tv; +#endif if (CFSocketIsValid (s) == false || address == NULL || data == NULL) return kCFSocketError; +#if defined(_WIN32) + /* Winsock expects a DWORD of milliseconds here, not a struct timeval. + */ + tv = (DWORD) (timeout * 1000.0); + err = setsockopt(s->_socket, SOL_SOCKET, SO_SNDTIMEO, (const char*) &tv, + sizeof(tv)); +#else tv.tv_sec = (int) floor(timeout); tv.tv_usec = (timeout - tv.tv_sec) * 1000000; - + err = setsockopt(s->_socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)); +#endif if (err != 0) return kCFSocketError;