X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FTCP_echo.c;h=57d61840120e90adc83a046a274b62e4a3f1a9a4;hp=275d570c1739d423f9a79fdcae0826f200add939;hb=af73295172df06a6a91edc4c206b6e5633c566f6;hpb=49ae2790472cc2ad1e54ea26d037306598f9c83b diff --git a/sources/TCP_echo.c b/sources/TCP_echo.c index 275d570..57d6184 100644 --- a/sources/TCP_echo.c +++ b/sources/TCP_echo.c @@ -1,4 +1,4 @@ -/* TCP_echo1.c +/* TCP_echo.c * * Copyright (C) 2001-2003 Simone Piccardi * @@ -18,7 +18,7 @@ */ /**************************************************************** * - * Program ElemEchoTCPClient.c + * Program TCP_echo.c * Simple TCP client for echo service (port 7) * * Author: Simone Piccardi @@ -26,7 +26,7 @@ * * Usage: echo -h give all info's * - * $Id: TCP_echo.c,v 1.5 2003/07/27 23:41:04 piccardi Exp $ + * $Id: TCP_echo.c,v 1.10 2003/10/19 10:38:27 piccardi Exp $ * ****************************************************************/ /* @@ -40,6 +40,8 @@ #include /* include error codes */ #include /* include erroro strings definitions */ +#include "macros.h" + #define MAXLINE 256 void usage(void); void ClientEcho(FILE * filein, int socket); @@ -115,7 +117,6 @@ int main(int argc, char *argv[]) perror("Cannot set linger"); exit(1); } - return 0; } /* do read/write operations */ ClientEcho(stdin, sock); @@ -139,19 +140,41 @@ void ClientEcho(FILE * filein, int socket) { char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1]; int nread, nwrite; - while (fgets(sendbuff, MAXLINE, filein) != NULL) { - nwrite = FullWrite(socket, sendbuff, strlen(sendbuff)); - if (nwrite < 0) { - printf("Errore in scrittura %s", strerror(errno)); - } - nread = FullRead(socket, recvbuff, strlen(sendbuff)); - if (nread < 0) { - printf("Errore in lettura %s", strerror(errno)); + int maxfd; + fd_set fset; + /* initialize file descriptor set */ + FD_ZERO(&fset); + maxfd = max(fileno(filein), socket) + 1; + while (1) { + FD_SET(socket, &fset); /* set for the socket */ + FD_SET(fileno(filein), &fset); /* set for the standard input */ + select(maxfd, &fset, NULL, NULL, NULL); /* wait for read ready */ + if (FD_ISSET(fileno(filein), &fset)) { /* if ready on stdin */ + if (fgets(sendbuff, MAXLINE, filein) == NULL) { /* if no input */ + return; /* we stopped client */ + } else { /* else we have to write to socket */ + nwrite = FullWrite(socket, sendbuff, strlen(sendbuff)); + if (nwrite < 0) { /* on error stop */ + printf("Errore in scrittura: %s", strerror(errno)); + return; + } + } } - recvbuff[nread] = 0; - if (fputs(recvbuff, stdout) == EOF) { - perror("Errore in scrittura su terminale"); + if (FD_ISSET(socket, &fset)) { /* if ready on socket */ + nread = read(socket, recvbuff, strlen(sendbuff)); /* do read */ + if (nread < 0) { /* error condition, stop client */ + printf("Errore in lettura: %s\n", strerror(errno)); + return; + } + if (nread == 0) { /* server closed connection, stop */ + printf("EOF sul socket\n"); + return; + } + recvbuff[nread] = 0; /* else read is ok, write on stdout */ + if (fputs(recvbuff, stdout) == EOF) { + perror("Errore in scrittura su terminale"); + return; + } } } - return; }