add SO_KEEPALIVE

This commit is contained in:
Cheng Liangyu 2018-04-25 17:50:59 +08:00
parent f6b606b364
commit c7ce8a8343

View File

@ -563,6 +563,7 @@ class TCPServer:
# Only works if self.address == "" # Only works if self.address == ""
self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
self.socket.setsockopt(IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) self.socket.setsockopt(IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
self.socket.bind(self.address) self.socket.bind(self.address)
except socket.error: except socket.error:
@ -574,6 +575,7 @@ class TCPServer:
# Binding to an IPv6 socket failed, lets fall back to IPv4. # Binding to an IPv6 socket failed, lets fall back to IPv4.
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
self.socket.bind(self.address) self.socket.bind(self.address)
self.address = self.socket.getsockname() self.address = self.socket.getsockname()