Task: Multiplexed Client Server
Navigate to labs/lab-11/tasks/multiplexed-client-server and run make to generate the support files.
This task builds on the previous implementation of a client-server ordered communication. Previously, the client and server followed a strict, sequential communication pattern: each peer had to send a message and wait for a reply before proceeding.
We plan to build a group chat where clients can send messages at any time, and each message is broadcast to all other connected clients. To accomplish this, we’ll implement I/O multiplexing mechanisms that notify us only when data is available on a file descriptor. This non-blocking approach allows for smooth, unhindered communication between clients.
-
We’ll use the
epollinterface to manage multiple file descriptors. Begin by openingw_epoll.hand completing the TODOs. We will define wrapper functions to add and remove file descriptors from theepollinstance, making the main code more readable. Note: Ensure that each wrapper returns the result of theepoll_ctl()for error handling. -
Complete the TODOs in
support/client.cto enable multiplexing of the available file descriptors. The file descriptors arestdin(for receiving user messages) andsockfd(for communication with the server). Useepoll_create(),epoll_wait(), and the wrappers defined inw_epoll.hto handle these descriptors without blocking. Remember to close the sockets before exiting.To test, start
python server.pyin one terminal and run your client implementation in two separate terminals. If successful, the clients should be able to communicate through the server. -
Complete the TODOs in
support/server.cto multiplex I/O with clients. You will need to create anepollinstance and dynamically add and remove clients as they connect and disconnect. Useepoll_create(),epoll_wait(), and the wrappers defined inw_epoll.hto achieve this functionality. Remember to remove the client sockets from theepollinstance before closing them.To test your implementation, run
./serverin one terminal and./client(orpython client.py) in two separate terminals. If everything works correctly, the clients should be able to communicate with each other via the server.
If you’re having difficulties solving this exercise, go through the epoll API from I/O Multiplexing.