Completato echo su UDP, e messi cenni sulla connect per UDP, da riprendere
[gapil.git] / listati / UDP_ClientEcho.c
1 void ClientEcho(FILE * filein, int socket, struct sockaddr_in * serv_addr) 
2 {
3     char sendbuff[MAXLINE+1], recvbuff[MAXLINE+1];
4     int nread, nwrite; 
5     /* initialize file descriptor set */
6     while (1) {
7         if (fgets(sendbuff, MAXLINE, filein) == NULL) {
8             return;                /* if no input just return */
9         } else {                   /* else we have to write to socket */
10             nwrite = sendto(socket, sendbuff, strlen(sendbuff), 0,
11                             (struct sockaddr *) serv_addr, sizeof(*serv_addr));
12             if (nwrite < 0) {      /* on error stop */
13                 printf("Errore in scrittura: %s", strerror(errno));
14                 return;
15             }
16         }
17         nread = recvfrom(socket, recvbuff, strlen(sendbuff), 0, NULL, NULL); 
18         if (nread < 0) {  /* error condition, stop client */
19             printf("Errore in lettura: %s\n", strerror(errno));
20             return;
21         }
22         recvbuff[nread] = 0;   /* else read is ok, write on stdout */
23         if (fputs(recvbuff, stdout) == EOF) {
24             perror("Errore in scrittura su terminale");
25             return;
26         }
27     }
28 }