Thread: bind() chosing a port

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    bind() chosing a port

    I set the server.sin_port=0 and then I call the bind function that doesn't return any error:

    Where can I get the port that was asigned by the bind function?

    Code:
            int sd;         //server descriptor
            struct sockaddr_in server;
            bzero(&server,sizeof(server));
            server.sin_family=AF_INET;
            server.sin_addr.s_addr=htonl(INADDR_ANY);
            server.sin_port=htons(0);
    
            if(bind(sd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1); // error
            
            printf("%d\n",ntohs(server.sin_port)); //is still 0

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Have you read bind description?
    For TCP/IP, if the port is specified as zero, the service provider assigns a unique port to the application with a value between 1024 and 5000. The application can use getsockname after calling bind to learn the address and the port that has been assigned to it. If the Internet address is equal to INADDR_ANY, getsockname cannot necessarily supply the address until the socket is connected, since several addresses can be valid if the host is multihomed.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    what happend to the itoa(..) function in unix ?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by spank View Post
    what happend to the itoa(..) function in unix ?
    This function is not standard - unix has nothing to do with it...
    use sprintf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by spank View Post
    Code:
    bzero(&server,sizeof(server));
    Don't use bzero -- it's been obsolete for decades. It makes you look like a stupid hacker (I'm not saying you are one -- but let me guess, you've been learning socket programming by reading hacking sites, right?) Use memset() instead.

    Also, why do you need to know the port number? Why are you binding without specifying the port, anyway?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can I bind a UDP socket to a port, but send to any other port?
    By trillianjedi in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-25-2009, 04:27 PM
  2. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 03:28 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM