'TypeError: can't concat str to bytes (socket)

Earlier it was working with the same code but after a while it started throwing exception and the this error.. I don't fully understand how sockets work but I was trying to make a chat app by watching tutorial and reading docs, but here I am.. Can someone explain to me what is happening and what should i do to resolve the error...

[STARTED] Waiting for Connections... [CONNECTION] ('127.0.0.1', 40642) connected to the server at 1652256586.9077659 Tim: hello [EXCEPTION] can't concat str to bytes

Full traceback --: File "/home/hussain/PycharmProjects/chat_app/server/server.py", line 53, in clientCommunication broadCast(f'{name} has left the chat...', '') File "/home/hussain/PycharmProjects/chat_app/server/server.py", line 30, in broadCast client.send(bytes(name, 'utf-8') + msg) TypeError: can't concat str to bytes

from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
import time

from person import Person

# GLOBAL CONSTANTS
HOST = 'localhost'
PORT = 5500
ADDR = (HOST, PORT)
MAX_CONECTIONS = 10

BUFSIZ = 512

# GLOBAL VARIABLES
persons = []
SERVER = socket(AF_INET, SOCK_STREAM) 
SERVER.bind(ADDR) # set up server


def broadCast(msg, name):
    """
    send new messages to all clients
    :param msg: bytes["utf-8"]
    :param name: str
    :return:
    """
    for person in persons:
        client = person.client
        client.send(bytes(name, 'utf8') + msg) # <== (line 30) the root cause

def clientCommunication(person):
    """    
    Thread to handle all messages from clients
    :param person: Person
    :return: None
    """
    
    client = person.client
    
    # get persons name
    name = client.recv(BUFSIZ).decode('utf-8')
    person.setName(name)
    msg = bytes(f'{name} has joined the chat!', 'utf-8') <==this
    broadCast(msg, '') # broadcast welcome message 
    
    while True:
        try:
            msg = client.recv(BUFSIZ)
            
            
            if msg ==  bytes('{quit}', 'utf-8'): #<===== it does not enter here
                broadCast(f'{name} has left the chat...', '') # <= (line 53) this leads to line 30 in braodcast func
                client.send(bytes('{quit}', 'utf-8'))
                client.close()
                persons.remove(person)
                print(f'[DISCONNECTED] {name} disconnected')
                break
            else:
                broadCast(msg, name+': ')
                print(f'{name}: ', msg.decode('utf-8'))
        except Exception as e:
            print('[EXCEPTION]', e) #<========  this one
            break
            

def WaitForConnection():
    """
    Wait for connection from new clients, start new thread once connected
    :param SERVER: SOCKET
    :return: None
    """
    run = True
    while run:
        try:
            client, addr = SERVER.accept()
            person = Person(addr, client)
            persons.append(person)
            print(f'[CONNECTION] {addr} connected to the server at {time.time()}')
            Thread(target=clientCommunication, args=(person,)).start()
        except Exception as e:
            print('[EXCEPTION]', e)
            run = False
    print('SERVER CRASHED')


if __name__ == '__main__':
    SERVER.listen(MAX_CONECTIONS) # listen for connections
    print('[STARTED] Waiting for Connections...')
    ACCEPT_THREAD = Thread(target=WaitForConnection)
    ACCEPT_THREAD.start()
    ACCEPT_THREAD.join()
    SERVER.close()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source