Transport Layer Multiplexing and Demultiplexing

Rukshani Athapathu
Coder's Corner
Published in
5 min readAug 26, 2018
Image Courtesy: Pixabay

In this post we are going to talk about how multiplexing and demultiplexing happens at the transport layer. Before we jump into the subject, let’s first jog our memory about what we already know about TCP/IP (Transmission Control Protocol/Internet Protocol), so that we are all in the same page.

Let’s start with processes.

What is a Process?

Think of a process as a program running within an end system. So how do processes on two different end systems communicate with each other? Well, by exchanging messages across a network and that’s where TPC/IP protocol suit helps us understand how the communication happens between hosts.

Following diagram roughly sketches out what happens when we send data across a network from one process to another.

As you can see here, TCP/IP protocol suit is made up of different layers and remember that each upper level protocol uses services provided by one or more lower level protocols.

Different Names for Packets?

Packets of information that passes through these layers are referred by different names in each layer. At the application level, data that needs to be exchanged is referred to as a message. Then the transport layer takes this message and adds transport layer headers to it. The resulting encapsulated packet is called a segment. When the segement is passed on to the network layer, it wraps the segment with its own network layer headers and it goes by the name of datagram. Then when the link layer takes over these datagrams, it adds link layer related headers and pass those packets which is then known as frames to the physical layer for transmission.

At the destination, the reverse process happens. Each layer decapsulates the receiving packets and handover the payload to the higher level layer until the message reaches the application level. One thing to keep in mind here is that, between the source and the destination, there can be many routers, link layer switches which can contain different set of layers depending on their functionality. For example, routers typically don’t have transport or application layers implemented within them.

Process to Process Communication

If you look at the network layer, it is only responsible for host to host communication. That is, it can only deliver messages to the destination computer. Once a message has been delivered to the destination, it still needs to be handed over to the correct process and transport layer has the responsibility to take care of that.

Suppose you have three application processes running in your machine. Now when your computer receives data from outside that message needs to be directed to the correct application process. This is where the sockets come into play and now it is a good time to introduce that.

Sockets

Transport layer actually does not directly deliver messages to processes. So, who does that? Well, there’s an intermediate software interface called a socket layer that does the transmission of messages between the underline network and the processes. Now remember that a process can have one or more sockets through which it can pass data to and from network and each of these sockets have a unique identifier.

So, What is Multiplexing and Demultiplexing?

Transport layer gathers chunks of data it receives from different sockets and encapsulate them with transport headers. Passing these resulting segments to the network layer is called multiplexing.

Multiplexing

The reverse process which is delivering data to the correct socket by the transport layer is called demultiplexing. But this still doesn’t explain how the transport layer identifies the correct socket. Port numbers are the ones that do the trick.

Demultiplexing

Port Numbers

Each socket in a host can be assigned a port number. In TCP/IP protocol suit, port numbers are 16-bit numbers ranging from 0 to 65,535. Well known port numbers start from 0 to 1023 and they are reserved for well-known application protocols. For example, HTTP uses port number 80, FTP 21, etc…

Now let’s examine how these port numbers are useful in understanding the role of transport-layer multiplexing and demultiplexing. When packets arrive, the transport layer looks for the destination port number in the segment that is bound to that port number, but that alone is not enough for the TCP to demultiplex the incoming segment. It has to examine all the four elements(local host, local port, foreign host, foreign port) in the socket pair to uniquely identify the endpoint to which the receiving segment should be directed to. Then the data in that segment passes through the socket into the attached process.

TCP Segment

Client and Server Processes

Finally about the client server paradigm. Earlier we briefly talked about process to process communication. In the context of the communication between these processes, remember that the process that initiates the communication is called a client and the process that waits to be contacted is called the server. To establish a connection between the client and the server, TCP suit needs both the IP address and the port number at each end and this combination of the IP address and the port is called a socket address.

What we have talked so far is just the theoretical stuff. Now to see all this in action you can read the following post which greatly explains these concepts with code snippets.

References

--

--