Assuming the following is defined in .../hosts:
127.0.0.1 localhost
What, if any, are the actual differences between using 127.0.0.1 and localhost as the server name, especially when hitting processes running locally that are listening for connections?
Well, the most likely difference is that you still have to do an actual lookup of localhost somewhere.
If you use 127.0.0.1, then (intelligent) software will just turn that directly into an IP address and use it. Some implementations of gethostbyname will detect the dotted format (and presumably the equivalent IPv6 format) and not do a lookup at all.
Otherwise, the name has to be resolved. And there's no guarantee that your hosts file will actually be used for that resolution (first, or at all) so localhost may become a totally different IP address.
By that I mean that, on some systems, a local hosts file can be bypassed. The host.conf file controls this on Linux (and many other Unices).
Wikipedia sums this up well:
On modern computer systems, localhost as a hostname translates to an IPv4 address in the 127.0.0.0/8 (loopback) net block, usually 127.0.0.1, or ::1 in IPv6.
The only difference is that it would be looking up in the DNS for the system what localhost resolves to. This lookup is really, really quick. For instance, to get to stackoverflow.com you typed in that to the address bar (or used a bookmarklet that pointed here). Either way, you got here through a hostname. localhost provides a similar functionality.
some applications will treat "localhost" specially. the mysql client will treat localhost as a request to connect to the local unix domain socket instead of using tcp to connect to the server on 127.0.0.1. This may be faster, and may be in a different authentication zone.
I don't know of other apps that treat localhost differently than 127.0.0.1, but there probably are some.
Well, by IP is faster.
Basically, when you call by server name, it is converted to original IP.
But it would be difficult to memorize an IP, for this reason the domain name was created.
Personally I use http://localhost instead of http://127.0.0.1 or http://username.
There is nothing different. One is easier to remember than the other. Generally, you define a name to associate with an IP address. You don't have to specify localhost for 127.0.0.1, you could specify any name you want.
Related
I have been trying to learn socket programming in C++ and got some progress for the basics. But I understood that basics are not fundamentals.
One of those fundamentals is about the question which is stated in the title. Lets say I have two separate applications running on the same computer. One of them has a server socket and the other one has a client.
When the server gets an IP address automatically, how can client find the server? Do I have to assign an IP address that is known by the client? In that case, maybe that IP address is not available on the network.
Or can client find and connect to the server by sone kind of name or id?
Have the server bind to all interfaces and have the client lookup 'localhost' by name; it's resolved locally, (ie. no external DNS service required), to an IP address stored in a 'hosts' file, and is set by default to 127.0.0.1
Google 'hosts file'
The IP address of any server in the same host is 127.0.0.1 unless the server has bound to a specific, different IP address. As #MartinJames points out, you can use 'localhost' as the hostname for that, except on certain broken Linux distributions.
I'm using autossh and set it up like shown in the following example: http://surniaulula.com/2012/12/10/autossh-startup-script-for-multiple-tunnels/
I found it works really well, but need some clarification. Particularly, the difference between 127.0.0.1 and * in the following examples:
ForwardPort=(
"L 127.0.0.1:3397:127.0.0.1:3306"
)
versus
ForwardPort=(
"L *:3397:127.0.0.1:3306"
)
The first one seems to do the redirect if coming from the host machine itself, where the 2nd seems to forward from anywhere. To me this translated as if the * was for any IP to forward the traffic through, but someone told me that it just says for any adapter on the machine. I'm curious is it any IP, or any adapter? I assume the end result is the same, but would like clarification for my own understanding.
Update
I updated my test to include a specific IP of a network I'm using and then tried to connect to the port and found that it did not work with the specific port specified, e.g.
ForwardPort=(
"L x.x.x.x:3397:127.0.0.1:3306"
"L 127.0.0.1:3397:127.0.0.1:3306"
)
Since this did not work, it makes me believe my buddy was right about the adapters. I am thus seeking details of how the adapter stuff works.
Figured out my answer, figured I would post my answer, in case anyone is interested or googles an answer like I did and not find it.
So when I did x.x.x.x this was the WAN IP of my remote server. The autossh has not clue of this as the IP is not one of its adapters. If you run ifconfig on the server then you'll get your local loopback of 127.0.0.1 and other adapters like eth0, which would have the WAN IP. By doing the *, it does the forwarding for any requests comes from the local loop back as well as from the WAN. So * routes all requests, where 127.0.0.1 only does the ones that come from the machine itself.
If you wanted to allow external forwards, but limit who could do it, you could achieve this by doing * and then limiting communication of the server via iptables.
I'm attempting to refactor old perl code to support some new IPV6-only hosts.
In testing the basic 'connect.pl' script, it fails with
Unsupported IP address format:
#/usr/lib/vmware-vcli/apps/general/connect.pl --url https://fe80::b6b5:2fff:fe5a:c5d8:443/sdk --username root --password pw
Unsupported IP address format
I also wrote a little test script to exercise Util::connect, and it fails in the same way.
Caveats:
I'm using VMware-vSphere-Perl-SDK-5.1.0-780721.
In my scenario, the ESXi hosts run IPV6 only, and the guests will run IPV4 only.
You have two issues here.
IPv6 literal addresses in a URL need to be in brackets.
Your link-local address is missing an interface identifier.
Specify the link-local address in brackets with the correct interface identifier. For example:
https://[fe80::b6b5:2fff:fe5a:c5d8%eth0]:443/sdk
The correct answer here is that, for unknown reasons, the API checks for link-local "fe80" addresses, and errors if it sees them.
connect.pl (which is sample code provided by VMware) works just fine with a global-scoped IPV6 address.
I'm trying to portably (Windows & Linux) find all of the IP addresses of the local machine. The method I am using is to first call gethostname(), and then pass the result of that to gethostbyname(), which returns an array of ip addresses.
The problem is that on linux, the only address I get back is 127.0.0.1. This works on Windows, and I've seen a few people state that this will not work on Linux if your network was configured by DHCP (don't know if that's a true statement).
Is this not the correct way to do this on Linux?
It is not the correct way on unix/linux. The correct way involves ioctls to pull the necessary information.
struct ifreq ifc_buffer[MAX_NUM_IFREQ];
ioctl(s, SIOCGIFCONF, &ifc) # Interface list
num_ifreq = ifc.ifc_len / sizeof(struct ifreq);
for(cnt=0;cnt<num_ifreq;cnt++)
struct ifreq *ifr = &ifc.ifc_req[cnt]
ioctl(s, SIOCGIFADDR, ifr); # get ip address
There are also more modern methods involving:
if_nameindex()
Doing a SO search for if_nameindex and SIOCGIFCONF will yield a number of questions similar to this one.
This happens because on most distributions you have this in /etc/hosts:
127.0.0.1 localhost.localdomain localhost aiur
gethostbyname simply resolves the hostname (aiure in this example) to an address. If it finds it in /etc/hosts it's more than happy to give you that.
Back to the question. Unfortunately I don't believe you can get all the addresses of your machine in a portable way. You can do it in a Unix-portable way, like ifconfig does. Open a socket s and do an ioctl(..., SIOCGIFCONF, ...).
By the way, gethostbyname is obsolete if you believe kernel.org and deprecated if you believe MSDN.
Just curious. When developing with Casini development server, one has an infinite number of ports. But, the production servers seem to give a particular importance to port 80.
Has that to do with a technical requirement, a convention, or both? I've checked the web but haven't been able to find a clear response so far.
Thanks for helping.
Many services have specifically-assigned ports This allows users to type, for example http://stackoverflow.com and get the website for SO, without needing to enter a port as well. This isn't a technical requirement; however, using a different port requires the user to know an extra piece of information, which must be entered into the URL every time.
When you connect to a server via TCP/IP you specify particular port you connect to. You do not connect to a server and hope that server guesses which port you would like to talk to.
So in most cases you tell browser to use protocol http, say "http://example.com/" then browser uses default port number assigned to that protocol (http) to connect to server "example.com". In this case port is 80. If for example you specify "https://example.com/" then browser looks for default port for https and then connects to port 443 instead.
So if you do not want to tell to every of your users to specify some non-default port for your service (say "http://example.com:60765/") you better use default one.
BTW there is a way to get port number your service listens to by it's protocol name (by asking a service's host's daemon at port 0) but this method seems to be rarely used (if at all).
See also other answers: default protocol numbers are assigned by IANA
It's a convention: you can use whatever port you feel like. You can look at the evolution of RFCs to see when the convention was official (http://www.faqs.org/rfcs/rfc1700.html)
You can see in the RFC 1060 (http://www.faqs.org/rfcs/rfc1060.html ) that it's the ISO Internet Protocol :)
In a production environment your web server is embedded in a server infrastructure (firewalls, proxies) protecting you against attacks from the internet. In such an environment port 80 is normally open for HTTP traffic. If you use this port there is no need to configure your server infrastructure.