使用tcp keeplive 检测socket 异常断开

def init(sck)
    sck.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
    #连接60秒没有任何数据开始发送探测
    sck.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 60)
    #探测发包时间间隔为6秒
    sck.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 6)
    #探测次数为5次
    sck.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 5)

 

你可能感兴趣的:(socket)