Rinominate le prime versioni dei vari programmi in Elem, la versione piu`
[gapil.git] / sources / ElemDaytimeTCPCuncServ.c
1 /****************************************************************
2  *
3  * Program ElemDaytimeTCPCuncServ.c 
4  * Elementary TCP cuncurrent server for daytime service (port 13)
5  *
6  * Author: Simone Piccardi
7  * May. 2001
8  *
9  * Usage: daytimed
10  *
11  * $Id: ElemDaytimeTCPCuncServ.c,v 1.1 2001/05/19 20:47:03 piccardi Exp $ 
12  *
13  ****************************************************************/
14 /* 
15  * Include needed headers
16  */
17 #include <sys/types.h>   /* predefined types */
18 #include <unistd.h>      /* include unix standard library */
19 #include <arpa/inet.h>   /* IP addresses conversion utiliites */
20 #include <sys/socket.h>  /* socket library */
21 #include <stdio.h>       /* include standard I/O library */
22 #include <time.h>
23
24 #define MAXLINE 80
25 #define BACKLOG 10
26 /* Program begin */
27 void usage(void);
28 int main(int argc, char *argv[])
29 {
30 /* 
31  * Variables definition  
32  */
33     int list_fd, conn_fd;
34     int i;
35     struct sockaddr_in serv_add, client;
36     char buffer[MAXLINE];
37     socklen_t len;
38     time_t timeval;
39     pid_t pid;
40     int logging=0;
41     /*
42      * Input section: decode parameters passed in the calling 
43      * Use getopt function
44      */
45     opterr = 0;  /* don't want writing to stderr */
46     while ( (i = getopt(argc, argv, "hv")) != -1) {
47         switch (i) {
48         /* 
49          * Handling options 
50          */ 
51         case 'h':  
52             printf("Wrong -h option use\n");
53             usage();
54             return(0);
55             break;
56         case '?':   /* unrecognized options */
57             printf("Unrecognized options -%c\n",optopt);
58             usage();
59             return(0);
60             break;
61         case 'v':
62             logging = 1;
63             break;
64         default:    /* should not reached */
65             usage();
66             return(0);
67         }
68     }
69     /* ***********************************************************
70      * 
71      *           Options processing completed
72      *
73      *                Main code beginning
74      * 
75      * ***********************************************************/
76     /* create socket */
77     if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
78         perror("Socket creation error");
79         exit(-1);
80     }
81     /* initialize address */
82     memset((void *)&serv_add, 0, sizeof(serv_add)); /* clear server address */
83     serv_add.sin_family = AF_INET;                  /* address type is INET */
84     serv_add.sin_port = htons(13);                  /* daytime port is 13 */
85     serv_add.sin_addr.s_addr = htonl(INADDR_ANY);   /* connect from anywhere */
86     /* bind socket */
87     if (bind(list_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
88         perror("bind error");
89         exit(-1);
90     }
91     /* listen on socket */
92     if (listen(list_fd, BACKLOG) < 0 ) {
93         perror("listen error");
94         exit(-1);
95     }
96     /* write daytime to client */
97     while (1) {
98         if ( (conn_fd = accept(list_fd, (struct sockaddr *)&client, &len)) 
99              <0 ) {
100             perror("accept error");
101             exit(-1);
102         }
103         /* fork to handle connection */
104         if ( (pid = fork()) < 0 ){
105             perror("fork error");
106             exit(-1);
107         }
108         if (pid == 0) {                 /* child */
109             close(list_fd);
110             timeval = time(NULL);
111             snprintf(buffer, sizeof(buffer), "%.24s\r\n", ctime(&timeval));
112             if ( (write(conn_fd, buffer, strlen(buffer))) < 0 ) {
113                 perror("write error");
114                 exit(-1);
115             }
116             if (logging) {
117                 inet_ntop(AF_INET, &client.sin_addr, buffer, sizeof(buffer));
118                 printf("Request from host %s, port %d\n", buffer,
119                        ntohs(client.sin_port));
120             }
121             close(conn_fd);
122             exit(0);
123         } else {                        /* parent */
124             close(conn_fd);
125         }
126     }
127     /* normal exit, never reached */
128     exit(0);
129 }
130 /*
131  * routine to print usage info and exit
132  */
133 void usage(void) {
134     printf("Simple daytime server\n");
135     printf("Usage:\n");
136     printf("  daytimed [-hv] \n");
137     printf("  -h           print this help\n");
138     printf("  -v           print request source on stdout\n");
139     exit(1);
140 }
141