Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Windows: the correct way to make TCP_KEEPALIVE work on MinGW

  • Loading branch information...
commit c0792e54052bd3222c3b00e45f7e81c87187504a 1 parent 99b512e
@piscisaureus piscisaureus authored
Showing with 5 additions and 1 deletion.
  1. +1 −1  src/win/tcp.c
  2. +4 −0 src/win/winsock.h
View
2  src/win/tcp.c
@@ -71,7 +71,7 @@ static int uv__tcp_keepalive(uv_tcp_t* handle, SOCKET socket, int enable, unsign
if (enable && setsockopt(socket,
IPPROTO_TCP,
- SO_KEEPALIVE,
+ TCP_KEEPALIVE,
(const char*)&delay,
sizeof delay) == -1) {
uv__set_sys_error(handle->loop, errno);
View
4 src/win/winsock.h
@@ -37,6 +37,10 @@
# define SO_UPDATE_CONNECT_CONTEXT 0x7010
#endif
+#ifndef TCP_KEEPALIVE
+# define TCP_KEEPALIVE 3
+#endif
+
#ifndef IPV6_V6ONLY
#define IPV6_V6ONLY 27
#endif

3 comments on commit c0792e5

@luislavena

Hello,

Would you mind point me to the right MSDN documentation of TCP_KEEPALIVE? I've tried find any reference to it on MSDN without success, only SO_KEEPALIVE exists.

When run tests with SO_KEEPALIVE nothing failed, so I assumed was correct, seems I was not :-(

Will be great to bring these missing definitions back to mingw and mingw-w64 projects.

Thank you.

@phasis68

Hi,

Calling setsockopt with IPPROTO_TCP and TCP_KEEPALIVE on Windows does not setting keep-alive timeout.
it just enable or disalbe TCP keep-alive option.
To set the keep-alive timeout, you must call WSAIoctl with SIO_KEEPALIVE_VALS and tcp_keepalive struncture.

Refer to http://msdn.microsoft.com/en-us/library/windows/desktop/dd877220(v=vs.85).aspx
http://pgbouncer.sourcearchive.com/documentation/1.4/socket_8h_aa4bb40aa83a8fa42bfa5dff63c46c0fc.html

@igorzi

Actually, IPPROTO_TCP + TCP_KEEPALIVE does map to SIO_KEEPALIVE_VALS + tcp_keepalive.keepalivetime (for API compatibility). You can try setting keep-alive delay through WSAIoctl, and then read it with getsockopt(IPPROTO_TCP,TCP_KEEPALIVE). But yeah we should use winsock WSAIoctl function instead as recommended by MSDN.

Please sign in to comment.
Something went wrong with that request. Please try again.