How Does the Internet Work?
Client and Server:
Client is your web browser; the server is where the data is stored.
Client-Server Communication:
Request:
1. Browser: You enter a URL (like www.google.com) into your web browser.
DNS Resolution:
2. Browser to DNS: The browser requests the IP address for the domain (google.com) from a DNS server.
3. DNS Server: The DNS server translates the domain name into its corresponding IP address and sends it back to the browser.
The DNS server that resolves a URL into an IP address is typically a recursive DNS server, usually provided by Internet Service Providers (ISPs) or third-party DNS services.
TCP Connection:
4. Browser Initiates TCP Connection: With the IP address, the browser initiates a TCP (Transmission Control Protocol) connection with the web server.
Once the browser has the IP address of the server, it initiates the TCP connection using a process known as the TCP three-way handshake:
- SYN (Synchronize) Packet: The browser sends a SYN packet to initiate the connection, containing a random sequence number for security and ordering.
- SYN-ACK (Synchronize-Acknowledgment) Packet: The server responds with a SYN-ACK packet, this packet contains the server’s own (1) random sequence number and an (2) acknowledgment number that is one more than the sequence number received from the client.
- ACK (Acknowledgment) Packet: The browser sends an ACK packet in response, confirming the server’s SYN-ACK and establishing the connection. The acknowledgment number in this packet is one more than the sequence number received from the server.
The TCP protocol ensures that all packets are delivered in order and without errors.
Sending the Request:
5. Browser Sends HTTP Request: Once the TCP connection is established, the browser sends an HTTP (Hypertext Transfer Protocol) request to the server, asking for the webpage associated with ‘www.google.com’.
6. Server Processes Request: The server receives the HTTP request and processes it. If the page exists and the server is configured to provide it, the server will prepare the appropriate HTTP response.
Sending the Response:
7. Server Sends HTTP Response: The server sends an HTTP response back to the browser. This response typically includes the status of the request (like 200 OK for a successful request) and the requested content, which is usually HTML, CSS, JavaScript, and media files.
Browser Render:
8. Browser Renders Webpage: After receiving the response, the browser interprets the content (HTML, CSS, JavaScript, etc.) and renders the webpage for you to view and interact with.
9. TCP Connection Closure: After the data transfer is complete, the TCP connection is usually closed, though modern web browsing often uses persistent connections where the TCP connection is kept open for further requests, reducing latency.
HTTP/HTTPS in Details:
HTTP (Hypertext Transfer Protocol) and HTTPS (Hypertext Transfer Protocol Secure) are protocols used for communication between web browsers and web servers. They define how data is formatted and transmitted over the internet.
HTTP uses port 80 by default, HTTPS uses 443.
HTTP is not secure for transmitting sensitive data, as it operates in plain text. HTTPS (HTTP Secure) is a secure version of HTTP that encrypts data using SSL/TLS, providing a secure communication channel.
HTTP Methods (Verbs):
- GET: Requests data from a specified resource. It is the most common method and is used for retrieving information.
- POST: Submits data to be processed to a specified resource. Often used for form submissions and file uploads.
- PUT: Updates a resource or creates a new resource if it does not exist at the specified URI.
- DELETE: Deletes the specified resource.
- HEAD: Similar to GET but retrieves only the headers and not the actual data. Useful for checking the headers before downloading a large resource.
- OPTIONS: Requests information about the communication options available for the target resource.
- PATCH: Applies partial modifications to a resource.
HTTP Status Codes:
- 1xx (Informational): Request received, continuing process.
- 2xx (Successful): The request was successfully received, understood, and accepted.
- 3xx (Redirection): Further action needs to be taken to complete the request.
- 4xx (Client Error): The request contains bad syntax or cannot be fulfilled.
- 5xx (Server Error): The server failed to fulfill a valid request.
Example Request:
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0
Example Response:
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 1234
<!DOCTYPE html>
<html>
<head>
<title>Example Page</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>