def __init__(self): ExternalWindows.getValuesFromUser() self.host = ExternalWindows.return_ip() self.port = ExternalWindows.return_port() try: self.s = socket.socket() self.s.connect((self.host, self.port)) data = self.s.recv(3).decode() if data == 'HLO': print('[Network]Connection with %s:%s established.' % (self.host, self.port)) data = self.s.recv(1024).decode() UserNames = data.split() while True: ExternalWindows.get_nickname_from_user() self.ID = ExternalWindows.return_nickname() if (self.ID in UserNames): ExternalWindows.show_error_box("User name is taken") continue break self.s.sendall(self.ID.encode()) print("Received ID is : " + self.ID) except: ExternalWindows.show_error_box("Could not connect to server") exit()
def __init__(self): ExternalWindows.getValuesFromUser() self._host = ExternalWindows.return_ip() self._port = ExternalWindows.return_port() # Here we attempt to establish a connection # We open a socket in the given port and IP # And start by checking to see if we received a greeting 'HLO' # Afterwards the server will send a list with all the names of the connected users # This is done to avoid repeating names when creating a new user # After the id has been chosen it responds to the server so it can add to the list of clients try: self.s = socket.socket() self.s.connect((self._host, self._port)) data = self.s.recv(3).decode() if data == 'HLO': print('[Network]Connection with %s:%s established.' % (self._host, self._port)) data = self.s.recv(1024).decode() UserNames = data.split() while True: ExternalWindows.get_nickname_from_user() self._ID = ExternalWindows.return_nickname() if (self._ID in UserNames): ExternalWindows.show_error_box("User name is taken") continue break self.s.sendall(self._ID.encode()) print("Received ID is : " + self._ID) except SystemExit: exit() except: ExternalWindows.show_error_box("Could not connect to server") raise Exception("Connection Failed")