#include #include #include #include #include #include #include #include #include #include "getoutput_demo/getoutput.h" int main(int argc, char ** argv){ struct sockaddr_in sad; sad.sin_port = htons(5130); sad.sin_addr.s_addr = INADDR_ANY; sad.sin_family = AF_INET; int skt = socket(AF_INET, SOCK_STREAM, 0); // Step 1 if(skt == -1){ perror("socket"); return 1; } if( bind(skt, (struct sockaddr*)(&sad), sizeof(struct sockaddr_in)) ){ // step 2 perror("bind"); return 1; } if( listen(skt, 5) ){ // step 3 perror("listen"); return 1; } while(1){ int client_fd; struct sockaddr_in client_address; socklen_t address_size = sizeof(struct sockaddr_in); client_fd = accept(skt, (struct sockaddr *)(&client_address), &address_size); // step 4 char *usernames = getoutput("who | cut -d ' ' -f 1 | sort -u"); dprintf(1, "Client connected from IP %s\n", inet_ntoa(client_address.sin_addr)); write(client_fd, usernames, strlen(usernames)); free(usernames); close(client_fd); } return 0; }