HTTP on TCP: Stateless Protocol on the Internet's Stateful Network

Photo by Leon Seibert on Unsplash

HTTP on TCP: Stateless Protocol on the Internet's Stateful Network

How HTTP Stateless Protocol is built on the TCP Stateful protocol

Sofwan A. Lawal's photo
Sofwan A. Lawal
·Aug 29, 2022·

6 min read

Featured on Hashnode

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

The Internet is a stateful network. Services such as connection management, congestion control, and error recovery are all made possible via the TCP protocol. When it comes to transmitting data reliably, these services are a must. In response to requests from HTTP clients, an HTTP server retrieves the requested files and returns them to the client in a suitable format (often HTML). An HTTP client is a piece of software that establishes a connection to an HTTP server and sends requests by issuing standard TCP messages bearing the Hypertext Transfer Protocol methods as their special headers. In the internet's stateful network, this blog post illustrates how the stateless Hypertext Transfer Protocol (HTTP) works on top of TCP packets.

Transmission Control Protocol

The Transmission Control Protocol, also known as TCP, is a transport protocol that operates on the Transport layer of the Open Systems Interconnection model. It is a protocol for connection streams that ensures the sequence of delivery and offers automatic retransmission of data. The Transmission Control Protocol (TCP) offers an assurance that the right transfer of an entire file or document will take place. It does this by dividing the file into a number of smaller packets and ensuring that each of those packets travels across the network in an orderly manner. This allows the individual packets to later be reassembled into the original file.

HTTP on TCP: An Overview

The HyperText Transfer Protocol (HTTP) is the means by which text is transferred between computers. It's a protocol for applications that provides access to and manipulation of hypertext objects and components (including HTML pages, PNG images, and so on). In order to function, HTTP relies on TCP, which relies on IP. HTTP is an application protocol, it is different from network protocols like IP and transport protocols like TCP because it is directly consumed by applications. When it comes to the nuts and bolts of sending data across a network, HTTP depends on TCP. The transmission of information in an HTTP request and response is mostly handled by TCP.

HTTP Request on the Internet’s Stateful Network

The following procedures need to be carried out in order for a client to successfully communicate with a server, whether that server is the ultimate server or an intermediate proxy.

1. Open a TCP connection

Through the TCP connection, a request (or several requests) can be sent and a response can be received. The client has the option of establishing a new connection with the servers, reusing an existing connection, or establishing several TCP connections.

2. Send an HTTP message:

A correctly composed HTTP request contains the following elements:

  • A request line: This contains the HTTP method, path and HTTP version
    GET /index.html HTTP/1.1
    
  • Headers: HTTP headers give the recipient with information about the message, sender, and method of communication. The HTTP protocol specifications specify HTTP headers and detail how to use them. Example
    Host: blog.sofwancoder.com
    Accept-Language: en
    
  • Optional message body: The HTTP message body is called the message body or the entity-body. The message, which is in the body, can be sent in its original form or in a way that makes it easier to send, such as by being broken into parts (chunked transfer-coding). Not all request methods can be used with message bodies.

3. Read the response:

HTTP/1.1 200 OK
Date: Wed, 17 Aug 2022 20:41:02 WAT
Server: Nginx
Content-Type: text/html

(the response body goes comes here)

3. Close the connection:

Mark the connection for closure and close it off.

TCP 3-Way Handshake Process: Establishing a session before communicating

Step 1 (SYN)

In the first step, the client wants to connect to a server, so it sends a segment with SYN (Synchronize Sequence Number). This tells the server that the client is likely to start communicating and what sequence number it starts segments with.

Step 2 (SYN + ACK)

The server responds to the client request with SYN-ACK signal bits set. Acknowledgement(ACK) signifies the response of the segment it received and SYN signifies with what sequence number it is likely to start the segments with

Step 3 (ACK)

In the final part client acknowledges the response of the server and they both establish a reliable connection with which they will start the actual data transfer

This is how the TCP handshake works. The client and server need to agree on each other's sequence numbers so that no packets get lost in transit. The client will then send a TCP segment with the sequence number of the last segment sent and the ACK flag set.

Sending Data over a Session

The client will now send data over the TCP connection. It will first break down the data into segments and send the segments in TCP segments with the data as the payload. It will set the sequence number of the last segment sent to the number of segments it has broken down the data into. The client will also send a TCP segment with the ACK flag set and the sequence number of the last segment it has received.

The server will receive the segments, and acknowledge they have been received by sending TCP segments with the ACK flag set and the sequence number of the last segment it has received. The server will send segments with the SYN flag set and the sequence number of the last segment sent. The server acknowledges the segments it has received by sending TCP segments with the SYN flag set and the sequence number of the last segment it has received.

TCP teardown: Ending a session after communicating

TCP close connection

Step 1 (Fin Wait)

A client sends a FIN packet and waits for a response; it can release some resources but awaits the response of the other part.

Step 2 (Close Wait)

A server receives the FIN packet and must release resources; it waits for a closing application level.

Step 3 (Last Ack wait)

The client can now fully close its job, but it must wait for network collision. it may have to send the final ACK another time.

Step 4 (CLOSE)

The server eventually receives the final ACK and destroys (kills) the connection.

TCP and reliability

The TCP protocol provides various mechanisms for managing reliability and congestion. For example, the TCP protocol retransmits data packets that are lost or marked as unread. The TCP protocol also reduces network congestion by limiting the rate at which a TCP sender can inject segments into the network. The TCP protocol implements these reliability and congestion control mechanisms by maintaining state information at the TCP header.

Conclusion

The Internet is a stateful network. The TCP protocol provides services such as connection management, congestion control, and error recovery. An HTTP server is a computer program that responds to requests from HTTP clients by serving them documents (often HTML files) in response to the request. An HTTP client is a computer program that connects to an HTTP server and makes requests by issuing standard TCP messages that have the Hypertext Transfer Protocol methods as their special headers.

 
Share this