LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What does socket bind 0.0.0.0:myport mean? (https://www.linuxquestions.org/questions/programming-9/what-does-socket-bind-0-0-0-0-myport-mean-4175467233/)

fantasy1215 06-24-2013 07:30 PM

What does socket bind 0.0.0.0:myport mean?
 
First I use ifconfig to see all my network interfaces in my server A as below.
Code:

eth0      Link encap:Ethernet  HWaddr 00:50:56:9C:57:65 
          inet addr:100.1.3.102  Bcast:100.1.3.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fe9c:5765/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:15845432 errors:0 dropped:0 overruns:0 frame:0
          TX packets:15555116 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:268738931 (256.2 MiB)  TX bytes:2892271506 (2.6 GiB)
          Interrupt:185 Base address:0x2000

eth0:1    Link encap:Ethernet  HWaddr 00:50:56:9C:57:65 
          inet addr:100.1.3.163  Bcast:100.1.3.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:185 Base address:0x2000

eth0:2    Link encap:Ethernet  HWaddr 00:50:56:9C:57:65 
          inet addr:100.1.3.164  Bcast:100.1.3.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:185 Base address:0x2000

eth0:3    Link encap:Ethernet  HWaddr 00:50:56:9C:57:65 
          inet addr:100.1.3.183  Bcast:100.1.3.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:185 Base address:0x2000

eth0:4    Link encap:Ethernet  HWaddr 00:50:56:9C:57:65 
          inet addr:100.1.3.153  Bcast:100.1.3.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:185 Base address:0x2000

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:736637 errors:0 dropped:0 overruns:0 frame:0
          TX packets:736637 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:91001623 (86.7 MiB)  TX bytes:91001623 (86.7 MiB)

sit0      Link encap:IPv6-in-IPv4 
          NOARP  MTU:1480  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

In the c socket bind function, we give bind function an ip address to bind.
1、What will bind do if I give bind function none of the ip address listed with ifconfig?
2、What will bind do if I give bind function the address 0.0.0.0?
3、What will bind do if I give bind function the address 127.0.0.1?
Thanks in advance if you can unlock my mind.

dwhitney67 06-24-2013 08:15 PM

Quote:

Originally Posted by fantasy1215 (Post 4977887)
In the c socket bind function, we give bind function an ip address to bind.
1、What will bind do if I give bind function none of the ip address listed with ifconfig?
2、What will bind do if I give bind function the address 0.0.0.0?
3、What will bind do if I give bind function the address 127.0.0.1?
Thanks in advance if you can unlock my mind.

Why don't you write a C program to experiment on your own as to what bind() will do in each of the scenarios you outlined above? While you are at it, read the man-page for bind.

Oh sorry... you wanted the quick answer:

1. Fail.
2. Success.
3. Success.

fantasy1215 06-24-2013 08:56 PM

Quote:

Originally Posted by dwhitney67 (Post 4977912)
Why don't you write a C program to experiment on your own as to what bind() will do in each of the scenarios you outlined above? While you are at it, read the man-page for bind.

Oh sorry... you wanted the quick answer:

1. Fail.
2. Success.
3. Success.

Thanks for your reply.
I can experiment out the answer by writing the test code. Actually what I don't understand is the reason and the internal scenario.

ta0kira 06-25-2013 06:54 PM

Quote:

Originally Posted by dwhitney67 (Post 4977912)
Why don't you write a C program to experiment on your own as to what bind() will do in each of the scenarios you outlined above? While you are at it, read the man-page for bind.

I only saw the answer to the first one in the manpage. Also, experimentation gives you an idea of what to expect, but such anecdotes aren't a replacement for the facts, which I've learned the hard way numerous times.

If you use the INADDR_ANY macro when creating a socket, that expands to the netlong value of 0.0.0.0. This means that the socket will be bound to all available IP addresses on the machine, which is convenient but sometimes bad. If you use the INADDR_LOOPBACK macro, that expands to the netlong value of 127.0.0.1, which is the default IP address of the loopback interface. You should use those macros whenever appropriate, rather than the dot-decimal notation.

Kevin Barry


All times are GMT -5. The time now is 09:00 PM.