bind() chosing a port

This is a discussion on bind() chosing a port within the Linux Programming forums, part of the Platform Specific Boards category; I set the server.sin_port=0 and then I call the bind function that doesn't return any error: Where can I get ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    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, 02:58 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  4. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21