Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Windows: the correct way to make TCP_KEEPALIVE work on MinGW
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Oct 29, 2011
1 parent 99b512e commit c0792e5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/win/tcp.c
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/win/winsock.h
Expand Up @@ -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
Expand Down

3 comments on commit c0792e5

@luislavena
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

@igorzi igorzi commented on c0792e5 Oct 31, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.