Completato server elementare.
[gapil.git] / sources / SimpleDaytimeTCPServer.c
1 /****************************************************************
2  *
3  * Program daytime_tcp_server.c: 
4  * Simple TCP server for daytime service (port 13)
5  *
6  * Author: Simone Piccardi
7  * Apr. 2001
8  *
9  * Usage: daytimed
10  *
11  * $Id: SimpleDaytimeTCPServer.c,v 1.1 2001/04/04 23:09:57 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;
36     char buffer[MAXLINE];
37     time_t timeval;
38     /*
39      * Input section: decode parameters passed in the calling 
40      * Use getopt function
41      */
42     opterr = 0;  /* don't want writing to stderr */
43     while ( (i = getopt(argc, argv, "h")) != -1) {
44         switch (i) {
45         /* 
46          * Handling options 
47          */ 
48         case 'h':  
49             printf("Wrong -h option use\n");
50             usage();
51             return(0);
52             break;
53         case '?':   /* unrecognized options */
54             printf("Unrecognized options -%c\n",optopt);
55             usage();
56         default:    /* should not reached */
57             usage();
58         }
59     }
60     /* ***********************************************************
61      * 
62      *           Options processing completed
63      *
64      *                Main code beginning
65      * 
66      * ***********************************************************/
67     /* create socket */
68     if ( (list_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
69         perror("Socket creation error");
70         exit(-1);
71     }
72     /* initialize address */
73     memset((void *)&serv_add, 0, sizeof(serv_add)); /* clear server address */
74     serv_add.sin_family = AF_INET;                  /* address type is INET */
75     serv_add.sin_port = htons(13);                  /* daytime port is 13 */
76     serv_add.sin_addr.s_addr = htonl(INADDR_ANY);   /* connect from anywhere */
77     /* bind socket */
78     if (bind(list_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
79         perror("bind error");
80         exit(-1);
81     }
82     /* listen on socket */
83     if (listen(list_fd, BACKLOG) < 0 ) {
84         perror("listen error");
85         exit(-1);
86     }
87     /* write daytime to client */
88     while (1) {
89         if ( (conn_fd = accept(list_fd, (struct sockaddr *) NULL, NULL)) <0 ) {
90             perror("accept error");
91             exit(-1);
92         }
93         timeval = time(NULL);
94         snprintf(buffer, sizeof(buffer), "%.24s\r\n", ctime(&timeval));
95         if ( (write(conn_fd, buffer, strlen(buffer))) < 0 ) {
96             perror("write error");
97             exit(-1);
98         }
99         close(conn_fd);
100     }
101
102     /* normal exit */
103     exit(0);
104 }
105 /*
106  * routine to print usage info and exit
107  */
108 void usage(void) {
109     printf("Simple daytime server\n");
110     printf("Usage:\n");
111     printf("  daytimed [-h] \n");
112     printf("  -h           print this help\n");
113     exit(1);
114 }