windows-server-2008 - 如何在 Windows Server 2008 上设置 Keepalive

标签 windows-server-2008 tcp winsock keep-alive

我有 win32 应用程序,其中 winsock 用于 TCP/IP 通信。 我正在使用 WSAIoctl 函数设置 keepalive 值,它在 Windows XP 中工作正常。

但是在 Win 上。 Server 2008 WSAIoctl 失败,并且 WSAGetLastError 返回 10022(无效参数),这没有任何意义。

我设置keepalive的代码如下;


// set per-connection keep-alive option 
tcp_keepalive keepAliveSettings; 
keepAliveSettings.onoff = 1; 
keepAliveSettings.keepalivetime = 500; 
eepAliveSettings.keepaliveinterval = 25;<p></p>

<p>int lastError = WSAIoctl(m_soc.chns[0], SIO_KEEPALIVE_VALS, &keepAliveSettings, sizeof(keepAliveSettings), NULL, 0, &BytesReturned, NULL, NULL);
          if(lastError == SOCKET_ERROR) {
                    lastError = WSAGetLastError();
                    Logger::Error("Setting keepalive failed ErrCode %d\n",lastError); }
</p>

编辑:

我找到了解决方案,也许它可以帮助遇到同样问题的其他人。我试图在异步之后设置 keepalive。连接调用,但现在我在连接调用完成后进行设置,这解决了问题。

来自 http://msdn.microsoft.com/en-us/library/ee470551(v=VS.85).aspx

On Windows Vista and later, the SO_KEEPALIVE socket option can only be set using the setsockopt function when the socket is in a well-known state not a transitional state. For TCP, the SO_KEEPALIVE socket option should be set either before the connect function (connect, ConnectEx, WSAConnect, WSAConnectByList, or WSAConnectByName) is called, or after the connection request is actually completed. If the connect function was called asynchronously, then this requires waiting for the connection completion before trying to set the SO_KEEPALIVE socket option. If an application attempts to set the SO_KEEPALIVE socket option when a connection request is still in process, the setsockopt function will fail and return WSAEINVAL.

最佳答案

我找到了解决方案,也许它可以帮助遇到同样问题的其他人。 我试图在异步之后设置 keepalive。连接调用,但现在我在连接调用完成后进行设置,这解决了问题。


来自 http://msdn.microsoft.com/en-us/library/ee470551(v=VS.85).aspx

On Windows Vista and later, the SO_KEEPALIVE socket option can only be set using the setsockopt function when the socket is in a well-known state not a transitional state. For TCP, the SO_KEEPALIVE socket option should be set either before the connect function (connect, ConnectEx, WSAConnect, WSAConnectByList, or WSAConnectByName) is called, or after the connection request is actually completed. If the connect function was called asynchronously, then this requires waiting for the connection completion before trying to set the SO_KEEPALIVE socket option. If an application attempts to set the SO_KEEPALIVE socket option when a connection request is still in process, the setsockopt function will fail and return WSAEINVAL.

关于windows-server-2008 - 如何在 Windows Server 2008 上设置 Keepalive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3173583/

相关文章:

tcp - TCP和Unix listen函数中Three way handshake的疑问

选择一个已被使用的本地源端口

c++ - 套接字编程 C/C++ - recv 函数在服务器中挂起

c - winsock recv 函数在相关数据之间接收垃圾

c++ - 在 winsock recv() 方法上调用了 abort()

ms-word - 如何使用 Lucene.NET 索引 Word 2003、2007 和 2010 文档

powershell - 在安装 Sharepoint 2010 之前卸载 PowerShell 1.0

java - 从机上的Datanode和Nodemanager无法连接到主机上的NameNode和ResourceManager

.net - 编译器失败,错误代码为 -1073741502(SharePoint 2010 SP1 在 Windows Server 2008 R2 上运行)

php - 为 Apache 设置 Windows 身份验证