Código
#include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <time.h> int socketfd, newsocket; char *filename; void shutup (int signal) { times (); close (newsocket); close (socketfd); } int times () { struct tm *ahora; char buffer [40]; return 0; } int main (int argc, char *argv []) { struct tm *ahora; char hora [40]; if (getuid()!=0) { return 1; } if (argc<3) { printf ("Use: %s <PORT> <LOG> [MODE]\nMode:\t\t0) Fork the proccess to background [DEFAULT].\n\t\t1) Run in the terminal.\n\n", argv [0]); return 1; } int cont; struct sockaddr_in client, host; char buffer [1024]; int size=sizeof (client); filename = argv [2]; socketfd=socket (2, 1 , 0); host.sin_family=AF_INET; host.sin_addr.s_addr=0; bind (socketfd, (struct sockaddr*)&host, sizeof (struct sockaddr)); listen (socketfd, 3); times (); signal (SIGTERM, shutup); signal (SIGINT, shutup); daemon (1, 0); while (1) { newsocket=accept (socketfd, (struct sockaddr*)&client, &size); times (); cont=recv (newsocket, &buffer, 1024, 0); while (cont>2) { times (); buffer [cont-1]='\0'; cont=recv (newsocket, &buffer, 1024, 0); } times (); printf ("Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port)); close (newsocket); } close (socketfd); return 0; }
Si alguien cree que algo se puede mejorar, que postee xD