#include #include #include #include #include #include #include int skt; // Global. This is the socket that the server will listen on void close_server(int signal_number){ printf("Closing down the server\n"); close(skt); } int main(int argc, char ** argv){ struct sockaddr_in sad; sad.sin_port = htons(5141); sad.sin_addr.s_addr = INADDR_ANY; sad.sin_family = AF_INET; skt = socket(AF_INET, SOCK_STREAM, 0); if(skt == -1){ perror("socket"); return 1; } struct sigaction close_action; close_action.sa_handler = close_server; if(sigaction(SIGINT, &close_action, 0)){ perror("sigaction"); return 1; } if( bind(skt, (struct sockaddr *)(&sad), sizeof(struct sockaddr_in)) ){ perror("bind"); return 1; } if( listen(skt, 5) ){ perror("listen"); return 1; } int client_fd; struct sockaddr_in client_address; socklen_t address_size = sizeof(struct sockaddr_in); for(;;){ client_fd = accept(skt, (struct sockaddr *)(&client_address), &address_size); if(client_fd == -1){ perror("accept"); break; } write(client_fd, "Good Morning", 13); printf("Connection made from address %s\n", inet_ntoa(client_address.sin_addr)); close(client_fd); } return 0; }