0

I'm creating a simple chat server and I keep getting this error.

Traceback (most recent call last):
  File "C:\Users\OneDrive\Desktop\Py Files\chat_server.py", line 47, in <module>
    recive()
  File "C:\Users\OneDrive\Desktop\Py Files\chat_server.py", line 41, in recive
    brodcast(f'{nickname} joined the chat!')
TypeError: brodcast() missing 1 required positional argument: 'message'

In the console of the server it says that the client joined the server. Right before giving me this massive error. The part before the error looks like this.

Connected with (Host and Port here)
Superior joined the chat!

One thing I've noticed is that the port it says it connected to in the console doesn't match the hard codded port on the server. However this could be the port for the computer connected to the server idk.

On the client end. it simply spams empty lines to the console. No errors though. I'm providing both the server and the client code just in case its something to do with the client.

Server:

import threading
import socket

host = 'Host'
port = Port

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen() 

clients = []
nicknames = []

def brodcast(str, message):
    for client in clients:
        client.send(message) 

def handle(client):
    while True:
        try:
            message = client.recv(1024)
            brodcast(message)
        except:
            index = clients.index(client)
            clients.remove(client)
            client.close()
            nickname = nicknames[index]
            brodcast(f'{nickname} left the chat'.encode('ascii')) 
            nicknames.remove(nickname)
            break 

def recive():
    while True:
        client, address = server.accept()
        print(f"Connected with {str(address)}")
        client.send('NICK'.encode('ascii'))
        nickname = client.recv(1024).decode('ascii')
        nicknames.append(nickname)
        clients.append(client)
        print(f'{nickname} joined the chat!')
        brodcast(f'{nickname} joined the chat!')
        client.send('Connected to the chat')

        thread = threading.Thread(target=handle, args=(client,))
        thread.start()

recive() 

Client:

import socket
import threading

nickname = input("Choose a display name: ")

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('Host', Port))

def recive():
    while True:
        try:
            message = client.recv(1024).decode('ascii')
            if message == 'NICK':
                client.send(nickname.encode('ascii'))
            else:
                print(message)
        except:
            print("An error occurred")
            client.close()
            break

def write():
    while True:
        message = f'{nickname}: {input("")}'
        client.send(message.encode('ascii'))

recive_thread = threading.Thread(target=recive)
recive_thread.start()

write_thread = threading.Thread(target=write)
write_thread.start() 
martineau
  • 119,623
  • 25
  • 170
  • 301
Superior125
  • 55
  • 3
  • 9

1 Answers1

0

In your definition, you probably meant to use the typing hints system if at all (this only hints editors here)

Incorporating a few comments

-    def brodcast(str, message):
+    def broadcast(message: str):

Example

>>> def test1(str, x): pass
...
>>> def test2(x: str): pass
...
>>> test1("foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: test1() missing 1 required positional argument: 'x'
>>> test2("foo")
ti7
  • 16,375
  • 6
  • 40
  • 68