Reputation: 63
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.
In case of C# UdpClient, we just specify port#. How does c# deal with this issue then?
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
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
Reputation: 4002
UdpClient
will listen on "the default interface"Upvotes: 0