Getting the system generated port value from bind 
Author Message
 Getting the system generated port value from bind

Hello,

I am using the "bind" call to bind the socket fd to a port value.  I
would like to have the system assign the port numbers from its reserved
range of 1024 - 5000.  The call works fine but I need to know the port
the assigned in the "bind" call.  How do I do this?

I will add that I am using VAXELN 4.6 and it is missing the "rresvport"
call which would effect do exactly what I am asking.  (Thanks DEC...)

Any help or suggests anyone could provide would be of great help.

Thanks,
Bob Wiggins
Fisher-Rosemount Systems, Inc.

########## CODE FRAGMENT ###############

  if( err = connect( sockfd1, (struct sockaddr *)&serv_addr,
                     sizeof(serv_addr) ) >= 0 )
  {

    if ( (*socktemp = socket( AF_INET, SOCK_STREAM, 0)) == -1 )
    {
       xdUnixErrorHandler();
       return( False );
    }

     setsockopt( *socktemp, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
     serv2_addr.sin_family = AF_INET;
     serv2_addr.sin_port = 0;
     serv2_addr.sin_addr.s_addr = INADDR_ANY;

     /* bind 2nd socket with port on logical host. */
     if( bind( *socktemp,(struct sockaddr *)&serv2_addr,
               sizeof(serv2_addr) ) )
      {
       xdUnixErrorHandler();
       return( False );
      }

    listen( *socktemp,1 );
    /* At this point, I need to know the port value the system assigned
       How do I get this number */
    lport = ??????
    sprintf(num,"%d", lport);

    /* string contains port number of secondary port that rhost will
    ** read this port number and create a socket with a reserved port and
    ** connects to clients secondary port
    */
    if( write( sockfd1, num, strlen(num) + 1) != strlen(num)+1 )
    {
      return( False );
    }
    emask = wmask = 0;
    rmask = ( 1<< *socktemp );
    timeout.tv_sec = 30;
    timeout.tv_usec = 0;

    if((select( 32, &rmask, &wmask,&emask,&timeout)) < 1 )
    {
       xdUnixErrorHandler();
       return( False );
    }



Mon, 13 Mar 2000 03:00:00 GMT  
 Getting the system generated port value from bind



Quote:
> Hello,

> I am using the "bind" call to bind the socket fd to a port
value.  I
> would like to have the system assign the port numbers from its
reserved
> range of 1024 - 5000.  The call works fine but I need to know
the port
> the assigned in the "bind" call.  How do I do this?

> I will add that I am using VAXELN 4.6 and it is missing the
"rresvport"
> call which would effect do exactly what I am asking.  (Thanks
DEC...)

> Any help or suggests anyone could provide would be of great
help.

> Thanks,
> Bob Wiggins
> Fisher-Rosemount Systems, Inc.

> ########## CODE FRAGMENT ###############

>   if( err = connect( sockfd1, (struct sockaddr *)&serv_addr,
>                      sizeof(serv_addr) ) >= 0 )
>   {

>     if ( (*socktemp = socket( AF_INET, SOCK_STREAM, 0)) == -1
)
>     {
>        xdUnixErrorHandler();
>        return( False );
>     }

>      setsockopt( *socktemp, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on));
>      serv2_addr.sin_family = AF_INET;
>      serv2_addr.sin_port = 0;
>      serv2_addr.sin_addr.s_addr = INADDR_ANY;

>      /* bind 2nd socket with port on logical host. */
>      if( bind( *socktemp,(struct sockaddr *)&serv2_addr,
>                sizeof(serv2_addr) ) )
>       {
>        xdUnixErrorHandler();
>        return( False );
>       }

>     listen( *socktemp,1 );
>     /* At this point, I need to know the port value the system
assigned
>        How do I get this number */
>     lport = ??????
>     sprintf(num,"%d", lport);

>     /* string contains port number of secondary port that
rhost will
>     ** read this port number and create a socket with a
reserved port and
>     ** connects to clients secondary port
>     */
>     if( write( sockfd1, num, strlen(num) + 1) != strlen(num)+1
)
>     {
>       return( False );
>     }
>     emask = wmask = 0;
>     rmask = ( 1<< *socktemp );
>     timeout.tv_sec = 30;
>     timeout.tv_usec = 0;

>     if((select( 32, &rmask, &wmask,&emask,&timeout)) < 1 )
>     {
>        xdUnixErrorHandler();
>        return( False );
>     }

Hi Robert,

I would suggest you try one of the UNIX newsgroups, since what
you are asking is operating system specific and off topic for
comp.lang.c.

Jack



Tue, 14 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Generating keypress...after getting input from system hook

2. data binding and showing value through messagbox

3. Null values & data binding in Wndows Forms

4. Combo box Data Binding Value inconsistencies

5. Binding enum values to a DropDownList

6. which value to be bind

7. which value to be bind

8. Bound column value for DataGrid C# ......DataKeys

9. sockets and ports -- bind()

10. Binding to NDS with System.DirectoryServices.DirectoryEntry

11. Generating Boolean values on BSD 4.3

12. Routine generates different floating point values

 

 
Powered by phpBB® Forum Software