X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;f=listati%2FUDP_ClientEcho.c;fp=listati%2FUDP_ClientEcho.c;h=746d5a772a3e3de7ab73213569556ec285b0be91;hb=a0184b68ca9dced41be95342ffd8a8ee04d2b861;hp=0000000000000000000000000000000000000000;hpb=9eb4436b75582594619576d54f28073ba027111a;p=gapil.git diff --git a/listati/UDP_ClientEcho.c b/listati/UDP_ClientEcho.c new file mode 100644 index 0000000..746d5a7 --- /dev/null +++ b/listati/UDP_ClientEcho.c @@ -0,0 +1,31 @@ +void ClientEcho(FILE * filein, int socket, struct sockaddr_in * serv_addr) +{ + char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1]; + int nread, nwrite; + /* initialize file descriptor set */ + while (1) { + if (fgets(sendbuff, MAXLINE, filein) == NULL) { + return; /* if no input just return */ + } else { /* else we have to write to socket */ + nwrite = sendto(socket, sendbuff, strlen(sendbuff), 0, + (struct sockaddr *) serv_addr, sizeof(*serv_addr)); + if (nwrite < 0) { /* on error stop */ + printf("Errore in scrittura: %s", strerror(errno)); + return; + } + } + nread = recvfrom(socket, recvbuff, strlen(sendbuff), 0, NULL, NULL); + if (nread < 0) { /* error condition, stop client */ + printf("Errore in lettura: %s\n", strerror(errno)); + return; + } + if (nread == 0) { /* server closed connection, stop */ + return; + } + recvbuff[nread] = 0; /* else read is ok, write on stdout */ + if (fputs(recvbuff, stdout) == EOF) { + perror("Errore in scrittura su terminale"); + return; + } + } +}