The Basics
6
If the client application is provided with a DNS name before it can communicate with
the server application, it must resolve the DNS name to obtain the actual IP address of the
host running the server application. Sometimes, the DNS name may map to multiple IP
addresses, in which case the client may want to try addresses one by one until it nds the
one that works. We'll consider a recipe describing how to resolve DNS names with Boost.Asio
later in this chapter.
The server application needs to deal with endpoints too. It uses the endpoint to specify to the
operating system on which the IP address and protocol port it wants to listen for incoming
messages from the clients. If the host running the server application has only one network
interface and a single IP address assigned to it, the server application has only one option
as to on which address to listen. However, sometimes the host might have more than one
network interface and correspondingly more than one IP address. In this situation, the server
application encounters a difcult problem of selecting an appropriate IP address on which to
listen for incoming messages. The problem is that the application knows nothing about details
such as underlying IP protocol settings, packet routing rules, DNS names which are mapped
to the corresponding IP addresses, and so on. Therefore, it is quite a complex task (and
sometimes even not solvable) for the server application to foresee through which IP
address the messages sent by clients will be delivered to the host.
If the server application chooses only one IP address to listen for incoming messages, it may
miss messages routed to other IP addresses of the host. Therefore, the server application
usually wants to listen on all IP addresses available on the host. This guarantees that the
server application will receive all messages arriving at any IP address and the particular
protocol port.
To sum up, the endpoints serve two goals:
f The client application uses an endpoint to designate a particular server application it
wants to communicate with.
f The server application uses an endpoint to specify a local IP address and a port
number on which it wants to receive incoming messages from clients. If there is more
than one IP address on the host, the server application will want to create a special
endpoint representing all IP addresses at once.
This recipe explains how to create endpoints in Boost.Asio both in client and
server applications.
Getting ready
Before creating the endpoint, the client application must obtain the raw IP address and the
protocol port number designating the server it will communicate with. The server application
on the other hand, as it usually listens for incoming messages on all IP addresses, only needs
to obtain a port number on which to listen.