Fundamentals 11 min read

Understanding TCP/IP, TCP Handshake, UDP, and HTTP Protocol Basics

This article explains the fundamentals of TCP/IP networking, including the protocol layering model, the TCP three‑way handshake and four‑way termination, differences between TCP and UDP, and how HTTP operates as a short‑lived, stateless protocol built on top of TCP.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding TCP/IP, TCP Handshake, UDP, and HTTP Protocol Basics

TCP

TCP belongs to the transport layer, while HTTP operates at the application layer; they are not comparable, but HTTP relies on TCP for data transmission.

TCP/IP Protocol Layering Model

Physical layer: converts binary 0/1 into voltage levels, light flashes, or radio signal strength.

Link layer: represents the driver that handles the physical medium.

Network layer: Uses the IP protocol, which forwards packets based on IP addresses. IP is an unreliable protocol; it does not perform retransmission. When IP delivery fails, ICMP is used to notify the failure. ARP resolves the MAC address corresponding to an IP address; the MAC address is provided by the network card. IP implicitly includes link‑layer functionality, allowing communication regardless of the underlying link technology.

Transport layer: TCP is connection‑oriented, correctly handles packet loss and out‑of‑order delivery, but establishing and terminating a connection requires at least seven packet exchanges, which wastes resources. UDP is connection‑less; it does not guarantee delivery, and any notification of receipt must be handled at the application layer. Both TCP and UDP are general transport protocols.

Above the transport layer (session, presentation, application): In the TCP/IP model these three layers are merged, and network management is performed via the SNMP protocol.

Key Interview Points

TCP Three‑Way Handshake and Four‑Way Teardown

Three‑Way Handshake:

Client sends a packet with the SYN flag (first handshake).

Server replies with a packet bearing SYN/ACK flags (second handshake).

Client sends a packet with the ACK flag (third handshake).

Four‑Way Teardown:

Client sends FIN to close its data flow to the server.

Server acknowledges the FIN with an ACK (sequence number increased by one).

Server then sends its own FIN to the client.

Client acknowledges the server's FIN with an ACK (sequence number increased by one).

Difference Between TCP and UDP

TCP provides reliable, ordered delivery with connection management, while UDP offers a lightweight, connection‑less service without reliability guarantees; any acknowledgment must be implemented by the application.

The above image illustrates a typical chat scenario where TCP is used, requiring a handshake before messages can be exchanged.

If UDP were used, messages would be sent without waiting for acknowledgments, similar to a direct phone call.

HTTP

HTTP is built on top of TCP. When a browser needs a web page, it sends an HTTP request, which establishes a short‑lived TCP connection; after the response is received, the connection is closed, making HTTP a stateless protocol.

Statelessness means each request is independent; the server does not retain any session information between requests. Applications can maintain state by using sessions (cookies, local storage, etc.).

HTTP Status Codes

Common status code categories:

2XX Success 200 OK – request processed successfully. 204 No Content – request succeeded but no body is returned. 206 Partial Content – used for range requests.

3XX Redirection 301 Moved Permanently – resource has a new permanent URL. 302 Found – temporary redirection. 303 See Other – client should retrieve the resource with a GET request. 304 Not Modified – cached resource is still valid. 307 Temporary Redirect – similar to 302.

4XX Client Error 400 Bad Request – syntax error in the request. 401 Unauthorized – authentication required. 403 Forbidden – server refuses to fulfill the request. 404 Not Found – resource does not exist on the server.

5XX Server Error 500 Internal Server Error – generic server failure. 503 Service Unavailable – server overloaded or under maintenance.

HTTP Message Format

An HTTP request or response consists of three parts: a start line, header fields, and an optional body, separated by CRLF sequences. The end of the header section is indicated by an empty line (two consecutive CRLFs).

Example start lines:

Request line: GET /index.html HTTP/1.1

Status line: 200 OK

Headers are key‑value pairs (e.g., Content‑Encoding: gzip ). The body length is specified by the Content‑Length header; if absent, the message has no body (common for GET requests).

What Happens When You Enter a URL?

Enter the address in the browser.

Browser resolves the domain to an IP address (checking browser cache, OS cache, router cache, etc.).

Browser sends an HTTP request to the web server.

Server may respond with a permanent redirect (e.g., from http://example.com to http://www.example.com).

Browser follows the redirect.

Server processes the request and returns an HTTP response.

Browser renders the HTML.

Browser requests additional resources referenced in the HTML (images, audio, video, CSS, JS, etc.).

Browser may send asynchronous requests (AJAX, fetch, etc.).

Source: juejin.im/post/5ad4094e6fb9a028d7011069
webTCPHTTPNetworkingprotocolUDPTCP handshake
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login 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.