def __init__(self): conn = s.socket(s.AF_INET, s.SOCK_STREAM) conn.setsockopt(s.SOL_SOCKET, s.SO_REUSEADDR, 1) conn.connect(('localhost', PORT)) fsock = conn.makefile() port = int(fsock.readline()) # Set up protocol for message passing self.pb = protocolBase() # Beauty sleep time.sleep(0.1) self.pb.connect('localhost', port)
def __init__(self, conn): """ Initialize the game server by setting up message passing protocol. """ # Get a free port hax hax = socket.socket(socket.AF_INET, socket.SOCK_STREAM) hax.bind(('localhost', 0)) port = hax.getsockname()[1] hax.close() # Set up protocol for message passing self.pb = protocolBase() # Send the port number to the connected client fsock = conn.makefile() fsock.write(str(port)+"\n") fsock.flush() # Wait for connection on the new port self.pb.wait_for_connect(port) # Close sockets no longer in use fsock.close() conn.close()