home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / programm / 5325 < prev    next >
Encoding:
Text File  |  1992-11-17  |  5.0 KB  |  154 lines

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!concert!gatech!destroyer!caen!batcomputer!rpi!crdgw1!rdsunx.crd.ge.com!julius!foggh
  2. From: foggh@julius.crd.ge.com (H.W. Fogg)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: Determining what ports are unused at run-time???
  5. Message-ID: <1992Nov17.205706.5956@crd.ge.com>
  6. Date: 17 Nov 92 20:57:06 GMT
  7. References: <1992Nov16.221540.1940@Trirex.COM>
  8. Sender: usenet@crd.ge.com (Required for NNTP)
  9. Reply-To: foggh@julius.crd.ge.com (H.W. Fogg)
  10. Organization: General Electric Corporate R & D
  11. Lines: 140
  12. Nntp-Posting-Host: julius.crd.ge.com
  13.  
  14. In article <1992Nov16.221540.1940@Trirex.COM>, bmehlman@trirex.com(Ben
  15. Mehlman) writes:
  16. |>
  17. |>I have an application in which a number of processes are running on either  
  18. |>the same or different machines.  Each of these processes will be accepting  
  19. |>connections on some port.  The process binds a socket to its port, and  
  20. |>then reports the port number to a central process listening on a well  
  21. |>known port.
  22. |>
  23. |>The question is: How do I assign the port numbers at run time?  Right now  
  24. |>Each process is manually assigned a known free port when it starts up.  I  
  25. |>would like instead for it to somehow determine what is a good port on that  
  26. |>machine, and use it.
  27. |>
  28. |>Any suggestions would be greatly appreciated.
  29. |>Thanks!
  30. |>--
  31. |>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  32. |>    \|/        Ben Mehlman                      Indigo..
  33. |>   ^   ^       Trirex Systems Inc      
  34. |>   #   #       bmehlman@trirex.com                   Indigoing..
  35. |>     @
  36. |>   \___/       >NeXTMail Welcome<                           Indigone.
  37.                      
  38.  
  39. Here are some code fragments which might get you started:
  40.  
  41. /*
  42.  *  Copyright (C) 1991, 1992 General Electric    
  43.  *    
  44.  *  Permission to use, copy, modify, and distribute this
  45.  *  software and its documentation for any purpose and without
  46.  *  fee is hereby granted, provided that the above copyright
  47.  *  notice appear in all copies and that both that copyright
  48.  *  notice and this permission notice appear in supporting
  49.  *  documentation, and that the name of General Electric not be used in
  50.  *  advertising or publicity pertaining to distribution of the
  51.  *  software without specific, written prior permission.
  52.  *  General Electric makes no representations about the suitability of
  53.  *  this software for any purpose.  It is provided "as is"
  54.  *  without express or implied warranty.
  55.  *
  56.  *  This work was supported by the DARPA Initiative in Concurrent
  57.  *  Engineering (DICE) through DARPA Contract MDA972-88-C-0047.
  58.  *
  59.  */
  60.  
  61. /* this routine is used below */
  62.  
  63. char * getaddrbyname(hname)
  64.      char *hname;
  65. {
  66.   register char *ptr;
  67.   register struct hostent *hostptr;
  68.   struct in_addr *in_ptr;
  69.   char *inet_ntoa();
  70.   
  71.   ptr = hname;
  72.   if((hostptr = gethostbyname(ptr)) == NULL)
  73.     {
  74.       printf("gethostbyname error for host: %s\n", ptr);
  75.       exit(1);
  76.     }
  77.   in_ptr = (struct in_addr *) *(hostptr->h_addr_list);
  78.   return(inet_ntoa(*in_ptr));
  79. }
  80.  
  81.  
  82. *********** some of the variables used in the following code fragment
  83. ***************
  84. struct sockaddr_in serv_addr;
  85. int  sockfd, channel;
  86. int  port;
  87. int sockfd, channel, port;
  88. int size;
  89. char current_hostname[31];
  90.  
  91. ***********  code fragment ******************
  92.       /* get the name of the host currently running on */
  93.       if (gethostname(current_hostname, 31) < 0)
  94.     {
  95.       printf("Could not get current hostname\n");
  96.       exit(1);
  97.     }
  98.  
  99.       /* initialize the port to 0 - let the system assign next free one */ 
  100.       port = 0;
  101.  
  102.       /* allocate a socket */      
  103.       if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  104.     {
  105.       printf("Could not get socket\n");
  106.       exit(1);
  107.     }
  108.  
  109.       /* build the socket address structure used to bind port to socket */
  110.       /* a port value of zero binds the next available port to the socket */
  111.       memset(&serv_addr, '\0', sizeof(serv_addr));
  112.       serv_addr.sin_family = AF_INET;
  113.       serv_addr.sin_addr.s_addr = inet_addr(getaddrbyname(current_hostname));
  114.       serv_addr.sin_port = htons(port);
  115.  
  116.       /* bind the socket and the port */      
  117.       if (bind(priv_sockfd, (struct sockaddr *)&serv_addr,
  118. sizeof(serv_addr)) < 0)
  119.     {
  120.       printf("Could not bind to socket\n");
  121.       exit(1);
  122.     }
  123.  
  124.       /* get the socket address structure for the newly bound socket */      
  125.       size = sizeof(serv_addr);
  126.       if (getsockname(sockfd, (struct sockaddr *)&serv_addr, &size) < 0)
  127.     {
  128.       printf("Could not get socket name\n");
  129.       exit(1);
  130.     }
  131.  
  132.       /* extract the port that was assigned by the system from */
  133.       /* the socket address structure */      
  134.       port = ntohs(serv_addr.sin_port);
  135.       printf("Port = %d\n", port);
  136.  
  137.  
  138.       /***** stuff not associated with this problem deleted from here ******/
  139.  
  140.       
  141.       /* listen on the socket for a connect request */
  142.       listen(sockfd, 5);
  143.  
  144.       /* accept any connect request */
  145.       size = sizeof(serv_addr);
  146.       channel = accept(sockfd, (struct sockaddr *)&serv_addr, &size);
  147.       if (channel < 0)
  148.     {
  149.       printf("Could not accept connection\n");
  150.       exit(1);
  151.     }
  152.  
  153. ********** end of code fragment **********      
  154.