Before we start coding a network application, it is better for us to understand how a network works. In this chapter, we will dig up network concepts with their contents. The topics that we'll cover in this chapter are as follows:
Network architecture is structured with layers and protocols. Each layer in the architecture has its own role, while its main purpose is to offer a certain service to the higher layer and communicate with the adjoining layers. However, a protocol is a collection of rules and conventions that are used by all the communicating parties to standardize the communication process. For instance, when the layer n in a device communicates with another layer n in another device, for the communication to take place, they have to use the same protocol.
There are two popular network architectures that are used nowadays: the Open Systems Interconnection (OSI) and TCP/IP reference models. We will dig deeper to understand each reference model with its advantages and disadvantages so that we can decide which model should be used in our network application.
The OSI model is used to connect to the open systems—these are the systems that are open and communicate with other systems. By using this model, we do not depend on an operating system anymore, so we are allowed to communicate with any operating system on any computer. This model contains seven layers, where each layer has a specific function and defines the way data is handled on certain different layers. The seven layers that are contained in this model are the Physical layer, Data Link layer, Network layer, Transport layer, Session layer, Presentation layer, and the Application layer.
This is the first layer in the OSI model and contains a definition of the network's physical specification, including the physical media (cables and connectors) and basic devices (repeaters and hubs). The layer is responsible for the input raw bits transmission data stream into zeros and for the ones that are on the communication channel. It then places the data onto the physical media. It is concerned with data transmission integrity and makes sure that the bits that are sent from one device are exactly the same as the data that is received by the other device.
The main role of the Data Link layer is to provide a link for raw data transmission. Before the data is transmitted, it is broken up into data frames, and the Data Link layer transmits them consecutively. The receiver will send back an acknowledge frame for each frame that has been sent if the service is reliable.
This layer consists of two sublayers: Logical Link Control (LLC) and Media Access Control (MAC). The LLC sublayer is responsible for transmission error checking and deals with frame transmission, while the MAC sublayer defines how to retrieve data from the physical media or store data in the physical media.
We can also find the MAC address, also called as the physical address, in this layer. The MAC address is used to identify every device that connects to the network because it is unique for each device. Using Command Prompt, we can obtain the address by typing the following command in the console window:
ipconfig /all
We will get the console output, as follows, after ignoring all other information except Windows IP Configuration and Wireless LAN adapter Wi-Fi. We can find the MAC address in the Physical Address section, which is 80-19-34-CB-BF-FB for my own environment. You will get a different result since the MAC address is unique for every device:
Windows IP Configuration Host Name . . . . . . . . . . . . : HOST1 Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Hybrid IP Routing Enabled. . . . . . . . : No WINS Proxy Enabled. . . . . . . . : No Wireless LAN adapter Wi-Fi: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Intel(R) Wireless-N 7260 Physical Address. . . . . . . . . : 80-19-34-CB-BF-FB DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes Link-local IPv6 Address . . . . . : fe80::f14e:d5e6:aa0a:5855%3 (Preferred) IPv4 Address. . . . . . . . . . . : 192.168.1.4(Preferred) Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : 192.168.1.254 DHCP Server . . . . . . . . . . . : 192.168.1.254 DHCPv6 IAID . . . . . . . . . . . : 58726708 DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1C-89-E6-3E-68-F7- 28-1E-61-66 DNS Servers . . . . . . . . . . . : 192.168.1.254 NetBIOS over Tcpip. . . . . . . . : Enabled
The MAC address contains twelve hexadecimal characters, where two digits are paired with each other. The first six digits represent the organizationally unique identifier and the remaining digits represent the manufacturer serial number. If you are really curious to know what this number means, you can go to www.macvendorlookup.com and fill the text box with our MAC address to know more about it. In my own system, I got Intel Corporate as the vendor company name, which is the same as the brand of my installed network card.
The Network layer is responsible for defining the best way to route the packets from a source to the destination device. It will generate routing tables using Internet Protocol (IP) as the routing protocol, and the IP address is used to make sure that the data gets its route to the required destination. There are two versions of IP nowadays: IPv4 and IPv6. In IPv4, we use 32-bit addresses to address the protocol and we use 128-bit addresses in IPv6. You are going to learn more about Internet Protocol, IPv4, and IPv6 in the next topic.
The Transport layer is responsible for transferring data from a source to destination. It will split up the data into smaller parts, or in this case segments, and then will join all the segments to restore the data to its initial form in the destination.
There are two main protocols that work in this layer: the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP). TCP supplies the delivery of data by establishing a session. The data will not be transmitted until a session is established. TCP is also known as the connection-oriented protocol, which means that the session has to be established before transmitting the data. UDP is a method of delivering data with the best efforts, but does not give a guaranteed delivery because it does not establish a session. Therefore, UDP is also known as the connection-less protocol. In-depth explanation about TCP and UDP can be found in the next topic.
The Session layer is responsible for the establishment, maintenance, and termination of the session. We can analogize the session like a connection between two devices on the network. For example, if we want to send a file from a computer to another, this layer will establish the connection first before the file can be sent. This layer will then make sure that the connection is still up until the file is sent completely. Finally, this layer will terminate the connection if it is no longer needed. The connection we talk about is the session.
This layer also makes sure that the data from a different application is not interchanged. For example, if we run the Internet browser, chat application, and download manager at the same time, this layer will be responsible for establishing the session for every single application and ensure that they remain separated from other applications.
There are three communication methods that are used by this layer: the simplex, half-duplex, or full-duplex method. In the simplex method, data can only be transferred by one party, so the other cannot transfer any data. This method is no longer common in use, since we need applications that can interact with each other. In the half-duplex method, any data can be transferred to all the involved devices, but only one device can transfer the data in the time, after it completes the sending process. Then, the others can also send and transfer data. The full-duplex method can transfer data to all the devices at the same time. To send and receive data, this method uses different paths.
The Presentation layer role is used to determine the data that has been sent, to translate the data into the appropriate format, and then to present it. For example, we send an MP3 file over the network and the file is split up into several segments. Then, using the header information on the segment, this layer will construct the file by translating the segments.
Moreover, this layer is responsible for data compression and decompression because all the data transmitted over the Internet is compressed to save the bandwidth. This layer is also responsible for data encryption and decryption in order to secure communication between two devices.
The Application layer deals with the computer application that is used by a user. Only the application that connects to a network will connect to this layer. This layer contains several protocols that are needed by a user, which are as follows:
This layer also decides whether enough network resources are available for network access. For instance, if you want to surf the Internet using an Internet browser, the Application layer decides whether access to the Internet is available using HTTP.
Let's see the following figure to see which all protocols are included in the OSI layer:
We can divide all the seven layers into two section layers: the Upper Layer and Lower Layer. The upper layer is responsible for interacting with the user and is less concerned about the low-level details, whereas the lower layer is responsible for transferring data over the network, such as formatting and encoding.
The format of data traveling is different for each layer. There are bits for the Physical layer, frame for the Data Link layer, and so on.
The TCP/IP model was created before the OSI model. This model works in a similar way to the OSI model, except that it just contains four layers. Each layer on the TCP/IP model corresponds to the layers of the OSI model. The TCP/IP Application layer maps the 5, 6, and 7 layers of the OSI model. The TCP/IP Transport layer maps the layer 4 of the OSI model. The TCP/IP Internet layer maps the layer 3 of the OSI model. The TCP/IP Link layer maps the layers 1 and 2 of the OSI model. Let's see the following figure for further details:
These are the main roles of each layer in the TCP/IP model:
Here are the protocols and devices that are involved in the TCP/IP model:
Layer |
Protocol |
Device |
---|---|---|
Application |
HTTP, HTTPS, SMTP, POP3, and DNS |
Proxy Server and Firewall |
Transport |
TCP and UDP |
- |
Internet |
IP and ICMP |
Router |
Link |
Ethernet, Token Ring, and Frame Relay |
Hub, Modem, and Repeater |