1 /****************************************************************
3 * Program daytime_tcp_client.c:
4 * Simple TCP client for daytime service (port 13)
6 * Author: Simone Piccardi
9 * Usage: daytime -h give all info's
11 * $Id: SimpleDaytimeTCPClient.c,v 1.1 2001/04/02 23:24:42 piccardi Exp $
13 ****************************************************************/
15 * Include needed headers
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 */
26 int main(int argc, char *argv[])
29 * Variables definition
33 struct sockaddr_in serv_add;
36 * Input section: decode parameters passed in the calling
39 opterr = 0; /* don't want writing to stderr */
40 while ( (i = getopt(argc, argv, "h")) != -1) {
46 printf("Wrong -h option use\n");
50 case '?': /* unrecognized options */
51 printf("Unrecognized options -%c\n",optopt);
53 default: /* should not reached */
57 /* ***********************************************************
59 * Options processing completed
63 * ***********************************************************/
65 if ( (sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
66 perror("Socket creation error");
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");
78 /* extablish connection */
79 if (connect(sock_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) {
80 perror("Connection error");
83 /* read daytime from server */
84 while ( (nread = read(sock_fd, buffer, MAXLINE)) > 0) {
86 if (fputs(buffer, stdout) == EOF) { /* write daytime */
87 perror("fputs error");
100 * routine to print usage info and exit
103 printf("Take daytime from a remote host \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");