Question: Code the following in python: Task 1 : Set up the Server and Client Sockets Server - Side: Set up the server socket by creating
Code the following in python:
Task : Set up the Server and Client Sockets
ServerSide: Set up the server socket by creating a TCP socket using the socket module. Use AFINET for the address family and SOCKSTREAM for the socket type. Bind the socket to localhost on a specified port egand call the listenmethod to allow the server to listen for incoming connections.
ClientSide: 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 eg
Input Parameters: The server.py script should take one input parameter, the port number. For example, to start the server: pythonserverpy The client.py script should take two input parameters: the servers IP address use localhost for testingand the port number. For example,
to start the client: pythonclientpy localhost
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 pythonserverpy and ensure that the server is listening on the specified port. Once the server is running, run the client code python
client.py localhost 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 : Handle Multiple Clients and Messages
ServerSide: Extend the server to handle multiple clients. Use the acceptmethod 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.
ClientSide: Once connected, the client should continuously prompt the user for input iethe message to be sentSend the inputted message to the server using the sendmethod Simultaneously, listen for incoming broadcast messages from the server using the recvmethod 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 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 clients to confirm the message broadcasting works.
Task : Implement Message Broadcasting
ServerSide: 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 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 itEnsure that all clients receive the messages in realtime.
Task : Handle Disconnections and Graceful Exits
ServerSide: 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.
ClientSide: Implement a command quit in the client. When the user types quitthe client should close the connection to the server and terminate the program gracefully.
Input Parameters: The input parameters remain the same as in Task 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 egusing CtrlC in the terminalEnsure 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
