The assignment consisted of implementing a simple chat with Sockets based on a client/server communication. We also needed to insert some functionalities into the application, like changing the name of the user, listing the active clients connected to the chat and even to start a private chat with a specific user. There is no direct communication between clients, all messages need to pass through the server to reach the other end.
Now let’s start to talk about the code!
The strategies to create the system involves using threads to communicate the clients and server parallelly with the help of sockets:
# import libraries from socket import * from threading import Thread # thread
Beginning with the server, the following lines were used for the main configuration. It is needed to describe the server IP, port and operating protocol (TCP in our case):
# Server IP serverName = '' # Server port to connect serverPort = 12000 # TCP protocol serverSocket = socket(AF_INET,SOCK_STREAM) # Bind the Server IP with its port serverSocket.bind((serverName,serverPort)) # Ready to receive connections serverSocket.listen(1)