Fundamentals 9 min read

Understanding HTTP vs TCP: Long vs Short Connections Explained

This article explains how HTTP relies on TCP/IP, clarifies the stateless nature of HTTP, and compares long‑lasting persistent connections with short‑lived ones, covering their handshakes, advantages, disadvantages, and appropriate usage scenarios.

21CTO
21CTO
21CTO
Understanding HTTP vs TCP: Long vs Short Connections Explained

1. Relationship between HTTP and TCP/IP protocols

HTTP long and short connections correspond to TCP long and short connections. HTTP is an application‑layer protocol that uses TCP at the transport layer and IP at the network layer. IP handles routing and addressing, while TCP provides reliable, ordered delivery of data packets.

2. Why HTTP is considered stateless

Statelessness means the protocol does not retain transaction state; the server does not know the client’s previous interactions. Opening a page does not affect subsequent requests. HTTP is a stateless, connection‑oriented protocol, but it can keep a TCP connection alive and does not use UDP.

3. What are long and short connections?

In HTTP/1.0 the default is a short connection: each request creates a new TCP connection that is closed after the response. In HTTP/1.1 the default is a persistent (keep‑alive) connection, indicated by the header Connection: keep-alive. With a persistent connection the TCP link remains open for subsequent requests, reducing the overhead of repeated handshakes.

3.1 TCP connection

TCP requires a three‑way handshake to establish a connection and a four‑way handshake to close it, consuming resources and time.

Classic three‑way handshake diagram:

Classic four‑way termination diagram:

3.2 TCP short connection

A short connection involves a single request‑response cycle; after the data exchange either side can close the connection, typically the client initiates the close. Advantages: simpler management and only useful connections are kept.

3.3 TCP long connection

In a long (persistent) connection the TCP link remains open after a request, allowing subsequent reads and writes without additional handshakes. TCP keep‑alive probes can detect half‑open connections; if no activity occurs for two hours the server sends a probe, and various client states determine whether the connection is kept or terminated.

3.4 Operation process of long vs short connections

Short connection flow: establish → data transfer → close.

Long connection flow: establish → data transfer → (keep alive) → further data transfer → close.

4. Advantages and disadvantages

Long connections reduce the overhead of repeated TCP handshakes, saving time and bandwidth for frequent requests, but they consume server resources and can be vulnerable to malicious idle connections. Servers may need to close idle long connections or limit the number per client. Short connections are simpler and avoid resource waste when requests are infrequent, but they incur handshake overhead for each request.

5. When to use long or short connections

Use long connections for frequent, point‑to‑point communication where connection count is limited (e.g., database connections). Use short connections for high‑concurrency web services where keeping many connections open would exhaust server resources.

Source: WhyWin URL: http://www.cnblogs.com/0201zcr/p/4694945.html
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TCPHTTPNetwork Protocolslong-connectionshort connection
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.