Sockets use IP addresses to route messages to specific hots. A host is identified by a unique IP address. DSN maps host names to IP address. When you ask DNS to resolve a host name it responds with an IP address.
IP addresses don’t always refer to remote hosts. Sometimes you want to connect to your local host. Most loopback interfaces are a virtual interface, unlike the interface to your network card. With a loopback address your network is constrained to the local host.
localhost is the name of the loopback interface and the IP is usually 127.0.0.1. This is defined in /etc/hosts.
The combination of IP address and port number must be unique for each socket. You could have a socket with an IPv4 address listening on the same port as a socket with an IPv6 address. You cannot have two sockets with the same IPv4 address listening on the same port number.
The following is an example of the different kinds of sockets you can create. :INET implies and IPv4 address.
In order to create a connection you must have two sockets.
- initiator (client)
- listener (server)
A socket that listens is a server. A socket that initiates a connection is a client.
Another way to write the above is like this:
An alternative way to write this is: