1

I am writing a simple chat using the Tkinter library and sockets.

enter image description here

When I connect for the first time to the server. The server processes incoming messages and they are displayed in the chat. I am trying to implement a Disconnect button that closes the window and closes the connection. I need the ability to reconnect by a client. However, an error is thrown:

Exception in thread Thread-9:
Traceback (most recent call last):
  File "user\anaconda3\lib\threading.py", line 932, in _bootstrap_inner self.run()
  File "user\anaconda3\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs)
  File "\server_.py", line 16, in listen_users send_to_users(data)
  File "\server_.py", line 10, in send_to_users user.send(data)
ConnectionAbortedError: [WinError 10053]

I have implemented a closing in a disconnect() Client code:

import socket
import threading
import tkinter as tkn

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
message = []

def connect_to_server():
    host = adress_text.get()
    port = 49094
    sock.connect((host, port))
    while True:
        data = sock.recv(1024)
        message.append(data.decode("utf-16"))
        message_text.delete(1.0, tkn.END)
        message_text.insert(1.0, '\n'.join(message))

def send_message():
    temp_mes = name_text.get() + " : " + entry_message.get()
    temp_mes = temp_mes.encode("utf-16")
    cur_mes = temp_mes
    entry_message.delete(0, tkn.END)
    sock.send(cur_mes)

root = tkn.Tk()

connecting = threading.Thread(target = connect_to_server)

def disconnect():
    sock.shutdown(socket.SHUT_RDWR)
    sock.close()
    root.destroy()

adress_text = tkn.Entry(root, width = 20)
adress_text.insert(1, 'localhost')
adress_text.grid(row = 1, column = 0, padx = 5, pady = 5, sticky = 'w')
name_text = tkn.Entry(root, width = 20)
name_text.insert(1, 'name')
name_text.grid(row = 2, column = 0, padx = 5, pady = 5, sticky = 'w')
conn_button = tkn.Button(root, text='Connect', command=connecting.start)
conn_button.grid(row = 1, column = 1, padx = 5, pady = 5)

disconn_button = tkn.Button(root, text='Disconnect', command=disconnect)
disconn_button.grid(row = 2, column = 1, padx = 5, pady = 5)

Server code:

import socket
import threading

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
users = []

def send_to_users(data):
    for user in users:
        user.send(data)

def listen_users(user):
    while True:
        data = user.recv(1024)
        print(data.decode("utf-16"))
        send_to_users(data)

def run_server(host, port):
    server.bind((host, port))
    server.listen(2)
    while True:
        usr_sock, addr = server.accept()
        print(addr)
        users.append(usr_sock)
        listen_acc_user = threading.Thread(
            target = listen_users,
            args = (usr_sock,)
        )
        listen_acc_user.start()
1
  • 3
    Accessing tkinter from multiple threads isn't ideal. It can cause tkinter to crash without giving you an error traceback. Also once the client disconnects you never remove it from your users list. So the server keeps trying to send the data to it even though the connection has been closed. I think that that might be causing the problem
    – TheLizzard
    Jun 17, 2021 at 8:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.