From: barmar@alum.mit.edu   
      
   In article ,   
    umutlu wrote:   
      
   > On 2010-10-25 16:17, Lew Pitcher wrote:   
   > > On October 25, 2010 09:35, in comp.protocols.tcp-ip,   
   > > miguelsanders@telenet.be wrote:   
   > >   
   > >> Is there any way how a TCP/IP C programmer can have more control on   
   > >> the source TCP port that will be used to setup the socket ?   
   > >   
   > > Use a bind(2) call ("man 2 bind") to connect your socket to a specific   
   > > source port number.   
   >   
   > Ok, normally the OS assigns randomly a free local port if one   
   > doesn't bind the socket to a specific port.   
   > But, for a special application I need to use raw sockets, ie.   
   > s = socket(PF_INET, SOCK_RAW, IPPROTO_TCP);   
   > Now, I would use a free random local port as the source port,   
   > but don't know how to get one. Is there a method to get such   
   > a port, and inform the OS (Linux) about the fact that   
   > this port is going to be used, so to prevent a possible use   
   > attempt of the same port by the normal socket functions?   
      
   Call bind() with the source port set to 0. The OS will find an   
   available local port and bind it. You can then call getsockaddr() on   
   the socket to find out the port it assigned.   
      
   --   
   Barry Margolin, barmar@alum.mit.edu   
   Arlington, MA   
   *** PLEASE don't copy me on replies, I'll read them in the group ***   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|