home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / socket / socketp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-07  |  2.6 KB  |  121 lines

  1. /*
  2.  
  3. $Header: socketp.c[1.4] Sun Aug  9 03:48:03 1992 nickel@cs.tu-berlin.de proposed $
  4. This file is part of socket(1).
  5. Copyright (C) 1992 by Juergen Nickelsen <nickel@cs.tu-berlin.de>
  6. Please read the file COPYRIGHT for further details.
  7.  
  8. */
  9.  
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <sys/errno.h>
  13. #include <netinet/in.h>
  14. #include <netdb.h>
  15. #include <stdio.h>
  16. #include "globals.h"
  17.  
  18. /*
  19.  * create a server socket on PORT accepting QUEUE_LENGTH connections
  20.  */
  21. int create_server_socket(port, queue_length)
  22. int port ;
  23. int queue_length ;
  24. {
  25.     struct sockaddr_in sa ;
  26.     int s;
  27.  
  28.     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  29.     return -1 ;
  30.     }
  31.  
  32.     bzero((char *) &sa, sizeof(sa)) ;
  33.     sa.sin_family = AF_INET ;
  34.     sa.sin_addr.s_addr = htonl(INADDR_ANY) ;
  35.     sa.sin_port = htons(port) ;
  36.  
  37.     if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
  38.     return -1 ;
  39.     }
  40.     if (listen(s, 1) < 0) {
  41.     return -1 ;
  42.     }
  43.  
  44.     return s ;
  45. }
  46.  
  47.  
  48. /* create a client socket connected to PORT on HOSTNAME */
  49. int create_client_socket(hostname, port)
  50. char **hostname ;
  51. int port ;
  52. {
  53.     struct sockaddr_in sa ;
  54.     struct hostent *hp ;
  55.     int a, s ;
  56.     long addr ;
  57.  
  58.  
  59.     bzero(&sa, sizeof(sa)) ;
  60.     if ((addr = inet_addr(*hostname)) != -1) {
  61.     /* is Internet addr in octet notation */
  62.     bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr)) ; /* set address */
  63.     sa.sin_family = AF_INET ;
  64.     } else {
  65.     /* do we know the host's address? */
  66.     if ((hp = gethostbyname(*hostname)) == NULL) {
  67.         return -2 ;
  68.     }
  69.     *hostname = hp->h_name ;
  70.     bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
  71.     sa.sin_family = hp->h_addrtype ;
  72.     }
  73.  
  74.     sa.sin_port = htons((u_short) port) ;
  75.  
  76.     if ((s = socket(sa.sin_family, SOCK_STREAM, 0)) < 0) { /* get socket */
  77.     return -1 ;
  78.     }
  79.     if (connect(s, &sa, sizeof(sa)) < 0) {                  /* connect */
  80.     close(s) ;
  81.     return -1 ;
  82.     }
  83.     return s ;
  84. }
  85.  
  86. /* return the port number for service NAME_OR_NUMBER. If NAME is non-null,
  87.  * the name is the service is written there.
  88.  */
  89. int resolve_service(name_or_number, protocol, name)
  90. char *name_or_number ;
  91. char *protocol ;
  92. char **name ;
  93. {
  94.     struct servent *servent ;
  95.     int port ;
  96.  
  97.     if (is_number(name_or_number)) {
  98.     port = atoi(name_or_number) ;
  99.     if (name != NULL) {
  100.         servent = getservbyport(htons(port), "tcp") ;
  101.         if (servent != NULL) {
  102.         *name = servent->s_name ;
  103.         } else {
  104.         *name = NULL ;
  105.         }
  106.     }
  107.     return port ;
  108.     } else {
  109.     servent = getservbyname(name_or_number, "tcp") ;
  110.     if (servent == NULL) {
  111.         return -1 ;
  112.     }
  113.     if (name != NULL) {
  114.         *name = servent->s_name ;
  115.     }
  116.     return ntohs(servent->s_port) ;
  117.     }
  118. }
  119.  
  120. /*EOF*/
  121.