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