Last Updated:

Network protocols | List | Services and Sockets

A computer is a tool that you need to be able to use. It's no secret that modern commercial programming differs from theoretical programming, and you will not surprise anyone with the "Hello, world!" application - there are needs of a different kind and of a different level. In the newly formed rubric (or series of articles) write not only students, but also just people who want to learn how to really program. Some of them want to implement some plans, the other - to acquire an additional profession. Begin...

 
Quite a lot of questions are asked on networks, protocols... Of course, they are often embarrassed to show ignorance and ask. And everyone can read the educational program :). We will try to explain everything at the most understandable level. Of course, n-e number of useful and voluminous books have been written on the networks, but our material for many can be just a good entry point into the topic. We do not claim the laurels of a "complete detailed guide."
 
Also, we will not engage in attempts to embrace the immensity, but turn to the letter of Andrei from Minsk, who complained that he can program local applications easily, but when it comes to "the Internet"... :) We will not cite the letter itself, I will only note that networks are an additional element in modern programming, nothing more. That is, working with them is not particularly difficult.
 
Initially, it is worth saying that protocols are a sea, but at the same time there are standard interconnected chains, and choosing, for example, the HTTP option (FTP, Gopher, etc.) - TCP-IP, we do not mean so many technologies.
 
It's just that very often beginners are deterred by the number of abbreviations.
 
 

A real-life example

 
 
Let's relax a bit and describe an ordinary situation in the form of a scene.
Imagine the office of a company engaged, for example, in the sale of construction equipment. It has its own mini-PBX, which unites the company's employees, while there are also common (landline) numbers that are called from outside.
 
You are the customer, looking for a concrete mixer. Communication goes like this:
 
Dial the number. The connection is successful.
...
Dispatcher: "Firma "All for the Planet". Hello!"
You: "Hello, I need to get information on concrete mixers."
Dispatcher: "I'm going to switch now..." Switches to internal phone 249.
...
Department Secretary: "Hello. Sales department of construction equipment. I'm listening to you."
You: "Hello. I need to get a technical specification and prices for such and such a model of concrete mixer. "
Department Secretary: "Are you from a firm or a private person?"
You: "I'm a representative of Firm N."
Department Secretary: "Wait a minute... I switch."
...
Manager Nikolay: "Hello, how can I help?".
You: "I need to get a technical specification and prices for such and such a model of concrete mixer."
Manager Nikolay: "Okay, are you more comfortable by e-mail or fax?"
 
And what's an example here? Read the following:
  1. Enter ip... (in our example, this is a landline phone number). The IP address delivers the packet to a specific computer.
  2. Enter the port... (in our example, switching to the internal number of the department). The port number determines which program the incoming packet is intended for. If the company did not have a construction equipment department in our scene, then you would turn to what is called "wrong address".
  3. Enter a name... password... (in our example, the question is who you are).
  4. Checking access rights, granting access to a certain folder / service (in our example, to the manager).
  5. A request to retrieve data.
  6. Data exchange.
Now, everything becomes more or less clear. The data transfer itself, including the technical support of communication, is carried out by hardware, at least by phone, and then you may need e-mail or fax.
 
In this example, without any diagrams and graphs, you can derive the rule:
If you need something from someone, then you are a client, if someone needs something from you, then you are a server. 
 
The task of the client's organization is to obtain information according to a certain protocol (in the scene you spoke Russian language, used the words "Hello", correctly answered questions, etc.). In computer science, a protocol is a given set of rules for interaction.
 
The task of organizing the server is to provide and update current data, track and manage access rights, ensure reliable responses and adequate actions to requests.
 
Now let's move on to the technical details.
 
 

Seven or four? Seven at first!

 
 
Initially, the theory (as, in fact, practice) of networks and network protocols was based on the OSI (Open Systems Interconnection) model, developed by the well-known International Organization for ISO Standards. What are ISO models as such? In any field, these are descriptions of technological standards, on the basis of which (or under which) certain technologies are implemented. ISO models are reference and advisory in nature. As for specifically network interactions, within the OSI we have seven layers, from hardware (physical) to applied (services).
 
Enumeration (OSI):
  1. Physical layer. Physical layer (cables, network card).
  2. Data-link layer. Channel layer (data transfer between any nodes in networks).
  3. Network layer. The network layer (packet delivery to any host on the networks).
  4. Transport layer. Transport layer (means for establishing a connection, buffering, numbering and ordering packets).
  5. Session layer. Session level (control of conversation between nodes).
  6. Presentation layer. The presentation layer (data transformation, such as compression or encryption).
  7. Application layer. Application layer (services).
 
Network protocols list
TCP/IP architecture, which you can find in Visual Studio Help (for Windows CE)
 
Initially, this may seem difficult to beginners. In fact, this is not the case, if you disassemble everything by bones and... at least know the direction of movement by points. So, when transferring data using HTTP, FTP or E-mail services, you start from the seventh level and gradually move to the first. The other machine, having received the package, starts from the physical layer and moves to the seventh. Pay attention to the word "services" in the 7th paragraph, this is the level of communication of the application ->service. After all, in essence, you can add an eighth point - user-> application. It is not explicitly mentioned, but it must be understood.
So, the basic rule:
  • Data transfer: 7 -> 1.
  • Data acquisition: 1-> 7.
In this case, we do not use the concepts of low and high levels, so that you do not get confused, although in documentation and educational materials based on OSI, high often means physical, and low - applied (the matter is in the numbering of the list). And you can get confused simply, because in programming the hardware level is always considered low. It is correct by the upper level to mean the software part (in our case 5-7).
 
All in all, seven points. How is this implemented technically? Let's say we transfer (!) data. At the application layer, a header is added to the package (!), then the transition to the presentation layer is carried out, where its own title is again added to it. At the session level, the packet receives another header, and so on, until everything reaches the physical layer.
When receiving data, there is a reverse unwinding: we start bypassing from the physical level, the corresponding header is removed (!) on the channel(!), then identical actions until all the headers are removed, and the user receives a clean packet without service information.
 
In fact, data transmission can begin not from the seventh, but from the fourth level, etc., it all depends on the conditions and the protocol used.
 
The first three items of the list are implemented at the hardware level (network cards, routers, hubs, bridges, etc.), the fourth is intermediate, and 5-7 are implemented within operating systems and applications.
 
 

About packages...

 
 
The package contains information about yourself and the data you want to transfer. When passing the chain 7-> 1 (transmission), it acquires service data, and when 1->7 (receipt), this data is deleted by those to whom it is intended.
 
 

Case study of the seven OSI points

 
 
Let's present a model based on a specific example, and for a closer comparison with the OSI structure (correspondence with seven points), let's take a variant of NetBIOS at the application layer. It's actually not that important, because we need to understand the basic structure.
All abbreviations will be deciphered. So, our seven points.
  1. Physical layer. Network card. Data packet translation.
  2. Channel level. The network card driver. NDIS. PPP/SLIP.
  3. The network layer. IP, ARP, RARP.
  4. Level of transport. TCP.
  5. The session level. TDI interface.
  6. The presentation layer. TCP/IP-based NetBIOS.
  7. Application layer. NetBIOS.
Now, in order. With hardware levels, structurally everything is more or less clear, but since we decided to decipher everything, it is worth stopping at the second point.
 
Item 2. To transmit packets, you need to know the hardware address of the recipient, or as it is commonly called the MAC address. It is unique and is stitched into a network device at the factory. That is, in other words, the MAC addresses of the devices are used as addresses at the link level. To make it quite clear, we will draw a parallel with mobile communications. You have your own number, but each phone has a separate number given by the manufacturer. Something like that.
 
NDIS (Network Driver Interface Specification) - support for data transfer at the hardware level. And we are talking about all possible network interactions. For example, when we say "networks", we now mean the Internet more, but communications between computers can be different, using different technologies and devices (Ethernet, IR, serial port). This is quite a complex topic for an introductory article. Details you can find out later.
 
Point-to-point protocol (PPP) is a communication protocol for connecting a computer to a network. It's about specific physical connectivity support, and as the most striking example, you use PPP to connect to the Internet. PPP implies its own family of intermediate protocols, thanks to which the connection takes place. In older literature, you can find the SLIP protocol as an alternative to PPP.
 
Item 3. IP (Internet Protocol) is located at the network layer and is responsible for data transmission. Over ip, packets are simply sent to the network without waiting for acknowledgement of data (ACC Acknowledgment). In turn, the data packet includes the sender and receiver addresses, the protocol identifier, the TTL (packet lifetime), and the checksum to verify the integrity of the packet. There is one "but" that many may have already noticed. The sender cannot trace not only the integrity of the packets (only the recipient can know this), but also the guarantee of packet delivery itself. This problem is solved by other protocols.
At this level, the transition from MAC addresses to IP addresses and back is performed. Address Resolution Protocol (ARP) is designed to determine the hardware (MAC) address of a computer on a network by its IP address. RARP (Revers Address Resolution Protocol) works backwards.
 
An IP address is a unique 32-bit address assigned to each Internet host.
 
Item 4. TCP (Transmission Control Protocol) is one of the main transport protocols. It directly interacts with the IP, or rather provides for its use (or receives from it) TCP packets. It is possible to explain the principle of operation of transport protocols simply. You need to send data, if their portion is large, and does not fit into one packet, then they are divided into several TCP packets. TCP packets themselves do not contain poison/recipient addresses because they are assigned at the IP level. That is, you cannot send a "naked" TCP packet, because it is not known to whom. At the same time, it is TCP that eliminates the shortcomings of IP in the field of integrity checking and delivery guarantee. This transport protocol organizes a certain communication between the sender and the receiver, it works on the "client-server" technology.
 
Usually they write just a bundle of TCP/IP and present it as a single abbreviation, which is largely true.
 
Item 5. The session level implies the management of the dialogue between the nodes, within its framework it is possible to fix the currently active party. TDI (Transport Driver Interface) is, to put it simply, the top level of NDIS. For example, NDIS implies support for performing transport at the lower (hardware) level over a standard protocol such as TCP/IP, while TDI is responsible for the higher layer. Here you can get confused, however, and the information at the moment for you will not be particularly relevant.
 
Paragraphs 6 and 7. NetBIOS (Network Basic Input Output System) is a set of API functions for working with the network. It works only at the software level and is responsible for generating packets to send data, and in most cases they can be physically transmitted either over TCP/IP or OVER IPX/SPX (other standards for Novell networks).
 
 

Seven or four? You can have four...

 
 
Microsoft is very often scolded, although there are two sides to every coin. For example, I personally like it when reasonable simplifications are made. Remember when we talked about how points 1-3 are hardware, 4 are intermediate, 5-7 are software? So, Microsoft went for simplifications, actually replacing seven points with four, combining everything under ms TCP/IP technology:
  1. Hardware level. Paragraphs 1 and 2 of the OSI.
  2. The internetwork layer. IP.
  3. Level of transport. TCP or UDP.
  4. The application layer. Windows sockets.
 
Comparison of MS TCP/IP and OSI (taken from the book by M. E. Flenov "C++ Programming through the Eyes of a Hacker")
 
In fact, this is a conditional breakdown that you may see in some books. If you refer to Visual Studio Help with the query "TCP/IP Architectural Model", you will get 7 OSI points and the distribution of protocols and relationships between them (there is an example for Windows CE in the first figure). If you look closely, you will find that the simplification arose due to the fact that within Microsoft, one item can combine several OSI.
 
 

What are sockets?

 
 
Sockets are communication channels between computers that transmit data in both directions and are implemented at the software level. That is, you know the IP and port number of the server and you connected to it, but with all this you need to organize a communication channel through which data will be exchanged.
 
You can open multiple sockets at the same time, and nothing limits the maximum number of open sockets.
 
In general, we are talking simply about programming interfaces that make it easier to work with transport protocols at the upper level.
 
 

TCP or UDP?

 
 
To verify guaranteed data delivery, TCP organizes a specific communication between the sender and the receiver (client and server). Data is exchanged thanks to which the receiver knows the number of packets that he must receive, their order, and so on. If the packet is not received, the protocol resends it. In fact, the communication mechanism is quite simple, but it is because of the guaranteed quality of delivery that TCP/IP works rather slowly.
 
UDP (User Datagram Protocol) can be explained using several definitions - (a) it is a lite TCP, (b) it is TCP without quality assurance of delivery, (c) it is a protocol that works without establishing communication, as they say, simply "throwing data into the network". There is no quality control of delivery, if one package is lost on the road, then it disappears. Moreover, figuratively speaking, you send: "Hello, my name is Sergey", and the recipient can take a line in the form: "Hello, Sergey is my name". But UDP is fast. It is often used to provide live communication (chats), broadcasts (Internet radio). After all, in essence, if one package is lost in transit, then you will hear a click in a musical composition, nothing more.
 
 

Services

 
 
Services or service protocols include FTP, HTTP, and so on. They allow you to interact according to certain rules at the level of user interfaces and applications. Services can be based on both TCP and UDP.
In addition, you can write services yourself.
 
 

Programming

 
 
So, within Visual Studio, socket programming, creating client and server programs, the easiest way to start is to use the CSocket class. It is the successor of ThesyncSocket, and when working with it, you use TCP/IP by default.
After that, it makes sense to start programming with the Winsock or Winsock2 library. On this basis you can program anything you want :).
 
There are also libraries afxinet and wininet. For example, as part of service programming, such as writing HTTP clients and servers, FTP clients can use a specific CInternetConnection class.
 
Programming with sockets, the creation of client-server connections, TCP/IP, UDP, the organization of high-volume data transmission, synchronous and asynchronous modes are simply perfectly shown in the book by M. E. Flenov "Programming in C++ through the eyes of a hacker".. As far as I know, there is a similar one for Delphi (the author is fluent in both languages/environments). If it is difficult for you to master these questions (and I have seen some teaching aids :)))) and books), then Flenoff will help you. A little worse everything is described in Visual Studio Help.
 
Games used to use a higher-level model, DirectPlay, instead of sockets. DirectPlay is an add-on to sockets that provides a set of high-level functions (and, it would seem, much higher than sockets?). And although older books talk about this, today the topic of DirectPlay is not relevant.
The test task is given in the appendix to the article.
 
 

(Appendix) Test Task

 
 
As a lab on socket programming, I created the assignment shown in the figure. It's designed so that by doing it, you can say, "I already know a lot! And I understand a lot of things." Let's call this program "Network Chameleon".
 
 
 
Take Winsock2 as a basis. As a desktop guide, it is recommended to use the book by M.E. Flenov "Programming in C++ through the eyes of a hacker", as well as open code from the Internet. True, if you will run everything under Visual C++ of the MSVS 2008 sample, yes, and, in principle, in general, then for the organization of trouble-free work related to connections and data exchange, understand the use of threads. Perhaps, and this is more likely, you will foresee a different organization of processes than Flenov and in some examples of open source.
 
The interface includes buttons 1 and 2 for selecting the application mode (client/ server), next to each of them there are windows 3 and 4 (EditBox), in which you must enter the port number. For client mode, you specify the port number of the server to which you want to connect, for the server mode, the port number on which it will start.
 
We will all test on the same machine (computer), running several identical applications.
 
It is worth saying that ports 0-1023 are reserved for standard services, so their use is not recommended, it is better to choose something in the range 1024 - 65535.
 
Since we will run all applications on one local computer, specify the IP address in the code: "127.0.0.1".
 
The connect button 5 activates the program mode. If it is a server, the message should appear in its log 8 (ListBox). If the client, the message should appear in the log window on the server, which in turn sends a confirmation, which is displayed in the client's log.
 
For messages, there is a text input window 6 (EditBox) and a send button 7.
 
Requirements:
  1. TCP is used as the transport protocol.
  2. You can run multiple servers on your computer with different ports.
  3. A certain number of clients can connect to each server, the server operates asynchronously. If it sends a message, it is received by all clients of the server.
  4. When a server is disconnected, all of its clients shut down (client programs are automatically closed).
  5. If one of the clients sent a message that begins with a command, such as destruct, the server is disconnected.
It must be said that the task will take a lot of time for beginners, but you acquire specific knowledge and experience. And this is the best prize!