Materiale su splice, ed esempio non funzionante
[gapil.git] / sources / UDP_daytimed.c
1 /* UDP_daytimed.c
2  * 
3  * Copyright (C) 2004 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program daytimed: 
22  * Elementary TCP server for daytime service (port 13)
23  *
24  * Author: Simone Piccardi
25  * Mar. 2004
26  *
27  * Usage: daytimed -h give all info
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #include <sys/types.h>   /* primitive system data types */
34 #include <unistd.h>      /* unix standard library */
35 #include <arpa/inet.h>   /* IP addresses conversion utilities */
36 #include <sys/socket.h>  /* socket constants, types and functions */
37 #include <stdio.h>       /* standard I/O library */
38 #include <time.h>        /* date and time constants, types and functions */
39
40 #define MAXLINE 80
41 /* Program begin */
42 void usage(void);
43 int main(int argc, char *argv[])
44 {
45 /* 
46  * Variables definition  
47  */
48     int sock;
49     int i, n, len, verbose=0;
50     struct sockaddr_in addr;
51     char buffer[MAXLINE];
52     time_t timeval;
53     /*
54      * Input section: decode parameters passed in the calling 
55      * Use getopt function
56      */
57     opterr = 0;  /* don't want writing to stderr */
58     while ( (i = getopt(argc, argv, "hv")) != -1) {
59         switch (i) {
60         /* 
61          * Handling options 
62          */ 
63         case 'h':  
64             printf("Wrong -h option use\n");
65             usage();
66             return(0);
67             break;
68         case 'v':
69             verbose = 1;
70             break;
71         case '?':   /* unrecognized options */
72             printf("Unrecognized options -%c\n",optopt);
73             usage();
74         default:    /* should not reached */
75             usage();
76         }
77     }
78     /* ***********************************************************
79      * 
80      *           Options processing completed
81      *
82      *                Main code beginning
83      * 
84      * ***********************************************************/
85     /* create socket */
86     if ( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
87         perror("Socket creation error");
88         exit(-1);
89     }
90     /* initialize address */
91     memset((void *)&addr, 0, sizeof(addr));     /* clear server address */
92     addr.sin_family = AF_INET;                  /* address type is INET */
93     addr.sin_port = htons(13);                  /* daytime port is 13 */
94     addr.sin_addr.s_addr = htonl(INADDR_ANY);   /* connect from anywhere */
95     /* bind socket */
96     if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
97         perror("bind error");
98         exit(-1);
99     }
100     /* write daytime to client */
101     while (1) {
102         n = recvfrom(sock, buffer, MAXLINE, 0, (struct sockaddr *)&addr, &len);
103         if (n < 0) {
104             perror("recvfrom error");
105             exit(-1);
106         }
107         if (verbose) {
108             inet_ntop(AF_INET, &addr.sin_addr, buffer, sizeof(buffer));
109             printf("Request from host %s, port %d\n", buffer,
110                    ntohs(addr.sin_port));
111         }
112         timeval = time(NULL);
113         snprintf(buffer, sizeof(buffer), "%.24s\r\n", ctime(&timeval));
114         n = sendto(sock, buffer, strlen(buffer), 0, 
115                    (struct sockaddr *)&addr, sizeof(addr));
116         if (n < 0) {
117             perror("sendto error");
118             exit(-1);
119         }
120     }
121     /* normal exit */
122     exit(0);
123 }
124 /*
125  * routine to print usage info and exit
126  */
127 void usage(void) {
128     printf("Simple daytime server\n");
129     printf("Usage:\n");
130     printf("  daytimed [-hv] \n");
131     printf("  -h           print this help\n");
132     printf("  -v           print request source on stdout\n");
133     exit(1);
134 }