Layers of a network and HTTP
Tags: java, beginners, webdev, tristan
Published: 2022-01-02 18:53:13

#Introduction
- This series will be everything I have learned while building my website with spring boot. If you would like to read more about the HTTP protocol, click [HERE](https://developer.mozilla.org/en-US/docs/Web/HTTP/Session)

###Networks
- What is a network? Well, a network is really just a collection of computers and other devices that can send data to and receive data from one another, more or less in real time. However, before we can dive any deeper into networks we need to define two words:

**1)**`packet-switched-networks`:all modern networks are this type of network and it means all the data is broken down into little chunks called packets and each packet is handled separately. Each packet contains information about who sent it an where it is going.

**2)**`protocol`:  a protocol is a precises set of rules defining how computers communicate: the format of addresses, how data is split into packets and so on. There are many different protocols and the one you and I are probably most familiar with is `HTTP`(more on this later)

###Layers of a network
- A large network(like the internet) can be very complex and to hide our sweet developer eyes from this complexity, a network can be broken down into layers. With each layer representing a different level of abstraction between the physical hardware(yuck) and the information being transmitted(yum).
- There are many different types of network layer models but for this tutorial we are going to discuss the standard `TCP/IP four layer` model.

#### 1) Host-to-Network layer
- This layer represents the physical stuff, like wires and routers. As Java developers we do not need to worry about this layer too much. The only time we would have to worry about this sort of layer is if we are building a protocol that has to take into account the specific latency of the network. However, for this post, we are not concerned with it.

#### 2) Internet layer
- Also called the network layer, this is the first layer that we as developers should really concern ourselves with. This layer is where we can find the `network protocol` and the one that you and I are using right now, is called `IP`(internet protocol). IP sends data in packets called `datagrams`, these datagrams carry all the necessary information to get the datagram to its appropriate destination. The second purpose of the internet layer is to let different types of Host-to-Network layers talk to each other (routers and wires...). Without the internet layer only computers connected to a similar network could talk to each other. 

#### 3) Transport layer
- This layer is responsible for ensuring that packets are received in the order they were sent and that no data is lost or corrupted. If a packet of data is lost, this layer will ask the sender to send another. This detection and retransmission is done by two protocols:

`1) Transmission Control Protocol (TCP)`: this protocol allows for the retransmission of lost or corrupted data and delivery of bytes in the order they are sent.

`2) User Datagram Protocol (UDP)`: allows the receiver to detect corrupt packets but does not guarantee the packets are delivered in the correct order. However, UDP is usually much faster than TCP.

#### 4) The Application Layer
- This is the layer that we work most directly with. This layer decides what to do with the data after it is transferred. All the application layer protocols can be found here. Some of the more famous protocols being HTTP and BitTorrent and if you wanted to, you could even create a application layer protocol yourself.

###HTTP
- Hypertext Transfer Protocol (HTTP) is a standard that defines how a web client talks to a server and how data is transferred back to the client. HTTP specifies how a client and server establish a connection, how the client requests data, how the server responds and how the connection is closed. HTTP connections use the TCP/IP protocol for data transfer. For each request from client to server, there is a sequence of 4 steps:

`1)` The client opens a TCP connection to the server on port 80 by default

`2)` The client sends a message to the server requesting the resource at the specified path. This request includes a header and optionally a blank link followed by data for the request.

`3)` The server sends a response to the client. The response begins with a response code followed by a header full of metadata, a blank line, and the requested document.

`4) ` The server closes the connection


####Typical Request
- The first line of the request is called the request line and includes a method, a path to a resource and the version of HTTP, usually something link this:

`GET/home.html HTTP/1.1`

- The client request usually includes other information as well in a header, each line takes the following form:

`KEYWORD:VALUE`

- other forms of information found in the request are:

`User-Agent`: lets the server know what browser is being used and allows it to send files optimized for the particular browser type.

`Host`: Specifies the server's name, which allows webservers to distinguish between different hosts served from the same IP address

`Accept`: Tells the server the types of data the client can handle.

####Typical Response
- A typical http response can be broken down into 3 sections. The first section is the first line and it is called the `status line`. Consisting of the HTTP version being used, followed by a response status code.

`HTTP/1.1 200 OK`

- The second section consists of all the other lines after the first. These lines hold meta data about the response, giving the client the appropriate information about the data received. This section ends with a blank line.

`
Content-Type: text/html; charset=utf-8
Content-Length: 55743
Connection: keep-alive

`

- The final section contains the optional data which is sent depending on your request.

###Conclusion
- Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on [Twitter](https://twitter.com/TristAndroidDev).