poll()Implement a basic multi-client server that listens for incoming
client connections and handles multiple clients using the poll()
system call. This task requires building a server from scratch that
can manage multiple client connections, read data from clients, and
broadcast messages to all connected clients.
Server Setup:
The server should listen on a specified port and accept incoming client connections.
It should handle multiple clients
simultaneously without blocking by using the poll()
system call.
Using poll()
for I/O Multiplexing:
The server should maintain an
array of pollfd structures to track
multiple client connections and the server socket.
Use the poll()
system call to wait for events on the server socket (new
connections) and on each client socket (data to read).
When a new connection is made,
the server should accept the connection and add it to the pollfd
array.
Broadcasting Messages:
When a client sends data, the server should read the data and broadcast it to all connected clients.
Handle client disconnects by
closing their sockets and updating the pollfd
array.
Error Handling:
The server should handle errors such as failed socket creation, connection issues, and disconnections.
Code Outline:
make an array of poll structs
socket = MakeServerSocket
stick socket into the array of poll structs[0]
loop
poll
if [0] is triggered
this_client = accept(…)
stick this_client into the poll struct array
else
for every other element in the array
if triggered
read
if error remove
else broadcast