Thread: What does bind() do, exactly?

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    What does bind() do, exactly?

    I know, you need to bind() explicitly if you want to receive anything (i.e. incoming connection requests, or connectionless datagrams), so that the sender can actually know where to send packets; and the sender need not bind to a specific port (or explicitly bind at all, for connection-oriented sockets), since its port doesn't need to be pre-known and it's an unnecessary risk since the port may already be in use.

    So I have a fair idea of what the port is for, in the sockaddr_in struct - something along the lines of a PO box or apt. number. However, what I don't understand is what the IP address is for. In all examples I've seen, it is simply set to INADDR_ANY, with the explanation being "any address is fine". What exactly does this address signify? For some time, I was thought that it was the range of addresses that you are interested in receiving packets from; but that doesn't make sense, since you can only specify one address or all addresses. After this, I had the impression that it has something to do with the local computer's hardware, but I am unsure exactly what. Can someone explain what it is used for, and possibly give an example of a situation in which it would be set to something other than INADDR_ANY? Thanks.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    A host can have more then one network interface INADDR_ANY means you want to bind to any available address otherwise you can specify a particular network interface in which case only packets send to that interface (to the IP that that interface has) will be received.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah, that seems to make more sense - so in other words, effectively (but not exactly), a single computer may be visible as two or more different hosts, and you have the option of keeping these 'host' addresses distinct for different purposes or combining them with their different addresses in a single server (I assume each interface has a distinct external IP)? Then, does that mean you can bind multiple sockets in separate applications to the same port?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Hunter2
    Ah, that seems to make more sense - so in other words, effectively (but not exactly), a single computer may be visible as two or more different hosts, and you have the option of keeping these 'host' addresses distinct for different purposes or combining them with their different addresses in a single server (I assume each interface has a distinct external IP)?
    Yes, each network interface has it's own IP address. I don't think a socket can be bound to more then one interface at a time allthough I'm not sure.
    Then, does that mean you can bind multiple sockets in separate applications to the same port?
    As long as each socket was using a different interface then I don't see any reason why the same port number can't be used as many times as there are interfaces.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I don't think a socket can be bound to more then one interface at a time allthough I'm not sure.
    Ah shoot, that's right. I confused "bind to any address" with "bind to all addresses". Then that means, if you bind using INADDR_ANY, you run the risk of binding to the wrong interface and ending up with a "packets don't show up" problem? How then do you know which interface to choose?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I don't know what would be the best way but you could call gethostbyname
    on "localhost" to get the list of addresses that the local host has and then give the user the choice or just read IP(s) from a configuration file and only listen on those IP(s).
    EDIT
    It seams you were right, a test reveals that using INADDR_ANY allows connections on all interfaces.
    Last edited by Quantum1024; 07-03-2005 at 08:30 PM.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah, I see. Sure is convenient if INADDR_ANY acts as you said, otherwise it could become quite a chore trying to find out what interface the system uses to connect to the internet and explicitly bind to that
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> I confused "bind to any address" with "bind to all addresses". <<

    INADDR_ANY does both, depending on whether you are receiving or sending.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah. I guess that makes sense; you can't send a packet on more than one interface, otherwise if the two computers are connected by more than one interface (device?), there'd be duplicate packets.

    Then if you want to set up a UDP server socket by binding using INADDR_ANY, am I right in thinking that in a situation where a packet is received and the server responds on the same socket with a packet sent to the 'received from' address and port, the operation will NOT necessarily succeed (i.e. because of an interface discrepancy, not plain packet loss)?

    **EDIT**
    And, since connect() implicitly calls bind(), does that mean that the interface used by connect() is unpredictable unless specified in a previous call to bind()?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    As far as addresses go, I like to think of binding a socket as being the equivalent to telling the socket to discard all packets except those destined for the bound address (obviously, that is unless the bound address is INADDR_ANY). This is a higher level of thought, but it affords a better understanding of exactly what you're asking your socket to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bind() fails with WSAEACCES
    By pheres in forum Tech Board
    Replies: 2
    Last Post: 02-24-2009, 01:58 PM
  2. 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
  3. Bind();
    By SirCrono6 in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-26-2005, 08:01 PM
  4. bind
    By chrismiceli in forum Linux Programming
    Replies: 3
    Last Post: 08-30-2003, 11:44 PM
  5. bind() error
    By PutoAmo in forum C Programming
    Replies: 5
    Last Post: 05-23-2002, 03:57 PM