Question: Task 1 : Set up the Server and Client Sockets Server - Side: Set up the server socket by creating a TCP socket using the

Task 1: Set up the Server and Client Sockets
Server-Side: Set up the server socket by creating a TCP socket using the socket module. Use AF_INET for the address family and SOCK_STREAM for the socket type. Bind the socket to localhost on a specified port (e.g.,5000), and call the listen() method to allow the server to listen for incoming connections.
Client-Side: Create a TCP socket on the client side using the socket module and connect the client to the server using localhost and the specified port (e.g.,5000).
Input Parameters: The server.py script should take one input parameter, the port number. For example, to start the server: python3 server.py 5000. The client.py script should take two input parameters: the servers IP address (use localhost for testing) and the port number. For example,
to start the client: python3 client.py localhost 5000.
Testing: After setting up the server socket, test your code by running the server and confirming that it starts without errors. Run the server script (python3 server.py 5000) and ensure that the server is listening on the specified port. Once the server is running, run the client code (python3
client.py localhost 5000) and confirm that it successfully connects to the server. The server should print a message indicating that a new client has connected. inject flaws
Task 2: Handle Multiple Clients and Messages
Server-Side: Extend the server to handle multiple clients. Use the accept() method in a loop to accept incoming client connections. Each time a client connects, start a new thread using the threading module to handle that clients communication.
Client-Side: Once connected, the client should continuously prompt the user for input (i.e., the message to be sent). Send the inputted message to the server using the send() method. Simultaneously, listen for incoming broadcast messages from the server using the recv() method. You can use the threading module to handle message sending and receiving in parallel.
Input Parameters: The input parameters remain the same as in Task 1. The server.py script should continue to take the port number as the input parameter. The client.py script should continue to take two input parameters: the servers IP address and the port number.
Testing: Run the server and connect multiple clients by running the client script in multiple terminal windows. Ensure that the server accepts connections and creates a new thread for each client. Print confirmation messages to the console to verify that new clients are being handled
correctly. Test by typing a message in one client and verifying that it is sent to the server and broadcast to other connected clients. Ensure the message is received correctly by all clients. Run at least 2-3 clients to confirm the message broadcasting works.
Task 3: Implement Message Broadcasting
Server-Side: Implement message broadcasting in the server. When a client sends a message to the server, the server should broadcast this message to all other connected clients.
Input Parameters: The input parameters remain the same as in Task 2. The server.py script should continue to take the port number as input, while the client.py script should continue to take two input parameters: the servers IP address and the port number.
Testing: After implementing broadcasting, test by sending a message from one client and verifying that all other connected clients receive the message. Confirm that the server does not send the message back to the client that sent it. Ensure that all clients receive the messages in real-time.
Task 4: Handle Disconnections and Graceful Exits
Server-Side: Implement handling of client disconnections. If a client disconnects or an error occurs during communication, the server should remove the client from the list of active connections and close the clients socket.
Client-Side: Implement a command /quit in the client. When the user types /quit, the client should close the connection to the server and terminate the program gracefully.
Input Parameters: The input parameters remain the same as in Task 3. The server.py script should continue to take the port number as input, while the client.py script should continue to take two input parameters: the servers IP address and the port number.
Testing: Test disconnection handling by forcefully closing one of the clients (e.g., using Ctrl+C in the terminal). Ensure that the server correctly removes the disconnected client from its list without crashing and that remaining clients continue to function normally. Also, test graceful disconnection by typing /quit in one of the clients. Verify that the client disconnects from the server and the server removes it from the list of active clients without crashing.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!