Finito server daytime su UDP
[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  * $Id: UDP_daytimed.c,v 1.2 2004/03/21 23:01:01 piccardi Exp $
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
35 #include <sys/types.h>   /* predefined types */
36 #include <unistd.h>      /* include unix standard library */
37 #include <arpa/inet.h>   /* IP addresses conversion utiliites */
38 #include <sys/socket.h>  /* socket library */
39 #include <stdio.h>       /* include standard I/O library */
40 #include <time.h>
41
42 #define MAXLINE 80
43 /* Program begin */
44 void usage(void);
45 int main(int argc, char *argv[])
46 {
47 /* 
48  * Variables definition  
49  */
50     int sock;
51     int i, n, len, verbose=0;
52     struct sockaddr_in addr;
53     char buffer[MAXLINE];
54     time_t timeval;
55     /*
56      * Input section: decode parameters passed in the calling 
57      * Use getopt function
58      */
59     opterr = 0;  /* don't want writing to stderr */
60     while ( (i = getopt(argc, argv, "hv")) != -1) {
61         switch (i) {
62         /* 
63          * Handling options 
64          */ 
65         case 'h':  
66             printf("Wrong -h option use\n");
67             usage();
68             return(0);
69             break;
70         case 'v':
71             verbose = 1;
72             break;
73         case '?':   /* unrecognized options */
74             printf("Unrecognized options -%c\n",optopt);
75             usage();
76         default:    /* should not reached */
77             usage();
78         }
79     }
80     /* ***********************************************************
81      * 
82      *           Options processing completed
83      *
84      *                Main code beginning
85      * 
86      * ***********************************************************/
87     /* create socket */
88     if ( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
89         perror("Socket creation error");
90         exit(-1);
91     }
92     /* initialize address */
93     memset((void *)&addr, 0, sizeof(addr));     /* clear server address */
94     addr.sin_family = AF_INET;                  /* address type is INET */
95     addr.sin_port = htons(13);                  /* daytime port is 13 */
96     addr.sin_addr.s_addr = htonl(INADDR_ANY);   /* connect from anywhere */
97     /* bind socket */
98     if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
99         perror("bind error");
100         exit(-1);
101     }
102     /* write daytime to client */
103     while (1) {
104         n = recvfrom(sock, buffer, MAXLINE, 0, (struct sockaddr *)&addr, &len);
105         if (n < 0) {
106             perror("recvfrom error");
107             exit(-1);
108         }
109         if (verbose) {
110             inet_ntop(AF_INET, &addr.sin_addr, buffer, sizeof(buffer));
111             printf("Request from host %s, port %d\n", buffer,
112                    ntohs(addr.sin_port));
113         }
114         timeval = time(NULL);
115         snprintf(buffer, sizeof(buffer), "%.24s\r\n", ctime(&timeval));
116         n = sendto(sock, buffer, strlen(buffer), 0, 
117                    (struct sockaddr *)&addr, sizeof(addr));
118         if (n < 0) {
119             perror("sendto error");
120             exit(-1);
121         }
122     }
123     /* normal exit */
124     exit(0);
125 }
126 /*
127  * routine to print usage info and exit
128  */
129 void usage(void) {
130     printf("Simple daytime server\n");
131     printf("Usage:\n");
132     printf("  daytimed [-hv] \n");
133     printf("  -h           print this help\n");
134     printf("  -v           print request source on stdout\n");
135     exit(1);
136 }