Computer Programming for Absolute Beginners
上QQ阅读APP看书,第一时间看更新

Client-server applications

Client-server is a model we can use to create distributed applications, which are applications that run on more than one machine.

The idea behind the client-server model is that we have at least two computers involved. One acts as the server, and all the others have the role of the client. Clients and servers need to communicate with each other. It is always the client who initiates the communication. Sometimes the server communicates with several clients at once; other times, the server only communicates with a single client at a time.

This means that we can use different computers to take care of different parts of an application's responsibility. We can let one computer deal with one aspect of a problem and another computer work on a different aspect of the same problem. These two computers then need to communicate their results, usually to a single computer, which can then assemble the different results into one solution.

We can also use this model when we have different roles for different parts of an application. For example, we have one role that is to display data to and get input from a user (user interaction) and another role that is to process and store this data. We can pide these roles so the processing and storing role is done by one computer and the user interaction role by another computer.

Figure 3.1 – A server connected to several clients

To illustrate this, let's take a couple of scenarios where we would use a client-server solution and see what the solutions would be.

Example of a chat application

Let's assume that you want to create an application where you and your friends can chat with each other. Everyone that will use this chat application will need the client software; this is the program we start when we want to chat.

When we start thinking about how to design this application, we will face our first problem. Imagine that you start your chat application because you want to chat with your friend Alice. Our application needs to connect to Alice's computer, running her version of our program. Both you and Alice will run identical programs, but how can they connect? How can our application find Alice's computer among all the computers connected to the internet? It would be like if you want to call Alice but don't have her phone number. Our chat application will be our phone, and Alice's client will be her phone. You can't just randomly enter a number in the hope of reaching Alice.

Figure 3.2 – How can you find Alice's computer when you want to chat?

An IP address (IP is an abbreviation for Internet Protocol and is part of a larger protocol stack, called TCP/IP, that describes how computers communicate over the internet) identifies all computers and other devices connected to the internet. We can think of this address as a phone number. This number can uniquely identify a telephone anywhere in the world. The same is true for an IP address; it can uniquely identify any device that is connected to the internet.

The problem is, how can we know what address Alice's computer has? And even if we knew what it was, we must understand that it is subject to change. If she is connected to her home Wi-Fi network, she will have one IP address, but if she takes her computer to a café downtown and connects to their Wi-Fi network, she will get another IP address. This is because when connecting to a Wi-Fi network, it is the network router that assigns an IP address to your computer.

A better solution would be if all clients connected to a computer that always has the same address. This would be our server. If both you and Alice connect to the same server, this server will know about both your addresses, and when you want to chat with Alice, you send a message to the server and the server will relay this message to Alice. When her chat client receives the message, it will make a sound so she knows a new message has arrived, and the problem is solved. To simplify things even more, we will use a domain name for the server instead of the IP address, as a domain name such as company.com is easier to remember than an IP address.

Figure 3.3 – Using a server to handle the communication between you and Alice

If more than two users are connected to the server, then the server will need to keep track of who is the recipient of the message. When you send your message to Alice, your client application will need to provide the identity of who should get the message so the server can make sure it is sent to the right client.

Example of an email client

Assume that you have been using several different applications to read and write emails, but you are not happy with how they work and you decide to write your own. What you will write is an email client.

Let's take our friend Alice again. What happens if she sends you an email? Your emails must be stored somewhere as you can't have your client application running all the time. The email Alice sends to you will end up on an email server. When you start the email client you wrote, it will connect to the server and ask for all new emails that have been received since the last time you connected. These will now be transferred to your client application.

Figure 3.4 – An email server will handle incoming and outgoing emails, and the client only connects to receive and transmit messages

Client-server, a two-part solution

In both these examples, we saw that the solution to a problem is pided into two parts. We need one part that will be the client, and the other will be the server. The characteristics of these two are that we have a server with its location known by its IP address, and we will have a client that will know about the server address and will be the part that initiates the communication. An IP address can also be in the form of a domain name, such as http://some-server.com. A domain name is a one-to-one mapping between an IP address and a name. In other words, a domain name is tied to one single IP address and is used because it is easier to remember a domain name than an IP address that is just four numbers in the form 123.123.123.123.

This format is true for the version of IP addresses called IPv4 (Internet Protocol version 4.) A new version of the Internet Protocol is now starting to get widespread. The main reason for the upgrade is that we are running out of possible combinations to give all devices unique addresses. The new version is called IPv6 (which is Internet Protocol version 6) and an address in IPv6 will look like 2001:db8:a0b:12f0::1. These numbers are separated by colon instead of a period. In IPv4 the address was represented as a 32-bit value, and in IPv6 it is 128 bits. This means that we have many more addresses to distribute.

Sometimes these two roles are only distinct for how the two parts will connect; the client connects to the server, and when the connection is made, they can act as two identical parts. If we take the chat application as an example, if we knew Alice's address, we could connect directly to her application. Our application will initially be the client, and Alice's application would act as the server. But as soon as we have a connection, both parts will act in the same way, and the roles of who is the client and who is the server will be unimportant.

Next, we move to understanding web applications.