Sub topic 7
Layer 5: Session, managing dialogues
This chapter assumes you know the layers below it. Layer 4 gives you a TCP connection or a UDP exchange identified by ports. It also assumes you are comfortable reading short Python snippets.
What the session layer is for
Layer 4 moves data between two ports. It does not know whether a series of exchanges belongs to one conversation. The session layer is the OSI layer that opens, manages, and closes such a conversation, called a session (a long-lived dialogue between two endpoints that can span many messages).
The OSI model assigns the session layer three jobs:
- Establishment and teardown. Agree to start a dialogue, and end it cleanly.
- Dialogue control. Decide who may send and when. The modes are simplex (one direction only), half-duplex (take turns), and full-duplex (both at once).
- Synchronization. Insert checkpoints into a long transfer so that, after a failure, the dialogue resumes from the last checkpoint instead of from the beginning.
A session is not a connection
A TCP connection and a session are different things. A connection lasts as long as the two sockets stay open. A session can outlive a connection, or use several connections. Suppose a laptop switches from Wi-Fi to mobile data. The TCP connection breaks, but a well-designed session can resume on a new one.
The diagram shows this. The checkpoint lets the transfer continue after the drop.
Where you meet it in practice
Real protocols rarely say "I am Layer 5". These examples are often placed there:
| Example | Session behavior |
|---|---|
| NetBIOS session service | Sets up named sessions between Windows hosts |
| RPC (remote procedure call) | Keeps call context between client and server |
| SMB | Authenticates once, then reuses the session for many file operations |
| SIP | Sets up, changes, and ends voice and video calls |
| PPTP | Manages a VPN tunnel session |
Web applications show the same idea at a higher level. HTTP is stateless (each request stands alone). A site creates a session by issuing a token, usually in a cookie, and looking up the user's state on each request.
A small session example
This script runs offline. It keeps a table of sessions and ends one after it sits idle too long. Establishment, use, and expiry are the session layer's concerns, even though here they are written as application code.
import secrets
import time
SESSIONS = {}
IDLE_TIMEOUT = 2 # seconds
def open_session(user):
sid = secrets.token_hex(8)
SESSIONS[sid] = {"user": user, "last_seen": time.monotonic()}
return sid
def use_session(sid):
s = SESSIONS.get(sid)
if s is None:
return "no such session"
if time.monotonic() - s["last_seen"] > IDLE_TIMEOUT:
del SESSIONS[sid]
return "session expired"
s["last_seen"] = time.monotonic()
return f"hello {s['user']}"
sid = open_session("alice")
print(use_session(sid)) # hello alice
time.sleep(3)
print(use_session(sid)) # session expired
print(use_session(sid)) # no such sessionNotice that the token is random and unguessable. A predictable session ID lets an attacker take over someone else's session, a flaw known as session hijacking.
Common errors
- Treating "connection" and "session" as synonyms. A dropped TCP connection does not always end a session, and an open connection does not prove a valid session.
- Blaming the network for session timeouts. If a user is logged out after ten idle minutes, the cause is usually a server-side idle timeout. Packets are not being lost.
- Assigning protocols to Layer 5 with certainty. Many protocols mix Layers 5, 6, and 7. Treat the assignment as a guide to what a protocol does, not a strict label.
- Skipping session state when load balancing. If a load balancer sends one user's requests to different servers, and session state lives on only one of them, the user appears logged out. Fix this with sticky routing or a shared session store.
Exercise
A user is logged in to a web app over a stable connection. After 30 minutes without activity, the next click sends them to the login page. Pings to the server succeed and port 443 accepts connections. Which layer's behavior most likely explains this, and why?
Show answer
The session layer's behavior (in practice, the application's session handling). The lower layers work, since ping succeeds and port 443 accepts TCP connections. The server most likely ended the session after an idle timeout and discarded its state, so the old session token is no longer valid.
Exercise
Why can a session outlive a single TCP connection? Name the session-layer feature that makes resuming possible.
Show answer
A session is identified by its own state, such as a session ID, and not by the sockets underneath. After a connection drops, a client can open a new connection and present the session ID. Synchronization checkpoints let the dialogue resume from the last confirmed point.