Advertisement
StarHonor

Client1u.c

Mar 25th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.18 KB | None | 0 0
  1. /*
  2.  *  File Client1u.C
  3.  *      ECHO UDP CLIENT with the following feqatures:
  4.  *      - Gets server IP address and port from keyboard
  5.  *      - LINE/ORIENTED:
  6.  *        > continuously reads lines from keyboard
  7.  *        > sends each line to the server
  8.  *        > waits for response (at most for a fixed amount of time) and diaplays it
  9.  *      - Terminates when the "close" or "stop" line is entered
  10.  */
  11.  
  12.  
  13. #include     <stdlib.h>
  14. #include     <string.h>
  15. #include     <inttypes.h>
  16. #include     "errlib.h"
  17. #include     "sockwrap.h"
  18.  
  19. #define BUFLEN 128  /* BUFFER LENGTH */
  20. #define TIMEOUT 15  /* TIMEOUT (seconds) */
  21.  
  22. /* FUNCTION PROTOTYPES */
  23. int mygetline(char *line, size_t maxline, char *prompt);
  24. void showAddr(char *str, struct sockaddr_in *a);
  25. int iscloseorstop(char *buf);
  26.  
  27. /* GLOBAL VARIABLES */
  28. char *prog_name;
  29.  
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.     char            buf[BUFLEN];       /* transmission buffer */
  34.     char        rbuf[BUFLEN];      /* reception buffer */
  35.  
  36.     uint32_t        taddr_n;  /* server IP addr. (net/host ord) */
  37.     uint16_t        tport_n, tport_h;  /* server port number */
  38.  
  39.     int     s;
  40.     struct sockaddr_in  saddr;
  41.     fd_set      cset;
  42.     struct timeval  tval;
  43.  
  44.     prog_name = argv[0];
  45.  
  46.  
  47.     /* input IP address and port of server */
  48.     mygetline(buf, BUFLEN, "Enter host IPv4 address (dotted notation):");
  49.     taddr_n = inet_addr(buf);
  50.     if (taddr_n == INADDR_NONE)
  51.     err_sys("Invalid address");
  52.  
  53.     mygetline(buf, BUFLEN, "Enter port : ");
  54.     if (sscanf(buf, "%" SCNu16, &tport_h)!=1)
  55.     err_sys("Invalid port number");
  56.     tport_n = htons(tport_h);
  57.  
  58.     /* create the socket */
  59.     printf("Creating socket\n");
  60.     s = Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  61.     printf("done. Socket number: %d\n",s);
  62.  
  63.     /* prepare server address structure */
  64.     bzero(&saddr, sizeof(saddr));
  65.     saddr.sin_family      = AF_INET;
  66.     saddr.sin_port        = tport_n;
  67.     saddr.sin_addr.s_addr = taddr_n;
  68.  
  69.     /* main client loop */
  70.     printf("Use message 'close' or 'stop' to terminate program.\n");
  71.    
  72.     for (buf[0]='\0' ; !iscloseorstop(buf); ) //main loop where we get a line of text from the keyboard
  73.     {
  74.         size_t      len, n;
  75.         struct sockaddr_in  from;
  76.         socklen_t       fromlen;
  77.  
  78.         mygetline(buf, BUFLEN, "Enter line (max 127 char): ");
  79.         len = strlen(buf);
  80.         n=sendto(s, buf, len, 0, (struct sockaddr *) &saddr, sizeof(saddr));
  81.         if (n != len)
  82.         {
  83.         printf("Write error\n");
  84.         continue;
  85.         }
  86.  
  87.         printf("waiting for response...\n");
  88.         FD_ZERO(&cset);
  89.         FD_SET(s, &cset);
  90.         tval.tv_sec = TIMEOUT;
  91.         tval.tv_usec = 0;
  92.         n = Select(FD_SETSIZE, &cset, NULL, NULL, &tval);
  93.         if (n>0)
  94.             {
  95.         /* receive datagram */
  96.         fromlen = sizeof(struct sockaddr_in);
  97.             n=recvfrom(s,rbuf,BUFLEN-1,0,(struct sockaddr *)&from,&fromlen);
  98.                 if (n != -1)
  99.             {
  100.             rbuf[n] = '\0';
  101.             showAddr("Received response from", &from);
  102.             printf(": [%s]\n", rbuf);
  103.  
  104.             }
  105.         else printf("Error in receiving response\n");
  106.         }
  107.         else printf("No response received after %d seconds\n",TIMEOUT);
  108.         printf("=======================================================\n");
  109.     }
  110.     close(s);
  111.     exit(0);
  112. }
  113.  
  114. /* Gets a line of text from standard input after having printed a prompt string
  115.    Substitutes end of line with '\0'
  116.    Empties standard input buffer but stores at most maxline-1 characters in the
  117.    passed buffer
  118. */
  119. int mygetline(char *line, size_t maxline, char *prompt)
  120. {
  121.     char    ch;
  122.     size_t  i;
  123.  
  124.     printf("%s", prompt);
  125.     for (i=0; i< maxline-1 && (ch = getchar()) != '\n' && ch != EOF; i++)
  126.         *line++ = ch;
  127.     *line = '\0';
  128.     while (ch != '\n' && ch != EOF)
  129.         ch = getchar();
  130.     if (ch == EOF)
  131.         return(EOF);
  132.     else    return(1);
  133. }
  134.  
  135. /* Utility function to display a string str
  136.    followed by an internet address a, written
  137.    in decimal notation
  138. */
  139. void showAddr(char *str, struct sockaddr_in *a)
  140. {
  141.     char *p;
  142.    
  143.     p = inet_ntoa(a->sin_addr);
  144.     printf("%s %s",str,p);
  145.     printf("!%u\n", ntohs(a->sin_port));
  146. }
  147.  
  148. /* Funzione per controllare se un buffer contiene la stringa "close" o "stop" */
  149. int iscloseorstop(char *buf)
  150. {
  151.     return (!strcmp(buf, "close") || !strcmp(buf, "stop"));
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement