socket bind

       int fd;
       int sock_type =  SOCK_STREAM;

        if ((fd = socket(AF_INET, sock_type, IPPROTO_TCP)) < 0) {
              // ERROR 
        }

        struct sockaddr_in sin;
        memset((char *)&sin, 0, sizeof (sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = 0;  // Why does this work even if it is 0. 
                 // Is there any significance of '0' compared to real ip adress of my host ??
        sin.sin_port = htons(port);

        while (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
        {
                // sleep for fews and retry and succeed
        }

Open in new window


// Why does this code works and also bind correctly even though I am assigning "0" to sin.sin_addr.s_addr
perlperlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

perlperlAuthor Commented:
What is the difference in behavior when I set s_adrr

1) sin.sin_addr.s_addr = 0  
2) sin.sin_addr.s_addr  = // uint32 value of localhost 127.0.0.1
3) sin.sin_addr.s_addr = // uint32 value of actual IP address. example 122.2.2.4


Does Case 1 blocks any incoming request from external to my host. (I mean any external client other than the request from same host)
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
For case 1), 0 is actually the value of a constant called INADDR_ANY, and so passing 0 (either literally or via that constant) is a "special" value which tells bind() to bind to ALL available interfaces. This means that the socket will accept incoming connections regardless of whether the come in externally or internally via localhost.

The other cases bind to specific interfaces and so only allow incoming connections via the particular interface, ie. 127.0.0.1 binds to localhost and so only local connections are allowed, etc.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
perlperlAuthor Commented:
Thanks mccarl.

case 1 -> open to entrie world (even outside subnet). If I have three NICS, it will listen on all the three interfaces.
case 2 -> only local connection allowed when client connects using 127.0.0.1
case 3 -> What does this mean then ? Is this similar to case 1 except it will only listen to one of the specific three NICS only.
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Sorry about the delay, I missed this one somehow...

case 1 -> open to entrie world (even outside subnet).
Whether it is open to the entire would outside a subnet is not determined here. That would be determined by whatever routers/firewalls/etc that are a part of the network. All this is saying, (as you correctly mention) is that incoming connections will be accepted on any interface available to the tcp/ip stack, ie. if you have three NIC's it will listen to all 3 AND the localhost interface.

The comments made for the other 2 cases are correct.
0
perlperlAuthor Commented:
Thanks a lot!!!!
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
You're welcome! :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.