Inserito il package listings ... e adesso ho la syntax highlight dei
[gapil.git] / sources / SimpleDaytimeTCPClient.c
1 /****************************************************************
2  *
3  * Program daytime_tcp_client.c: 
4  * Simple TCP client for daytime service (port 13)
5  *
6  * Author: Simone Piccardi
7  * Apr. 2001
8  *
9  * Usage: daytime -h give all info's
10  *
11  * $Id: SimpleDaytimeTCPClient.c,v 1.1 2001/04/02 23:24:42 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
23 #define MAXLINE 80
24 /* Program begin */
25 void usage(void);
26 int main(int argc, char *argv[])
27 {
28 /* 
29  * Variables definition  
30  */
31     int sock_fd;
32     int i, nread;
33     struct sockaddr_in serv_add;
34     char buffer[MAXLINE];
35     /*
36      * Input section: decode parameters passed in the calling 
37      * Use getopt function
38      */
39     opterr = 0;  /* don't want writing to stderr */
40     while ( (i = getopt(argc, argv, "h")) != -1) {
41         switch (i) {
42         /* 
43          * Handling options 
44          */ 
45         case 'h':  
46             printf("Wrong -h option use\n");
47             usage();
48             return(0);
49             break;
50         case '?':   /* unrecognized options */
51             printf("Unrecognized options -%c\n",optopt);
52             usage();
53         default:    /* should not reached */
54             usage();
55         }
56     }
57     /* ***********************************************************
58      * 
59      *           Options processing completed
60      *
61      *                Main code beginning
62      * 
63      * ***********************************************************/
64     /* create socket */
65     if ( (sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
66         perror("Socket creation error");
67         return -1;
68     }
69     /* initialize address */
70     memset((void *) &serv_add, 0, sizeof(serv_add)); /* clear server address */
71     serv_add.sin_family = AF_INET;                   /* address type is INET */
72     serv_add.sin_port = htons(13);                   /* daytime post is 13 */
73     /* build address using inet_pton */
74     if ( (inet_pton(AF_INET, argv[optind], &serv_add.sin_addr)) <= 0) {
75         perror("Address creation error");
76         return -1;
77     }
78     /* extablish connection */
79     if (connect(sock_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
80         perror("Connection error");
81         return -1;
82     }
83     /* read daytime from server */
84     while ( (nread = read(sock_fd, buffer, MAXLINE)) > 0) {
85         buffer[nread]=0;
86         if (fputs(buffer, stdout) == EOF) {          /* write daytime */
87             perror("fputs error");
88             return -1;
89         }
90     }
91     /* error on read */
92     if (nread < 0) {
93         perror("Read error");
94         return -1;
95     }
96     /* normal exit */
97     return 0;
98 }
99 /*
100  * routine to print usage info and exit
101  */
102 void usage(void) {
103     printf("Take daytime from a remote host \n");
104     printf("Usage:\n");
105     printf("  daytime [-h] [-v] [host in dotted decimal form] \n");
106     printf("  -v           set verbosity on\n");
107     printf("  -h           print this help\n");
108     exit(1);
109 }