Concepts / Introduction to Sockets and Network Connections

Introduction to Sockets and Network Connections

HTTP describes what the client asks for and how the server reports the result. The network connection is the setting in which this request-response exchange takes place.

  • Programming
Interactive lab

Try it: HTTP Request and Response

What really happens when a browser fetches a URL: DNS lookup, TCP connection, the exact HTTP/1.1 request text, how the server picks a status code (200, 301, 304, 404, 405, 500), and how a cache with max-age and ETag avoids or shortens the next request.

How it works

  1. Check the browser cache: a copy younger than its max-age is used with no request at all; an older one is revalidated with If-None-Match.
  2. Look up the host name to get an IP address (DNS), then open a TCP connection to port 80 (http) or 443 (https).
  3. Send the request line (method, path, HTTP/1.1), one header per line, a blank line, and the body if any.
  4. The server routes the path: missing → 404, wrong method → 405 with Allow, crash → 500, moved → 301 with Location, matching ETag → 304, otherwise 200 with Content-Type, Content-Length, ETag and Cache-Control.
  5. The client follows a 301 to its Location, stores a cacheable 200, or reuses its cached body after a 304.

Default run (17 steps): The browser will request GET http://data.pr4e.org/page1.htm, then visit it again 90 s later. Browser cache: on. … Done. Visit 1: 200; Visit 2: 304.

Simplified: A toy browser and one toy server on a simulated clock (fixed per-phase delays). The DNS resolver is a three-entry table, TLS is a single step with no real cryptography, every request opens a new connection (Connection: close), only 200 responses to GET are cached, and ETags are a toy hash of the body. No real network traffic is sent.

Educational simulation

Loading the simulation…

From Connection to Web Page

When a web client communicates with a web server, the exchange follows a predictable pattern. The client connects, sends a request, the server processes that request, and the server sends a response. HTTP, the Hypertext Transfer Protocol, defines this communication as a standardized, text-based request-response model. This cycle is the foundation of web communication.

Web clientConnectionWeb serverClient processingconnectscarries requestsends responsedelivers response
What happens as a client connects to a server, sends an HTTP GET request, receives a response, and processes it?

HTTP as the Communication Protocol

HTTP provides the rules for communication between a web client and a web server. It is standardized, which means the participants follow an agreed structure, and text-based, which means the request and response are organized as readable textual parts. The model is intentionally simple: the client sends a request, and the server returns a response.

Key point

HTTP describes what the client asks for and how the server reports the result. The network connection is the setting in which this request-response exchange takes place.

Reading a GET Request

An HTTP GET request identifies the resource the client wants. Its required structure includes the HTTP method, the resource path, and the protocol version. Header lines can provide additional request information. The request ends with a mandatory blank line, which separates the request headers from what follows.

GETHTTP method/resourceresource pathHTTP versionprotocol versionHeader linesrequest metadataBlank linerequest terminatorfollowed byfollowed bythenends with
How are the request line, headers, and mandatory blank line arranged in an HTTP GET request?
Worked example

A Request for a Resource

Identify the parts of a generated HTTP GET request.

  1. 1
    Method

    GET identifies the HTTP method used by the client.

  2. 2
    Resource path

    /articles identifies the requested resource path.

  3. 3
    Protocol version

    HTTP/1.1 identifies the protocol version in this generated example.

  4. 4
    Header

    Host: example.test is a header line carrying request metadata in this generated example.

  5. 5
    Blank line

    The empty line marks the required end of the request headers.

Result

The request communicates that the client wants the resource at /articles and then terminates its headers with a blank line.

The exact names and values of headers can vary, but their structural role is the same in this model: they are header lines that carry metadata. The essential GET pattern is the request line followed by header lines and a mandatory blank line.

Interpreting the Server Response

After processing the request, the server sends an HTTP response. The response has four structural parts: a status line, header lines containing metadata, a blank line separator, and a response body. The body contains the requested resource.

Status lineresponse statusHeader linesresponse metadataBlank lineseparatorResponse bodyrequested resourcefollowed bythenseparates
What does each part of an HTTP response contain, and how do the status line, headers, and body relate to one another?
Worked example

Reading a Generated Response

Identify the structural parts of a generated HTTP server response.

  1. 1
    Status line

    HTTP/1.1 200 OK is the status line in this generated example.

  2. 2
    Header lines

    Content-Type: text/plain is a header line containing response metadata in this generated example.

  3. 3
    Blank line

    The empty line separates the response headers from the response body.

  4. 4
    Response body

    Welcome to the articles resource is the body containing the requested resource in this generated example.

Result

The client can distinguish the response status, its metadata, and the requested resource by reading the response in order.

Key point

The blank line has a structural purpose in both directions of the exchange. In a GET request, it marks the end of the request headers. In a server response, it separates response headers from the response body.

Tracing the Complete Exchange

The complete cycle can be traced as four connected stages. First, the client establishes a connection. Second, it sends an HTTP request such as a GET request. Third, the server processes that request. Fourth, the server sends an HTTP response, which the client receives and processes. Leaving out any of these stages gives an incomplete picture of web communication.

What do you think happens?

A client has connected to a server and sent a GET request. What must happen before the client can process the requested resource?

  • The server processes the request and sends an HTTP response

  • The client sends the response before the server reads the request

  • The connection alone becomes the requested resource

Reveal answer
Answer:

The server processes the request and sends an HTTP response.

HTTP follows a request-response model. The connection provides the setting for communication, but the server must process the request and return a response containing a status line, headers, a separator, and the response body.

  • The client connects to the server.
  • The client sends a structured HTTP request.
  • The server processes the request.
  • The server sends a structured HTTP response.
  • The client processes the response and its requested resource.

Mistakes in Message Structure

  • Common mistake

    Treating a GET request as only a resource path

    What it looks like

    Writing down /articles but ignoring the method and protocol version

    Why it fails

    An HTTP GET request specifies the method, resource path, and protocol version as part of its request structure.

    Do this instead

    Read the request line as a complete structure: method, resource path, and protocol version.

  • Common mistake

    Omitting the mandatory blank line at the end of a GET request

    What it looks like

    Ending immediately after the last header line

    Why it fails

    The blank line marks the end of the request headers.

    Do this instead

    Include the blank line as the final structural part of the request.

  • Common mistake

    Confusing response headers with the response body

    What it looks like

    Treating metadata header lines as if they were the requested resource

    Why it fails

    The response headers contain metadata, while the response body contains the requested resource.

    Do this instead

    Use the blank line to distinguish the metadata section from the response body.

  • Common mistake

    Stopping the cycle after the connection

    What it looks like

    Assuming that connecting to a server completes the web communication

    Why it fails

    The complete cycle also requires a request, server processing, and a response.

    Do this instead

    Trace all stages from connection through response processing.

Practice the Trace

MEDIUM

A client wants a resource from a web server. Describe the exchange in order, beginning with the connection and ending when the client processes the server's response. Then list the four structural parts of the response.

Hints
  • Use the sequence: connection, request, server processing, response.
  • For the response structure, begin with the status line.
  • Remember the blank line separator before the response body.
MessageRequired structural parts
HTTP GET requestMethod, resource path, protocol version, header lines, mandatory blank line
HTTP server responseStatus line, header lines with metadata, blank line separator, response body

A comparison of the two main HTTP messages introduced in this article.

Key Takeaways

  1. 1
    HTTP is a standardized, text-based protocol built around a request-response model.
  2. 2
    A GET request identifies a method, resource path, and protocol version, includes header lines, and ends with a mandatory blank line.
  3. 3
    An HTTP server response contains a status line, metadata headers, a blank line separator, and a body containing the requested resource.
  4. 4
    The complete web communication cycle is connection, request, server processing, response, and client processing.
  5. 5
    A network connection is only one stage; HTTP gives the exchange its request and response structure.

At a glance

  • HTTP is a standardized, text-based protocol for communication between web clients and servers.
  • A GET request contains a method, resource path, protocol version, header lines, and a mandatory blank line.
  • A server response contains a status line, metadata headers, a blank-line separator, and the requested resource in its body.
  • The complete HTTP cycle moves from connection to request, server processing, response, and client processing.

Related concepts

More concepts starting with I