Networking – What is Persistent Connection and Session

datamacnetwork-discoveryositransport-protocol

There is one thing I still cannot understand is persistent connection and sessions.
I will explain how I understand all network stuff.

  1. Physical layer . This is just voltage levels going through wires, there is nothing difficult on this layer to understand.

  2. Data link layer for example frame which contains some information about devices like MAC address. How does frame reach the destination ? Using switches for example. Switch knows which port is mapped to specific address and when frame comes it just send voltage levels to specific port.And on the other side network card receives voltage and triggers interrupt which is handled by the OS. I don't understand how does packet is directed to the right device using Wifi connection.

  3. Network layer. Just uses space defined for data in data link layer to add IP header on the top, decreasing data payload size that can be transmitted over the network in one frame

What about my main question about persistent connection.
I don't understand what does persistent mean in this case. I understand word persistent as following : no one else can occupy wire at the time of persistent connection. So this connection is only one-to-one. But this is not right. Persistent connection can be established while other packets are going through the same wire.
I guess it works in a following way. Periodically client or server sends packet called heartbeat in order to accept that the opposite side is still here and stores this information for example somewhere in OS like boolean flag ( is_still_alive) in other case if response doesn't come in some period of time connection will be closed (removing flag, clearing buffers). And if packet with ACK for heartbeat comes later it will response with intent to establish connection again.

I guess I will understand session mechanism if I understand described above.

Please help to understand how this stuff works. I would be grateful for any help.

Best Answer

First a word about:

I understand word persistent as following : no one else can occupy wire at the time of persistent connection. So this connection is only one-to-one. But this is not right. Persistent connection can be established while other packets are going through the same wire.

This is circuit-switched network vs packet-switching network

circuit-switched network
In such networks, a physical dedicated line is established between the two hosts who wants to communicate.

The better example is the first phone networks where human operators manually connect two phone lines to establish communications on request. enter image description here

Latter human operators where replaced with mechanisms. The enterprise internal version of such mechanism is known as "PBX" (Private Branch eXchange).

In this case you actually have a physical connection that is maintained and nobody else can use any of the circuit during that time.

packet-switching network
IP networks are packet-switched networks. As you said many packets from several applications can flow on the wire at the same time, and for a single conversation, different packets can take different routes.

This allow better utilization of the link and more robust design (of course it has some drawbacks).

So at the IP layer there's no "connection" nor "session" established.

There's an emerging trend to identify flows and apply specific switching and / or routing rules to them, and a flow can for example correspond to a TCP session but this is not what we usually refer to when speaking about connection / session.

Connection

IP provide two main protocols (among others) to establish communication between two hosts : UDP and TCP.

UDP is connection-less. That means (quoted from Wikipedia) that "a message can be sent from one end point to another without prior arrangement. The device at one end of the communication transmits data addressed to the other, without first ensuring that the recipient is available and ready to receive the data".

TCP is connection-oriented. Before actually transmitting date, the two hosts talk to each other to check that they are both available and agree to exchange some kind of data (think of the preamble of a typical phone call). This connection is maintained until one host signal that it want to end it or after some time has passed without exchanging any data.

A VPN connection usually work the same way. Two hosts exchange some information (including which encryption protocol to use, some cryptography keys and user authentication mechanism etc...) and if bot agree on the terms, a connection is established and maintain. Usually some keep-alive packets are sent at regular interval to say "I'm still here, don't drop the connection".

Session
Sessions are established by applications. A session can correspond to a single TCP connection, but a session can use several different TCP connection or use UDP as the transport protocol.

A HTTP(s) session for example may use cookies to identify the user and can provide access to different resources (think about web banking).

A Voice Over IP (VOIP) phone call will use the SIP protocol. SIP means "Session Initiation Protocol" and it can run over UDP.

The exact mechanism of establishing / maintaining / closing a session depends on the applications used.