Notes on networking
A high level introduction on network basics
Just like humans have to communicate to each other, computers have to communicate between them to create more powerfull applications.
Applications such as email, web and instant messaging use different network protocols, but they all rely on the same general network transport methods.
OSI Model
Two computers have to speak the same language to comunicate. This language is described in layers by the OSI model. They are conceptual layers of communication that allow hardware such as routers and firewalls to focus on a particular aspect of the communcation, and ignore the higher level of data encapsulation used by applications. There are 7 layers, from lowest to highest:
-
Physical Layer: physical connection between two points, passing raw bit streams
-
Data-link Layer: provides high level functions as error correction and flow control, deals with actually passing the data
-
Network Layer: act as a middle ground. It provides addressing (like IP address) and routing, connects the lower and higher layers
-
Transport Layer: provides reliable data communication, so higher systems don’t have to worry about reliability and cost efficiency
-
Session Layer: establish and mantain connections between network applications
-
Presentation Layer: present the data to the applicaton in a syntax/language they can understand. This allows encryption and compression
-
Application Layer: keeps track of the requirements of the application
Data is sent through these layers in packets (small pieces), where each packet contains implementations of these layers.
Starting from the application layer, the packet wraps the presentation layer, which wraps the session layer and so on so forth. This is called encapsulation, and each wrapped part has a header and a body.
The header contains information about that layer and the body contains the data for that layer. The body of one layer has the entire package of the previous encapsulated layer, like an onion.
Using the web as example, the physical layer is the ethernet cable and cards, transmiting raw bits to one cable to another.
The data-link layer is the ethernet itself, which allows communication between ethernet ports on the LAN (local network area).
The network layer provides IP addresses and is also responsible for moving data from one address to another. Other addressing schemes exists in this layer, but the web traffic usually uses IPv4 (eg. XX.XX.XX.XX).
physical layer, data-link and network together are able to send packets of data from one IP to another
The transport layer is where TCP lives: a bidirectional socket connection. The term TCP/IP describes the use of TCP on transport layer and IP on network layer.
The top of OSI model for web is the HTTP.
When we communicate from a web browser to a web server, our data packets are encapsulated from application layer to physical layer. Then they are passed to a router, which is not concern on what’s inside the packets, he just want to transfer the packets. So it only implements protocols up to the network layer. Then, the packets reach the other network router, the final web server destination. This destination router then encapsulates the packets for the final destination on the webserver.
These protocols are programmed into routers, firewall and the OS. Programs that uses networking such as email clients and browsers use the interface of the OS to handle network communication. The OS already takes care of encapsulation details, so writing network programs is just a matter of using the OS interfaces.
Sockets
Is a standard way to perform network communication through a OS. Sockets can be thought as anh abstraction to all the details of OSI model. It can be used to send or receive data over a network. This data is transmitted over the Session Layer, above the lower level layers (which are handled by the OS)
There are many different types of sockets, which determines the structure of the transport layer. the most common are stream sockets and datagram sockets.
Stream sockets provides a reliable two way connection between sides, just like a phone conversation. Once the connection is established, either sides can communicate. Stream sockets uses TCP, which exists on the transport layer. When data is sended as chuncks called packets, TCP garantees the packets reach the destination in order and without errors.
Datagram sockets is more like mailing. Is a one way communication and is unreliable, you don’t know if the message is gonna reach the destination. Datagram sockets uses another transport layer protocol called UDP. This protocol is very lightweight and basic, it’s just a basic method to send data from one point to another. If you want confirmation that your packet was received, the other end must be coded to send a response back. Datagram sockets are mostly used in network games and streaming media.