user2574953
user2574953

Reputation: 63

Is 127.0.0.1 different from the IP address?

  1. I create a c# socket using 127.0.0.1 and port#. Now I create a local client(which is running locally on the same machine as server) and try to connect using machine's fqdn. It doesn't work. If I use 127.0.0.1 instead of fqdn for client, it works. Vice-versa, if I create a server using the local machine's fqdn and port# and if the client uses 127.0.0.1, it doesn't work. If I use fqdn in client, then things work. This makes me think what's the difference between the machine's own IP address/fqdn and 127.0.0.1, as I was under impression they both will refer to the same machine.

  2. In case of C# UdpClient, we just specify port#. How does c# deal with this issue then?

  3. In case if a machine has multiple NICs and if we are just specifying port# (in case of UdpClient for example), what NIC is used to create the port?

Upvotes: 0

Views: 2496

Answers (2)

Corey
Corey

Reputation: 16564

The IP address 127.0.0.1 is the address of the local loopback virtual network adapter that exists on every computer. This virtual adapter is entirely internal to the operating system and cannot be connected to from elsewhere. Programs running on the same machine can communicate with each other, but nothing else.

When you use a FQDN to connect to a computer the name is resolved to an IP address which belongs to that name, usually an address that is attached to an external network adapter. Let's say you computer's network address is 192.168.0.2. When you attempt to connect to any port at that address, it will never connect to a program listening on 127.0.0.1 any more than it would connect to a different computer listening on the address 192.168.0.3.

Addresses are important.

If you want you program to accept connections on any network adapter attached to the computer, bind it to the every address: 0.0.0.0.

Upvotes: 2

JohnD
JohnD

Reputation: 4002

  1. Your client resolves the FQDN which probably points to another interface, and not loopback. They do referrer to the same machine, however they are different interfaces.
  2. According to MSDN, UdpClient will listen on "the default interface"
  3. Again, whatever microsoft defines as "the default interface". In reality it means if you listen on one IP address, you will only get traffic bound for that IP.

Upvotes: 0

Related Questions