error (THIS IS PYTHON PROGRAMMING) line 54, in write self.sock.send(message.encode('utf-8')) OSError: [WinError 10038] An operation was attempted on something that is not a socket Tcl_AsyncDelete: async handler deleted by the wrong thread I know it is something small that I am missing, I just have no idea what it is. this is my server file: import threading import socket
I'm getting the error (THIS IS PYTHON
line 54, in write
self.sock.send(message.encode('utf-8'))
OSError: [WinError 10038] An operation was attempted on something that is not a socket
Tcl_AsyncDelete: async handler deleted by the wrong thread
I know it is something small that I am missing, I just have no idea what it is.
this is my server file:
import threading
import socket
#local network, to set for online utilize your IP address
host = '127.0.0.1'
port = 55555
#setting up the server, AF_INET for internet and SOCK_STREAM for TCP
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host,port))
#Starts the serrver
server.listen()
#list of info to manage
clients = []
nicknames = []
#sends messages to client connections
def broadcast(message):
for client in clients:
client.send(message)
#
def handle(client):
while True:
try:
#sends messages to the other clients, server just broadcasts the messages
message = client.recv(1024).decode('utf-8')
print(f"{nicknames[clients.index(client)]} says {message}")
broadcast(message)
except:
#Removes and closes clients from the server
index = clients.index(client)
clients.remove(client)
client.close
nickname = nicknames[index]
broadcast(f'{nickname} left the chat'. encode('utf-8'))
nicknames.remove(nickname)
break
#Main function that runs everything
def receive():
while True:
#accepts new clients to the server
client, address = server.accept()
print(f"connected with {str(address)}")
#Gets nickname from the client and adds to the list
client.send("NICK".encode('utf-8'))
nickname = client.recv(1024)#.decode('utf-8')
nicknames.append(nickname)
clients.append(client)
print(f"{nickname} has joined.")
broadcast(f"{nickname} has joined.\n".encode("utf-8"))
client.send("You are connected".encode('utf-8"'))
#Allows multiple
thread = threading.Thread(target=handle, args=(client,))
thread.start()
print("server is listening....")
receive()
and this is my client file:
import socket
import threading
import tkinter
from tkinter import simpledialog
import tkinter.scrolledtext
HOST = '127.0.0.1'
PORT = 55555
class Client:
def __init__(self, host, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
msg = tkinter.Tk()
msg.withdraw()
self.nickname = simpledialog.askstring("Nickname", "Choose your nickname: ", parent=msg)
self.running = True
gui_thread = threading.Thread(target=self.gui_loop)
receive_thread = threading.Thread(target=self.receive)
gui_thread.start()
receive_thread.start()
def gui_loop(self):
self.win = tkinter.Tk()
self.chat_label = tkinter.Label(self.win, text="Chat:")
self.chat_label.pack(padx=15, pady=5)
self.text_area = tkinter.scrolledtext.ScrolledText(self.win)
self.text_area.pack(padx=15, pady=5)
self.text_area.config(state='disabled')
self.msg_label = tkinter.Label(self.win, text="Message:")
self.msg_label.pack(padx=15, pady=5)
self.input_area = tkinter.Text(self.win, height=4)
self.input_area.pack(padx=15, pady=5)
self.send = tkinter.Button(self.win, text="Send", command=self.write)
self.send.pack(padx=15, pady=5)
self.win.protocol("WM_DELETE_WINDOW", self.stop)
self.win.mainloop()
def write(self):
message = f"{self.nickname}: {self.input_area.get('1.0', 'end')}"
self.sock.send(message.encode('utf-8'))
self.input_area.delete('1.0', 'end')
def stop(self):
self.running = False
self.win.destroy()
self.sock.close()
exit(0)
def receive(self):
while self.running:
try:
message = self.sock.recv(1024).decode("utf-8")
if message == 'NICK':
self.sock.send(self.nickname.encode("utf-8"))
else:
self.text_area.config(state='normal')
self.text_area.insert('end', message)
self.text_area.yview('end')
self.text_area.config(state='disabled')
except ConnectionAbortedError:
break
except:
print("An error occurred!")
self.sock.close()
break
client = Client(HOST, PORT)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps