Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 79 ยท Getting Started with Networks

Session and Presentation Layers and RPC

Understand OSI layers 5 and 6: sessions, checkpoints, character encoding, serialization, compression and encryption, plus how RPC stubs marshal calls, with Python demos.

The layered models lesson gave you the OSI model's seven layers. Layers 1 to 4 have obvious homes: cables, frames, packets, segments. Layers 5, 6 and 7 are blurrier. In the Internet's own TCP/IP model they are all squeezed into one "application" layer, which is why many people have never seen a "session protocol" or a "presentation protocol" in a packet capture.

They still describe real jobs that every networked program has to do, so they are worth understanding. The session layer manages a long-running conversation between two programs. The presentation layer makes sure both programs agree on how data is represented. Remote procedure calls (RPC), one of the most important ideas in distributed systems, use both. In this lesson you will see what each job involves, where it lives in real protocols today, and how an RPC call turns a function call into bytes on the wire and back.

The session layer: managing a conversation

A transport connection (TCP) moves bytes. It knows nothing about what those bytes mean to the two programs. A session is a higher-level relationship: "this user is logged in, we are halfway through transferring a file, and this is who has the right to speak next". OSI assigns these duties to layer 5:

  • Session establishment and teardown. Agree to start, authenticate, set parameters, and end politely so that nothing is left half-done.
  • Dialog control. Decide whose turn it is: one direction at a time (half duplex), or both (full duplex).
  • Synchronization and checkpoints. Insert markers into a long transfer. If the connection breaks after a 2 GB file reached the 1.5 GB checkpoint, the transfer resumes from there rather than from zero.
  • Recovery. Reconnect and restore the state of the conversation after a failure.

No single "OSI session protocol" won in practice. Instead, session behavior is built into other protocols and tools:

Where you see session ideas What it does
Login cookies and tokens in HTTP An application session on top of stateless requests
TLS session resumption Skips the full handshake when a client returns
Database connections and SMB/NetBIOS sessions Authentication and state for a long conversation
Resumable downloads (HTTP Range) A checkpoint-like restart
RPC call identifiers and timeouts Matching replies to requests and recovering from loss
SSH and VPN sessions Keys, keepalives and re-keying

When an exam asks "which layer handles authentication, checkpointing and dialog control?", the OSI answer is the session layer. When a developer says "session", they usually mean a login session in a web application, which OSI would call layer 7.

The presentation layer: agreeing on representation

Two computers can have different ideas about how to store the same value. A 32-bit integer may be stored with the most significant byte first (big endian, used by network protocols) or last (little endian, used by x86 and most ARM systems). Text may be ASCII, Latin-1 or UTF-8. A structure may be padded differently by two compilers. If one program just copies its memory onto the wire, the other program may read garbage.

The presentation layer takes care of this with three kinds of work:

  1. Translation. Convert between a program's internal form and an agreed transfer syntax. This includes character encoding (UTF-8, UTF-16) and serialization formats: JSON, XML, ASN.1 with BER (used by SNMP and LDAP), Protocol Buffers, CBOR and XDR.
  2. Compression. Make the payload smaller (gzip, brotli, zstd) so it travels faster.
  3. Encryption. Hide the data from eavesdroppers; in practice done by TLS, which is why it is often drawn "between" layers 4 and 7.

Turning an in-memory value into bytes is called marshalling (or serialization); turning bytes back into a value is unmarshalling (deserialization). The next program shows a record in several forms and then demonstrates the classic bug: reading bytes with the wrong byte order.

Python Marshalling, endianness and compression

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the output closely. The JSON is longer but self-describing: any program can read it without a schema. The binary form is compact, but the receiver must know the layout in advance. Decoding with the wrong order reads the number 258 as 513 and turns 21.5 into a meaningless tiny float. And compression shrinks the repetitive log hugely but can make very small messages larger, because of the format's own overhead.

Remote procedure calls

A remote procedure call (RPC) lets a program call a function that runs on another machine as if it were a local function:

TEXT
balance = bank.deposit(account=17, amount=50)   # looks local, runs on the server

The programmer sees a normal call. Underneath, a lot of networking work happens, and that work uses the session and presentation ideas above.

  1. The program calls a client stub: a generated local function with the same name and parameters as the remote one.
  2. The stub marshals the procedure name and arguments into a message, adds a call identifier, and gives it to the transport (TCP, UDP or HTTP).
  3. The message crosses the network to the server.
  4. The server stub unmarshals it, finds the real function and calls it.
  5. The result is marshalled and sent back; the client stub unmarshals it and returns it to the caller, which never noticed that anything left the machine.
sequenceDiagram
  participant C as Client program
  participant CS as Client stub
  participant SS as Server stub
  participant S as Server function
  C->>CS: deposit(17, 50)
  CS->>SS: marshalled request (call id 7)
  SS->>S: deposit(17, 50)
  S-->>SS: 150
  SS-->>CS: marshalled reply (call id 7)
  CS-->>C: 150

The stubs hide marshalling and the network from both programs.

Well-known RPC systems include ONC/Sun RPC (used by NFS; data is encoded with XDR, and a port mapper on port 111 tells clients where each service listens), DCE/RPC and Microsoft RPC, XML-RPC and JSON-RPC (text over HTTP), and gRPC (Protocol Buffers over HTTP/2). Stubs are normally generated from an interface description such as a .proto file, so client and server always agree on the types.

Why a remote call is not a local call

RPC hides the network, but it cannot remove its failures. A local call either happens or throws. A remote call has a third outcome: you do not know. The request may have been lost, the server may have crashed halfway, or the reply may have been lost after the work was done. That raises the question of how many times the procedure ran:

Semantics Meaning Example
At least once The client retries until it gets a reply; the server may run the call several times Fine for reading a value
At most once The server remembers call identifiers and never runs the same call twice Needed for "deposit 50"
Exactly once Ideal, but only achievable with extra machinery (transactions) Hard in general

The simulation below shows the problem. A client deposits 50 into an account over an unreliable network where the first reply is lost. The client times out and retries. Without call identifiers, the deposit happens twice.

Python RPC retries: at-least-once versus at-most-once

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without call identifiers the retry credits the account twice, and the real balance is 200. With the saved-reply table the server recognizes call 7, returns the stored answer, and the work is done once. Real systems also expire the table, and they add deadlines, cancellation and retry limits. This is why well-designed APIs make operations idempotent (safe to repeat) whenever they can.

Putting the layers together

When your phone opens a banking app, several layer 5 to 7 jobs run together: the app logs in (a session with a token), sends a JSON request (a serialization format), the connection is compressed and encrypted (TLS), and the server's handler is called through a framework that works like an RPC stub. OSI would split these into three layers; the Internet does not, and both views are useful for reasoning. When you debug, ask: is the problem in the bytes on the wire (presentation), in who is logged in or where we are (session), or in what the application means (application)?

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which OSI layer handles checkpoints so a long transfer can resume after a break?

    Synchronization points and recovery are session layer duties in the OSI model. In practice they are implemented inside applications and protocols.
  2. 2. Two computers disagree about byte order and exchange raw 32-bit integers. What happens?

    Big-endian and little-endian machines store the bytes of a number in opposite order. A common wire format (such as network byte order) solves this.
  3. 3. What is the job of a client stub in RPC?

    The stub looks like the real function to the caller but turns the call into a network message and the reply back into a return value.
  4. 4. A client retries a deposit request because the reply was lost. How can the server avoid depositing twice?

    At-most-once semantics rely on unique call ids and a table of earlier replies. Idempotent operations are another solution.
  5. 5. In the TCP/IP model, where do session and presentation functions live?

    TCP/IP has no separate layers 5 and 6. Applications and their libraries (TLS, JSON, gRPC, login sessions) do these jobs.

Summary

  • The session layer manages a conversation: setup, dialog control, checkpoints, recovery and teardown. In real systems these live in applications (login sessions, TLS resumption, resumable transfers).
  • The presentation layer agrees on representation: character encoding, serialization (JSON, ASN.1, Protocol Buffers, XDR), compression and encryption.
  • Marshalling turns values into bytes and unmarshalling reverses it; byte order mistakes corrupt numbers silently.
  • RPC wraps a remote call in stubs that marshal and send it, but it must handle lost requests and replies: use call ids, timeouts and idempotent operations.
  • TCP/IP folds OSI layers 5 to 7 into the application layer.

Next, see the concrete protocols that sit at the top of the stack in the application layer.