I have this gap in my understanding of networking that I've always had a hard time crossing.
Over the years I've written various little TCP/UDP sender/receiver projects -- mostly simple hello-world-level code, in order to learn the mechanics of sending/receiving data over TCP or UDP.
But almost all the examples I've referenced tend to use 127.0.0.x as the receiver's address. I.e. a TCP listener does a bind()
to a 127.0.0.x address.
Similarly, in my Udemy Docker & Kubernetes course, the instructor creates a Node.js web server that listens at 127.0.0.1 port 3000.
I have a shaky understanding that these "loopback" addresses are some OS feature that makes these "virtual addresses" and allows them to act as if they are real network addresses. (That explanation was probably really bad, revealing my shaky understanding)
My questions (I know multiple questions per post are frowned upon, but I hope these questions are logically grouped enough that it's justified asking these questions together):
- What is the difference between these loopback addresses (I assume 127.0.0.1 - 127.0.0.255?) and "real" (maybe non-loopback is the better word?) IP addresses?
- On a given PC, what governs whether an IP address is available to the C
bind()
function?
E.g in the following code snippet (adapted from here):
...what governs whether the IP address 72.212.216.41 is available on that particular PC for the C code to function correctly?struct sockaddr_in sin; int s = socket(AF_INET, SOCK_STREAM, 0); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(1221908521); // 72.212.216.41 according to http://www.aboutmyip.com/AboutMyXApp/IP2Integer.jsp?ipAddress=72.212.216.41 sin.sin_port = htons(3000); bind(s, (struct sockaddr *) &sin, sizeof sin);
- Who gave my network card the address 72.212.216.41?
- Is 72.212.216.41 something that's tightly-coupled to my network card and immutable? Or can some software process change it?
- Can one network card only have one IP address?
- Why do so many networking tutorials use of 127.0.0.1 instead of "non-loopback" addresses? (I don't mean to generalize, but I have found this to be largely true)
I think the gist of my questions is I'm trying to understand how IP address assignment/availability works in the context of a single PC.
Like what software/hardware says, "StoneThrow's PC, you're assigned 72.212.216.41, so your C programs are allowed to call bind()
with that address"?
ip addr
on most newish Linux,ifconfig
on other Unix, oripconfig
on Windows to see the (non-loopback on Windows) address(es) you can bind.