use getaddrinfo to know if we should use ipv4 or v6 for zeroconf

This commit is contained in:
Yann Leboulanger 2008-04-06 21:33:40 +00:00
parent 7dbef236af
commit da966efaa7
1 changed files with 4 additions and 5 deletions

View File

@ -59,16 +59,15 @@ class ZeroconfListener(IdleObject):
self.conn_holder = conn_holder self.conn_holder = conn_holder
def bind(self): def bind(self):
try: ai = socket.getaddrinfo(None, self.port, socket.AF_UNSPEC,
self._serv = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG)[0]
except socket.error: self._serv = socket.socket(ai[0], ai[1])
self._serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._serv.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) self._serv.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
self._serv.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self._serv.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
# will fail when port is busy, or we don't have rights to bind # will fail when port is busy, or we don't have rights to bind
try: try:
self._serv.bind(('', self.port)) self._serv.bind((ai[4][0], self.port))
except Exception, e: except Exception, e:
# unable to bind, show error dialog # unable to bind, show error dialog
return None return None