Next, the server enters into a loop which allows it to keep receiving and sending messages to clients. The newly connected client name is stored in the client names list and the "client name display" is updated in Line 16. The server receives the client's name using the socket.recv method (Line 11 where socket is the connected client socket) and sends a welcome message using the nd method to the client (Line 12). When a new client connects, it sends its name to the server. Let's go through the actual sending/removing function now that we've got the helper functions out of the way. update_client_names_display(…) basically updates the clients name display when a client connects (add) or disconnects (remove). The server has an area for displaying connected clients name - let's call that "clients name display". This is particularly useful for handing two scenarios: (i) When a client disconnects, we need to know the client index in the client list so we can safely remove the client (ii) We want to send a client message to ALL other connect clients EXCEPT the client that sends the message (i.e. get_client_index(…) returns the current client index in the clients list. I have also included two helper functions which are (hopefully) self explanatory :).
#MULTI CHAT CLIENT PY CODE#
5 shows the code snippet for sending/receiving clients messages. 5: Send and receive message code snippetįig. Next, let's look at the accept_clients function. Thus, we need a new thread for accepting new clients connection requests (Line 25) and another thread for handling sending/receiving clients messages. As stated earlier, we want to be able to keep listening for and accepting new clients connection requests while sending and receiving messages from already connected clients. We bind the server to the host address and port number in Line 22 and start listening for clients connection in Line 23. The server socket is created in Line 21 for IPv4 ( socket.AF_INET) and TCP protocol ( socket.SOCK_STREAM). The thread library is imported in Line 2. Still not sure why we need threads? Then see the official python documentation. Thus, we need "threads" for such parallel tasks if we do not want to clog the Main and GUI thread. Our awesome server will be able to accept new client connection requests while processing messages from connected clients. See the official documentation for more info on the subject. In simple terms, a socket provides us with a bi-direction communication channel for sending and receiving data. This is important for handing network communication. The video below shows the running application for four connected clients/users. This means that messages from a user are broadcasted to other users. The group chat application will allow multiple users to connect to the server and chat with all other online users. In any case, it's still very easy to understand. I promise, creating GUI components using Tkinter is a breeze especially if you have worked with Java Swing, C# Window Form or HTML/CSS.
#MULTI CHAT CLIENT PY HOW TO#
I will be using the popular Python GUI library: Tkinter for the User Interfaces - who likes boring command-line apps anyways :) If you haven't, then you should see my first tutorial titled " Learn Python by Building a GUI Guessing Game with Tkinter" where I covered how to use the Tkinter library in greater detail. To learn more about python and how to set it up, please see the official documentation. I am going to assume that you have Python installed and setup on your computer. I think it's always best to approach programming as a concept instead of a language. The good news is that the concepts are also applicable to other programming languages. The goal of this tutorial is to get you started with Network Programming (client/server application model) in Python. Hey guys, I am going to show you how to build a multi-user group chat application in Python.