But if the socket has been sending data , the keepalive does not work, until the transmit buffer is full before disconnecting
int keepalive = 1; // 开启keepalive属性
int keepidle = 5; // 如该连接在5秒内没有任何数据往来,则进行探测
int keepinterval = 1; // 探测时发包的时间间隔为1 秒
int keepcount = 5; // 探测尝试的次数.如果第1次探测包就收到响应了,则后4次的不再发
socklen_t optlen = sizeof(socklen_t);
if (setsockopt(m_Socket, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive, optlen) < 0 ||
setsockopt(m_Socket, SOL_TCP, TCP_KEEPIDLE, (void *)&keepidle, optlen) < 0 ||
setsockopt(m_Socket, SOL_TCP, TCP_KEEPINTVL, (void *)&keepinterval, optlen) < 0 ||
setsockopt(m_Socket, SOL_TCP, TCP_KEEPCNT, (void *)&keepcount, optlen) < 0)
{
ClosePort();
return false;
}
------ Solution ------------------------------------- -------
according to your analysis of the phenomenon I feel like it works : If you do not set the keep alive, might you have in your socket ( obstructive ) above , the receiver : recv has been blocked can not be returned unless the peer closes a connection , because I do not know socket recv broken .
------ Solution ---------------------------------------- ----
enable this option to allow TCP connections on TCP socket send " keep alive " packets . Setting of this option value is stored in the transmission control block sk_flags of SOCK_KEEPOPEN bit , while according to the keep-alive function is enabled to determine whether to open the keep-alive timer .
keep-alive timer is selected in the application process SO_KEEPALIVE socket option is to take effect. If the connection is idle for more than 2 consecutive hours, the keep-alive timer expires, the connection sends a probe packet , forcing the responding .
------ For reference only -------------------------------------- -
other comments you
------ For reference only ---------------------------------------
I encountered the same problem, Finally how LZ solution ?
------ For reference only -------------------------------------- -
I have encountered similar problems , the landlord to solve it ?
------ For reference only -------------------- -------------------
roof
没有评论:
发表评论