home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / sep91.zip / 9N09051A < prev    next >
Text File  |  1991-07-17  |  3KB  |  76 lines

  1. Listing 3. Datagram sockets example. The server program.
  2.  
  3.  
  4. /* services -- a datagram transmission example. server program. Works with 
  5.  * the  client program 'getservice'. The program receives the name of a service
  6.  * from a client running on the same machine or on a remote host. If it finds
  7.  * the service in the "/etc/services" file, it sends to the client an answer
  8.  * that includes the service name, the port number and the protocol used with
  9.  * the service. Otherwise it sends a negative answer.
  10.  *
  11.  *   Usage:  services &
  12.  */
  13.  
  14. #include   <sys/types.h>
  15. #include   <sys/socket.h>
  16. #include   <netinet/in.h>
  17. #include   <netdb.h>
  18.  
  19. #define    FAIL   1
  20. #define    SUCC   0
  21. #define    PORT_NUMBER   2228   /* arbitrarily chosen free port number
  22.                                  * for client-server communication. Must
  23.                                  * be the same in both client and server
  24.                                  */
  25.  
  26. /* ------------------------------------------------------------------- */
  27.  
  28. main(ac, av)
  29.     int ac;   char **av ;
  30. {
  31.     int sock ;                        /* socket descriptor */
  32.     struct sockaddr_in sock_addr ;    /* local socket address structure */
  33.     struct sockaddr_in peer_addr ;    /* client's socket address structure */
  34.     char *progr_name = av[0] ;        /* the present program name */
  35.     char service_name[30] ;           /* name of the service sougth by the client */
  36.     char response[256] ;              /* response to be sent to the client */
  37.     struct servent *serv_info, *getservbyname() ;
  38.     void error() ;                    /* same routine as printed in listing 1 */
  39.     int addr_len = sizeof(struct sockaddr_in) ;
  40.  
  41.  
  42.     /* allocate a datagram socket descriptor */
  43.     if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  44.         error(progr_name, ": error opening socket") ;
  45.  
  46.     /* copy server address and type to socket structure */
  47.     sock_addr.sin_port        = PORT_NUMBER ;
  48.     sock_addr.sin_family      = AF_INET ;
  49.     sock_addr.sin_addr.s_addr = INADDR_ANY ;
  50.  
  51.     /* bind the local address to the socket */
  52.     if (bind(sock, (struct sockaddr *)&sock_addr, sizeof sock_addr) != 0)
  53.         error(progr_name, ": error binding to datagram socket") ;
  54.  
  55.     /* loop to receive client's requests */
  56.  
  57.     setservent(1) ;    /* keeps the "/etc/services" file open */
  58.     for ( ; ;)  {
  59.         if (recvfrom(sock, service_name, sizeof service_name,
  60.                         0, &peer_addr, &addr_len) < 0)
  61.             error(progr_name, ": receive error") ;
  62.         if ((serv_info = getservbyname(service_name, (char *)0)) == NULL)  
  63.             strcpy(response, "Service Not Found") ;
  64.         else  
  65.             sprintf(response, "%s service found at port %d with protocol %s",
  66.                 serv_info->s_name, serv_info->s_port, serv_info->s_proto) ;  
  67.  
  68.         /* send response back to the client using its address
  69.          * "peer_addr" obtained with "recvfrom"
  70.          */
  71.         if (sendto(sock, response, sizeof response, 0, &peer_addr, addr_len) == -1) 
  72.             error(progr_name, ": send to client failed") ;
  73.     }
  74. }
  75.  
  76.