This tutorial assumes you have a basic knowledge of socket programming, i.e you are familiar with basic server and client model. In the basic model, server handles only one client at a time, which is a big assumption if you want to develop any scalable server model. The simple way to handle multiple clients would be to spawn new thread for every new client connected to the server. This method is strongly not recommended because of various disadvantages, namely:
Select()
A better way to handle multiple clients is by using
select()
Data structure used for select:
fd_set It contains the list of file descriptors to monitor for some activity. There are four functions associated with fd_set:
fd_set readfds;Activating select:
Please read the man page for select to check all the arguments for select command.
activity = select( max_fd + 1 , &readfds , NULL , NULL , NULL);
Implementation:
for io multiplexing (select) <span error handling <span using select for listen to multiple client serverfd then it means its C
Compile the file and run the server. Use telnet to connect the server as a client. Try running on different machines using following command:
telnet localhost 8888
Code Explanation: