home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!concert!gatech!destroyer!caen!batcomputer!rpi!crdgw1!rdsunx.crd.ge.com!julius!foggh
- From: foggh@julius.crd.ge.com (H.W. Fogg)
- Newsgroups: comp.unix.programmer
- Subject: Re: Determining what ports are unused at run-time???
- Message-ID: <1992Nov17.205706.5956@crd.ge.com>
- Date: 17 Nov 92 20:57:06 GMT
- References: <1992Nov16.221540.1940@Trirex.COM>
- Sender: usenet@crd.ge.com (Required for NNTP)
- Reply-To: foggh@julius.crd.ge.com (H.W. Fogg)
- Organization: General Electric Corporate R & D
- Lines: 140
- Nntp-Posting-Host: julius.crd.ge.com
-
- In article <1992Nov16.221540.1940@Trirex.COM>, bmehlman@trirex.com(Ben
- Mehlman) writes:
- |>
- |>I have an application in which a number of processes are running on either
- |>the same or different machines. Each of these processes will be accepting
- |>connections on some port. The process binds a socket to its port, and
- |>then reports the port number to a central process listening on a well
- |>known port.
- |>
- |>The question is: How do I assign the port numbers at run time? Right now
- |>Each process is manually assigned a known free port when it starts up. I
- |>would like instead for it to somehow determine what is a good port on that
- |>machine, and use it.
- |>
- |>Any suggestions would be greatly appreciated.
- |>Thanks!
- |>--
- |>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- |> \|/ Ben Mehlman Indigo..
- |> ^ ^ Trirex Systems Inc
- |> # # bmehlman@trirex.com Indigoing..
- |> @
- |> \___/ >NeXTMail Welcome< Indigone.
-
-
- Here are some code fragments which might get you started:
-
- /*
- * Copyright (C) 1991, 1992 General Electric
- *
- * Permission to use, copy, modify, and distribute this
- * software and its documentation for any purpose and without
- * fee is hereby granted, provided that the above copyright
- * notice appear in all copies and that both that copyright
- * notice and this permission notice appear in supporting
- * documentation, and that the name of General Electric not be used in
- * advertising or publicity pertaining to distribution of the
- * software without specific, written prior permission.
- * General Electric makes no representations about the suitability of
- * this software for any purpose. It is provided "as is"
- * without express or implied warranty.
- *
- * This work was supported by the DARPA Initiative in Concurrent
- * Engineering (DICE) through DARPA Contract MDA972-88-C-0047.
- *
- */
-
- /* this routine is used below */
-
- char * getaddrbyname(hname)
- char *hname;
- {
- register char *ptr;
- register struct hostent *hostptr;
- struct in_addr *in_ptr;
- char *inet_ntoa();
-
- ptr = hname;
- if((hostptr = gethostbyname(ptr)) == NULL)
- {
- printf("gethostbyname error for host: %s\n", ptr);
- exit(1);
- }
- in_ptr = (struct in_addr *) *(hostptr->h_addr_list);
- return(inet_ntoa(*in_ptr));
- }
-
-
- *********** some of the variables used in the following code fragment
- ***************
- struct sockaddr_in serv_addr;
- int sockfd, channel;
- int port;
- int sockfd, channel, port;
- int size;
- char current_hostname[31];
-
- *********** code fragment ******************
- /* get the name of the host currently running on */
- if (gethostname(current_hostname, 31) < 0)
- {
- printf("Could not get current hostname\n");
- exit(1);
- }
-
- /* initialize the port to 0 - let the system assign next free one */
- port = 0;
-
- /* allocate a socket */
- if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
- {
- printf("Could not get socket\n");
- exit(1);
- }
-
- /* build the socket address structure used to bind port to socket */
- /* a port value of zero binds the next available port to the socket */
- memset(&serv_addr, '\0', sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = inet_addr(getaddrbyname(current_hostname));
- serv_addr.sin_port = htons(port);
-
- /* bind the socket and the port */
- if (bind(priv_sockfd, (struct sockaddr *)&serv_addr,
- sizeof(serv_addr)) < 0)
- {
- printf("Could not bind to socket\n");
- exit(1);
- }
-
- /* get the socket address structure for the newly bound socket */
- size = sizeof(serv_addr);
- if (getsockname(sockfd, (struct sockaddr *)&serv_addr, &size) < 0)
- {
- printf("Could not get socket name\n");
- exit(1);
- }
-
- /* extract the port that was assigned by the system from */
- /* the socket address structure */
- port = ntohs(serv_addr.sin_port);
- printf("Port = %d\n", port);
-
-
- /***** stuff not associated with this problem deleted from here ******/
-
-
- /* listen on the socket for a connect request */
- listen(sockfd, 5);
-
- /* accept any connect request */
- size = sizeof(serv_addr);
- channel = accept(sockfd, (struct sockaddr *)&serv_addr, &size);
- if (channel < 0)
- {
- printf("Could not accept connection\n");
- exit(1);
- }
-
- ********** end of code fragment **********
-