Re: More allocating portnumbers....
From: ZoombyWoof (zoombywoofremove_at_thishotmail.com)
Date: 07/04/04
- Next message: sean larsson: "Re: usb 2.0 specification, mass storage class SCSI emulating devices"
- Previous message: Kasper Dupont: "Re: More allocating portnumbers...."
- In reply to: Kasper Dupont: "Re: More allocating portnumbers...."
- Next in thread: Kasper Dupont: "Re: More allocating portnumbers...."
- Reply: Kasper Dupont: "Re: More allocating portnumbers...."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 04 Jul 2004 21:47:50 +0200
Kasper Dupont wrote:
> ZoombyWoof wrote:
>
>>HI all again. I have now come to the point where my programs compile
>>fine but something weird is happening and I cant find out what.
>>
>>Process A creates a socket, binds it to port 0 and then I call
>>getsockname to retrieve the port allocated.
>
>
> I'm not sure you can actually bind to port 0. IIRC giving
> zero as the port number is interpreted as a request for the
> kernel to chose a free port number.
That is exactly what I want. I want the kernel to get me a free port
number and then retrieve it by a call to getsockname. I will then use
this portnumber and registrate it in a database so remote processes can
get this portnumber and connect to.
>
>
>>int allocPort( int *listensock )
>
>
> Why do you take a pointer as argument. Normally you would
> take no argument and return the filedescriptor (or -1 in
> case of an error).
allocPort is called by another function. Sure I could have had allocPort
return the filedescr, but I choose this method, it should work, and it
does. listensock is later on passed on to a created thread, and
listen/accept is called on it and it works. It's just that it doesnt
listen to the portnumber returned from getsockname, it listens to
another one....
>
>
>>{
>> stat = OpenSocket( listensock );
>
>
> Where is stat and OpenSocket declared? I think stat should
> have been a local variable.
OpenSocket is just e generic routine I use (It takes care of small
differences between win32 and linux). I didnt put out local variable
declarations in my example.
>
>
*snip*
>> CloseSocket( *listensock );
>> return( -1 );
>> }
>>
>> // Bind our adress
>> memset( (char*)&serv_addr, '\0', sizeof(serv_addr) );
>> serv_addr.sin_family = AF_INET;
>> serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
>> serv_addr.sin_port = 0;
>> if( (bind(*listensock, (struct sockaddr*)&serv_addr,
>>sizeof(serv_addr))) < 0 )
>> {
>> perror( "bind" );
>> sprintf( buf, "Error bind local address. errno = %d.\n", errno );
>> fprintf( stderr, buf );
>> CloseSocket( *listensock );
>> return( -1 );
>> }
>>
>> clilen = sizeof(cli_addr);
>> stat = getsockname( *listensock, (struct sockaddr*)&cli_addr, &clilen );
>> if( stat < 0 )
>> {
>> perror( "getsockname" );
>> sprintf( buf, "Error getsockname. errno = %d.\n", errno );
>> fprintf( stderr, buf );
>> CloseSocket( *listensock );
>> return( -1 );
>> }
>>}
>>
>>The port number returned in cli_addr is mostly around 61000 or
>>something, lets say 61228.
>
>
> Strange, my ip_local_port_range is 32768-61000. So why
> does it allocate something just outside that range?
well, thats what I want to know. When this program runs, the portnumber
returned in cli_addr is something else than what the process actually
listens too when I check with netstat.
>
>
>>I then issue a listen and accept call on the listensock and if I do a
>>netstat, I see that my process listens to 31015 or something like that.
>>If I telnet localhost 31015 my process accepts the connection. If I
>>telnet to the port getsockname returned for me, nothing happens.
>>
>>Why does my process listens and accepts on some other portnumber that
>>the socket descriptor was binded to ??
>
>
> I couldn't compile your code and test it. Some parts
> seems to be missing.
It wasnt complete. The important stuff was there just to show how I
called bind, getsockname etc.
/ZW
>
- Next message: sean larsson: "Re: usb 2.0 specification, mass storage class SCSI emulating devices"
- Previous message: Kasper Dupont: "Re: More allocating portnumbers...."
- In reply to: Kasper Dupont: "Re: More allocating portnumbers...."
- Next in thread: Kasper Dupont: "Re: More allocating portnumbers...."
- Reply: Kasper Dupont: "Re: More allocating portnumbers...."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|