/* Client - Server database system * 8th Semester , Computational Systems Laboratory * Ioannou Spiros * Kontopodis Dimitris * May '97 */ #include #include #include #include #include #include #include #include #include #include #include #include void sigphdl (); main (int argc, char *argv[]) { int siz, j, sd, insd,r, i, b, reqok; char buf[80]; char buf1[80]; char buf2[80]; char me[80]; char *b1; char *req, **tp, *arg1, *arg2; long offset; int value, cs; int i1, i2; char c; struct sockaddr_in sa, client_new, la; int len; char host[63]; struct hostent *hostp, *myhost; struct timeval tval; fd_set input_fds; if (argc != 3) { printf ("Usage: client [host] [port]\n"); exit (-1); } /*Client operation socket */ if ((sd = socket (AF_INET, SOCK_STREAM, 0)) < 0) { perror ("socket"); exit (1); } /* Do Client Initialization stuff */ hostp = gethostbyname (argv[1]); if (!hostp) { perror ("gethostbyname"); exit (-2); } else printf ("Official hostname:%s\n", hostp->h_name); bcopy ((char *) hostp->h_addr, (char *) &sa.sin_addr, hostp->h_length); sa.sin_family = AF_INET; sa.sin_port = htons ((u_short) atoi (argv[2])); /* sa.sin_addr.s_addr=htonl((u_long)hostp->h_addr); */ if (connect (sd, (struct sockaddr *) &sa, sizeof (sa)) < 0) perror ("connect"); fflush (stdout); /*timeout not used, just for testing*/ tval.tv_sec = 0; tval.tv_usec = 10; /*fcntl(sd,F_SETFL,O_NDELAY);*/ for (;;) { reqok = 0; FD_ZERO (&input_fds); /* Initialization of flags*/ FD_SET (0, &input_fds); /* Input from stdin=keyboard*/ FD_SET (sd, &input_fds); /* Input from Coordinator */ /* Scan for input without timeout -> interupt driven*/ if ((i=select(64,&input_fds,(fd_set *)0,(fd_set *)0, (struct timeval *)0)) < 0) { perror ("global select"); exit (5); } /* Not really used without the -DNONET option of server compiling*/ if (FD_ISSET (sd, &input_fds)) { b = read (sd, buf, 80); if (b <= 0) { printf ("Connection closed by foreign host\n"); exit (b); } if (b < 2) { if (isprint (buf[0])) printf ("\nByte requested is \"%d\"(%c)", buf[0], buf[0]); else printf ("\nByte requested is \"%d\"", buf[0]); } else { write (1, buf, b); } printf ("\nClient Ready >"); } /*Keyboard input!*/ if (FD_ISSET (0, &input_fds)) { r =read (0, buf, 80); b = write (sd, buf, r); } } } void sigphdl () { printf ("\nBROKEN PIPE\n"); signal (SIGPIPE, sigphdl); } /* * Counts arguments. Used for * parsing user input */ countspace (char *str) { int c = 0, i = 0, p; while (str[i] && str[i] == ' ') i++; if (str[i]) do { if (str[i] == ' ' && str[i + 1] != ' ') c++; i++; } while (str[i + 1]); return c; } /* The end */