Next, let's build a multi-client chat room. The goal of this program is to explore socket programming in further detail. This section also implements and discusses the client-server architecture that is so common in all network programs.
Our chat program will consist of a chat server, which listens for and receives all incoming messages on a given port.
It also maintains a list of chat clients that connect to the server. It then broadcasts any incoming messages to all connected clients:
Let's start with the code for the chat server.
A server runs on a remote host and has a socket bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.
Here's the code for a chat server (see 9.09_chat_server.py):
class ChatServer:
clients_list = []
last_received_message = ""...