Search code examples
pythonsocketsserverclient

Python Server not sending data/Client not receiving


Hey there so I want to detect if a nickname is already on a nickname list. If it's already in there, send a message to the client so it generates a new nickname. The problem is i believe the server does not send the message.

Server code:

try:
            client.send("--%||NICK||%--".encode('utf-8'))
 
            nickname = client.recv(1024).decode('utf-8')
            #Check if the same user is connected.
 
            if not nickname in nicknames:
                print('User is not in nicknames')
 
                print(f'[Server] {nickname} has connected to the server')
 
                #This line doesn't make anything yet, still here to remind me to redo the broadcast func.
                broadcast(f'[Server] {nickname} joined the room!\n'.encode('utf-8'))
                
                nicknames.append(nickname)
 
                thread = threading.Thread(target = handle, args=(client,))
                thread.start()
 
            else:
                print('User is already on nicknames')
 
                #Client will generate a new discrim and will tell the user to retry connecting.
 
                print('[Server] Sending user ALREADYONLINE message.')
                try:
                    client.send('--%||ALREADYONLINE||%--'.encode('utf-8'))
                
                except Exception as e:
                    print('An error occured sending ALREADYONLINE:')
                    print(e)
 
                #Get index for client and nickname
                index = clients.index(client)
                nickname = nicknames[index]
 
                print(f'[Server] {nicknames[clients.index(client)]} disconnected from the server.')
                
                #Remove client object and nickname from lists.
                clients.remove(client)
 
                #Close connection on client

Client code:

def receive(self):
        try:
            message = self.sock.recv(1024).decode('utf-8')
 
            if message == '--%||NICK||%--':
                print('--%||NICK||%--')
                self.sock.send(settings.get_setting(
                    'nickname').encode('utf-8'))
 
            elif message == '--%||ALREADYONLINE||%--':
                print('--%||ALREADYONLINE||%--')
                messagebox.showerror(
                    title='Nickname Error', message=f"There is already an user connected with the nickname: \"{settings.get_setting('nickname')}\". \n We've changed your last 4 numbers to allow your connection to the server.")
                self.sock.close()
                
            else:
                print('Message is not nickname')
 
        except Exception as e:
            print(e)

Client output:

--%||NICK||%--

Server output:

[Server] server_data folder exists.
Server started
[Server] Connected with ('ip', 'address')
User is already on nicknames
[Server] Sending user ALREADYONLINE message.
[Server] MrFellah el dev#1927 disconnected from the server.

I've tried changing the string the message sends, printing any errors but still, it seems it doesn't recieves the message. But the client closes the connection as expected.

Also, sorry for my disorganized code :)


Solution

  • EDIT: I solved it, I was missing a while True loop on the client function.