Python Forum
Chat box locking up with multiple clients running
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Chat box locking up with multiple clients running
#1
I am working on a GUI based chat program.
I am using someone else's server which has worked well for many people so I am assuming the problem is with my client's code.
When I run a single instance of the client it works perfectly, but if I run two instances of the client on the same computer the listener stops responding when the 2nd client logs in.
# server is from socket module
# chat_box is a tkinter ListBox
class listener_thread(threading.Thread):
    def __init__(self, server, chat_box):
        super(listener_thread, self).__init__()
        self.server = server
        self.chat_box = chat_box

    def run(self):
        try:
            update = self.server.recv(1024)
            msg = update.decode("utf-8")
            if msg != "":
                self.chat_box.insert(END, msg)
        except Exception as e:
            print(e)
I've verified that the server is putting each client on a different port. The server is receiving the messages. When 'Michael' logs in and says 'Hi' it updates in his chat_box.
Though, the clients are no longer updating their histories after 'Dave' logs in.
Yet, the server continues to show that it is receiving the messages from both clients.
Output:
Michael - ('127.0.0.1', 56263) connected Hi Dave - ('127.0.0.1', 56264) connected Yo Hi
I'm not sure on which board I should place this question; it might be networking or it might be threading.
The network connection is working properly. It just locks up the list_box updating threads.
No exceptions are being thrown.
Reply
#2
I solved my own problem.
I needed to make the chat_history_listbox as a ListBox initially, instead of None
I needed to put the receive code into a function, with a loop and an exit condition
def receive_func():
    global server, chat_history_listbox
    while True:
        try:
            update = server.recv(1024)
        except OSError as e:
            update = None
            break
            connect()
        msg = update.decode("utf-8")
        if msg != "":
            chat_history_listbox.insert(END, msg)
I needed to make the thread call a function and make it a daemon
listener = Thread(target=receive_func, daemon=True)
listener.start()
This got it working with multiple clients
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to split socket data onto multiple clients for separate processing ConcealedFox70 0 1,903 Jan-11-2022, 08:26 PM
Last Post: ConcealedFox70
  Chat (Client-Server) andresdrr 3 3,696 Aug-26-2019, 02:01 PM
Last Post: ThomasL
  Issues in Chat App for Local Network iMuny 2 2,630 Aug-19-2019, 10:25 AM
Last Post: iMuny
  Socket won't receive data suddenly with multiple clients SquareRoot 0 2,768 Sep-06-2017, 09:09 PM
Last Post: SquareRoot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020