May-07-2018, 02:53 PM
Hello,
I am trying to get this code to work. I am following a guide from YouTube to make a python Socket chat application, but get errors when I attempt to run. Attached are the outputs when running server and client sides of the application. Any help would be appreciated.
Outputs:
and
I am trying to get this code to work. I am following a guide from YouTube to make a python Socket chat application, but get errors when I attempt to run. Attached are the outputs when running server and client sides of the application. Any help would be appreciated.
Outputs:
Output:Traceback (most recent call last):
File "C:\py\SocketChat.Py", line 58, in <module>
client = Client(sys.argv[1])
NameError: name 'Client' is not defined
and
Output:Traceback (most recent call last):
File "C:\py\SocketChat.Py", line 61, in <module>
server.run()
TypeError: run() missing 1 required positional argument: 'self'
The Code:import socket import threading import sys class Server: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connections = [] def __init__(self): self.sock.bind(('127.0.0.1', 10000)) self.sock.listen(1) def handler(self, c, a): while True: data = c.recv(1024) for connection in self.connections: connection.send(data) if not data: break def run(self): while True: c, a = self.sock.accept() cThread = threading.Thread(target=self.handler, args=(c, a)) cThread.daemon = True cThread.start() self.connections.append(c) print(self.connections) class Client: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def sendMsg(self): while True: self.sock.send(bytes(input(""), 'utf-8')) def __init__(self, address): self.sock.connect((address, 10000)) iThread=threading.Thread(target=self.sendMsg) iThread.daemon=True iThread.start() while True: data=self.sock.recv(1024) if not data: break print(data) if(len(sys.argv) > 1): client = Client(sys.argv[1]) else: server = Server server.run()