home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / GianuzziV / SO1 / TCPtimeclient.c < prev    next >
C/C++ Source or Header  |  2005-04-03  |  2KB  |  80 lines

  1. /**********************************************************************************
  2. * Programma:      TCPtimeclient.c                                                 *
  3. *                                                                                 *
  4. * finalita':      si connette con il server e legge il valore di time             *
  5. *                 il server e' TCPtimeserver.c                                    *
  6. *                                                                                 *
  7. * Sintassi:       TCPtimeclient IPserver [ portserver ]                           *
  8. *                            IPserver - IP del server in dotted notation          *
  9. *                            portserver  - port number del server                 *
  10. *                 portserver e' opzionale, se non passato si usa TIME_PORT        *
  11. *                                                                                 *
  12. ***********************************************************************************/
  13.  
  14. #include <stdio.h>
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <arpa/inet.h>
  19. #include <unistd.h>
  20.  
  21.  
  22. #define SIZE 1024
  23. char buf[SIZE];
  24.  
  25. #define TIME_PORT 3123
  26.  
  27. int main(int argc, char **argv)
  28. {
  29.   int sockfd;
  30.   int nread, port, err;
  31.   struct sockaddr_in serv_addr;
  32.  
  33.   if (argc < 2) {
  34.     fprintf(stderr, "usage: %s IPaddr  [ portserver ]\n", argv[0]);
  35.     exit(1);
  36.   }
  37.  
  38. // apro il socket
  39.   if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
  40.   {
  41.     perror(NULL);
  42.     exit(2);
  43.   }
  44.  
  45. // assegno la struttura indirizzo del server (argomento di
  46.   serv_addr.sin_family = AF_INET;
  47.   err = inet_aton(argv[1], &serv_addr.sin_addr);
  48.  
  49.   if (err<0) 
  50.      {
  51.       printf("client errore inet_addr \n");
  52.       exit(1);
  53.      }
  54.  
  55.   // Controlla gli argomenti passati nella command-line (port) 
  56.   if (argc > 2)  port = atoi (argv[2]);     // converte in binario 
  57.   else port = TIME_PORT;                    // default port number del server
  58.  
  59.   if (port > 0)  serv_addr.sin_port = htons((u_short)port);
  60.   else { 
  61.          printf ("client errore port number %s/n", argv[1]);
  62.          exit (1);
  63.     }
  64.  
  65.   if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
  66.   {
  67.     perror(NULL);
  68.     exit(3);
  69.   }
  70.  
  71.   printf("connesso con port %d  IP %s\n", ntohs(serv_addr.sin_port), inet_ntoa(serv_addr.sin_addr));
  72.  
  73.   nread = read(sockfd, buf, SIZE);
  74.   write(1, buf, nread);
  75.   close(sockfd);
  76.   exit(0);
  77. }
  78.  
  79.  
  80.