/** Handle multiple socket connections with select and fd_set on Linux */ #include #include //strlen #include #include #include //close #include //close #include #include #include #include //FD_SET, FD_ISSET, FD_ZERO macros #define TRUE 1 #define FALSE 0 #define PORT 8888 #define MAX_CLIENTS 5 int main(int argc , char *argv[]) { int master_socket , addrlen , new_socket , activity, i , valread , sd; int max_sd; int client_socket[MAX_CLIENTS]; struct sockaddr_in address; struct timeval tv; char buffer[1025]; //data buffer of 1K /* Wait up to 2 seconds in select for activity to happen*/ tv.tv_sec=2; tv.tv_usec = 0; //set of socket descriptors fd_set readfds; //initialise all client_socket[] to 0 so not checked for (i = 0; i < MAX_CLIENTS; i++) { client_socket[i] = 0; } //create a master socket if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } //type of socket created address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons( PORT ); //bind the socket to localhost port 8888 if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0) { perror("bind failed"); exit(EXIT_FAILURE); } printf("Listener on port %d \n", PORT); //try to specify maximum of 3 pending connections for the master socket if (listen(master_socket, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } //accept the incoming connection addrlen = sizeof(address); printf("Waiting for connections ...\n"); while(TRUE) { //clear the socket set FD_ZERO(&readfds); //add master socket to set FD_SET(master_socket, &readfds); max_sd = master_socket; //add child sockets to set for ( i = 0 ; i < MAX_CLIENTS ; i++) { //socket descriptor sd = client_socket[i]; //if valid socket descriptor then add to read list if(sd > 0) FD_SET( sd , &readfds); //highest file descriptor number, need it for the select function if(sd > max_sd) max_sd = sd; } //wait for an activity on one of the sockets , timeout is 2 seconds, so continue in loop if nothing seen in 2 seconds. activity = select( max_sd + 1 , &readfds , NULL , NULL , &tv); if ((activity < 0) && (errno!=EINTR)) { printf("select error"); } //If something happened on the master socket , then its an incoming connection if (FD_ISSET(master_socket, &readfds)) { if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) { perror("accept"); exit(EXIT_FAILURE); } //inform user of socket number - used in send and receive commands printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port)); //add new socket to array of sockets for (i = 0; i < MAX_CLIENTS; i++) { //if position is empty if( client_socket[i] == 0 ) { client_socket[i] = new_socket; printf("Adding to list of sockets as %d\n" , i); break; } } } //else its some IO operation on some other socket :) for (i = 0; i < MAX_CLIENTS; i++) { sd = client_socket[i]; if (FD_ISSET( sd , &readfds)) { //Check if it was for closing , and also read the incoming message if ((valread = read( sd , buffer, 1024)) == 0) { //Somebody disconnected , get client details and print getpeername(sd , (struct sockaddr*)&address , (socklen_t*)&addrlen); printf("Host disconnected , ip %s , port %d \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port)); //Close the socket and mark as 0 in list for reuse close( sd ); client_socket[i] = 0; } //Echo back the message that came in else { //set the string terminating NULL byte on the end of the data read buffer[valread] = '\0'; printf("%s\n", buffer); } } } } return 0; }