LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2013, 07:30 PM   #1
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Rep: Reputation: Disabled
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.
 
Old 06-24-2013, 08:15 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by fantasy1215 View Post
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.

Last edited by dwhitney67; 06-24-2013 at 08:16 PM.
 
1 members found this post helpful.
Old 06-24-2013, 08:56 PM   #3
fantasy1215
Member
 
Registered: Oct 2011
Posts: 75

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
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.
 
Old 06-25-2013, 06:54 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
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
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bind fails on socket thesource2 Programming 3 01-27-2012 03:54 AM
how to name a socket by bind tkmsr Programming 1 10-20-2010 11:30 AM
Can I bind to a serial port using BIND Socket API?? venkat_p257 Linux - General 2 12-04-2007 05:49 AM
can not bind socket to address terrormaniak Linux - Server 1 09-14-2007 01:16 PM
can not bind socket to address terrormaniak Linux - Software 0 09-12-2007 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration