There are many different concepts and modes of control over computer networks. This also applies to the Internet of Things. I won't present all the concepts here. I'll just give you some theory here. In the following chapters and through various examples, I will demonstrate how these concepts can be applied in practice.
The base of all network programming are sockets. A socket is a mechanism that allows communication between different applications. More precisely, the sockets allow communication between two different processes on the same or distant machines.
There are four types of socket but only two of them are commonly used.
1. Stream Sockets
These sockets use TCP (Transmission Control Protocol) for data transmission. If you send through the TCP socket three data packages "A, B, C", they will be delivered in exactly the same order - "A, B, C". If delivery is impossible, the sender receives an error message.
2. Datagram Sockets
These sockets use UDP (User Datagram Protocol) for data transmission. This mode does not confirm whether the data has been delivered - the delivery is not guaranteed, and neither does the order of the delivered data packets. Nevertheless, the advantage is that the use of this socket is very simple and the data transfer is very fast. All we need to do is build a data package and attach relevant destination information. The package is ready to send it.
From a programmer's point of view, a socket is the interface between his application and the transport layer for communication with the target node on the network. The programmer only needs to know how to implement such a socket in the code of his application and what the API is for using the socket's library functions.
Most applications that use HTTP (Hypertext Transfer Protocol) and FTP (File Transfer Protocol) use sockets to establish a connection between the client and server and then to exchange data. Of course, the first TCP-based mode is most often used in computer control.
Servers and Clients
A server is a process that performs some functions on request from a client.
A client is a process that sends a request to a server that the server perform a desired task.
When talking about network communication, one should not forget about hardware also.
A network host is a computer or other device connected to a computer network that has an IP address.
In computer networks every host must have an IP address. An Internet Protocol address (IP address) is an unique number assigned to each device connected to a computer network. The IP (32 bit number) allows to identyficate a host and data exchange with it.
However, the IP address alone is not sufficient for network communication because multiple applications can run on the same host. Each of them can send or wait for data. Therefore, we also use sub-addresses (called "ports") in addition to the IP address. Each application is assigned a unique port value. Thus, the use of ports allows multiple applications to be run simultaneously on a single host. The 16-bit port number can be from 0 to 65535.
In other words, in order to request a specific service from the server, we have to specify the socket address as: IP address + Port number
Now we are fully ready to understand the diagrams below.
The diagram presents the simple example of a socket connection between two machines in a computer network.
As you can see, the connection is based on TCP and IP protocols via relevant sockets (TCP = Transmission Control Protocol, IP = Internet Protocol). Each of these machines (server and client) can have any operating system implemented.
This software architecture is based on the commonly used four-layered model of the TCP/IP protocol stacks.
It is worth mentioning that today the TCP/IP and OSI models are the two most widely used networking standards for communication.
In general, we can summarize that the TCP/IP is a combination of two separate protocols that work together to provide connections over the Internet. The IP addressing scheme is responsible for proper routing the datagrams (basic units of transfered data). The TCP is responsible for higher-level transport services and it includes the Hypertext Transfer Protocol (HTTP). There are many descriptions of the different methods of HTTP. For example, look at this elaboration.
Network control of devices that have public IP addresses.
"Client - Server" architecture
We can build control systems for various devices basing on the above presented model.
Imagine that we have a device directly linked to the server - that machine is both a network server and a controller of our device. The server must be equipped with a suitable application that listens on a proper port to commands from client machines.
The client should be equipped with an appropriate application for communication with the server - it can be a web browser. The controller may be a web server that is able to respond to incoming HTTP commands. This architecture is successfully implemented in such popular systems as ARDUINO and Raspberry Pi. The such implementations are powerful supported by Node-Red software that is the dedicated tool for wiring together hardware devices in Internet of Things.
If we want to control such devices over the Internet, their controllers must be available from outside in this network - they should have public IP addresses.
This is often a big disadvantage of such the solution.
Network control of devices that do not have public IP addresses.
"Client - Server - Client" architecture
We have billions of devices in the internet. Many of them are commercially available at very affordable prices. These devices can be installed everywhere where there is access to the Internet - wired or wireless (e.g. WiFi). For example, there are very many kinds of popular WiFi switches that turn electrical devices on/off from anywhere in the world.
Among other things, they are so willingly used because they do not require public IP addresses.
Today we have a great many solutions for device control over the internet where device controllers do not need public IP addresses.
Below, the diagram displays the such concept - both the controlled device and the device with the control application do not need to have public IP addresses.
As you can see the device controller and "remote control" are implemented as client applications. The applications exchange data via a server. Only this server must have a public IP address.
The control client applications may or may not be permanently stored on the client computers.
These applications can also be loaded from any servers (e.g. WWW) and run in web browsers implemented on client machines. Notice, that the server for loading the control application can be a different server than the one used for data exchange.
In my gadgets you can find a practical example of such a network communication system between two applications on hosts without public IP addresses. Run "Virtual Fan 02 - network controlled" to test this technique..
Both presented architectures are consistent with the original model of using the Internet, because the network was designed from the very beginning in the "Client-Server" model. In this model, the client must always be the originator of the service request.
Today there are many servers specialized for data exchange between devices connected to the Internet. Also new communication models were designed and new advanced protocols have been developed for this purpose, for example MQTT (Message Queue Telemetry Transport).
Of course, the communication based on the traditional "client- server" model is still used.
The such model allows to build very simple systems based on the simplest web servers . It is enough to use a HTTP server that support scripting developed in PHP language (Hypertext Preprocessor) or other scripting languages.
Such simple solutions will be used in my implementations presented here.
Of course, when developing web applications it should be remembered that usually only authorized users can have access to the controlled devices. It is often rational to divide these users into groups since then each group may have individual rights to control devices. Therefore, it is often convenient to implement a relational database on the server machine. The very popular solution is the use of the world's most popular free MySQL database. For a small number of users of a controlled device we can use flat-file database stored in one or several files.