[Music] let's continue our study of the transport layer here by looking at the issues of multiplexing and demultiplexing now these actually happened in all layers of the protocol stack but we're going to consider them here in the context of the transport layer here's how to think about demultiplexing think of an internet host it's got data grams that are arriving these data grams have payloads that are bound for different applications or possibly to different protocols running in that host the process by which those payloads are directed to the appropriate application or the appropriate protocol running in
that host that's the issue of demultiplexing multiplexing is is essentially the inverse of that we learned in chapter 2 for example that many applications may be sending down through multiple sockets their application messages to TCP TCP is going to be taking those messages and then funneling them down into into IP that's the process of multiplexing now again we're going to look at this primarily in the context of TCP and UDP but again this is a more general topic that occurs in all layers of the protocol stack here's the setting in the scenario we've got an
HTTP server in the middle here and this HTTP server is going to be sending HTT messages over to the client over on the left-hand side now of course this client has many applications running we see a Skype application a Netflix application and of course we've got an HTTP web browser there and so the question we want to ask ourselves is how among all those applications when a message is sent from the server to the client that that information is actually demultiplex up to the web browser and not to any of the other applications running on
the client and we also have to think about demultiplexing at the server where multiple clients may be sending HTTP messages back to the server these messages may need to be demultiplexed up to different processes at the server each of which are responsible for communicating back to the clients was that done well we're all familiar with multiplexing and demultiplexing from things that happen in our daily lives highways are a good example of that many cases we have multiple on ramps where cars are being multiplexed on to the highway and of course at big interchanges weirdy multiplexed
off of off of a highway onto a number of different possible exit ramps maybe an even better example is what happens when we get to an airport for example you arrive and immediately well if you have business class you go here economy class you go over here when you get to pre-check if you have tsa PreCheck you go to the left otherwise you go to the right and once you get to the gate of course there's a place where you line up for premium class service and there are multiple coach class service lines as well
that notion of being directed being split and being directed to a particular class of service that's sort of at the very core of what demultiplexing is all about so let's continue on here and let's actually look at this now in an Internet context of course and take a look at what happens in UDP so here we have our three host scenario again and in this particular example we're actually showing the processes that are going to be communicating as well as those sockets those little yellow rectangles there with the white in the middle showing where buffers
actually are and so in this example p1 and p3 are going to be communicating in p2 and p4 are going to be communicating let's see how that's done well on on the multiplexing side if we look at the host in the center we see that p1 and p2 are going to be sending down through their sockets through the transport layer transport layer is going to have to multiplex data coming in from p1 and p2 take that data put it into segments and add information into the transport header that's going to be used for later demultiplexing
now when packets and data grams are actually received at that host in the center we're going to have to perform the dual operation there the d multiplexing operation and in that case the transport layer is going to be using header information to liver the contents of the received segments up to the correct socket okay so let's take a deeper dive into how demultiplexing works remember when a host receives an IP Datagram each data gram has a source IP address the IP address of the sender of the data gram it's also got a destination IP address
and that's actually the host where we're actually doing this processing each data gram also carries one transport layer segment and in the transport layer segment there's a header and we're going to be interested in two fields within the header of the transport layer segment for for our purposes here and that's the source port number and the destination port number and the host is going to use the IP addresses and the port numbers to direct the segment to the appropriate socket and we're going to see that this is a little bit different in TCP than it
is from UDP UDP simpler so let's start there okay in order to now understand how UDP connectionless demultiplexing works we're gonna have to remember a little bit about what we learned in Chapter two and remember there when we're looking at socket programming you recall that when creating a socket an application programmer has to specify a host local port number here's a code fragment here where the data gram socket is being created and there's a host local port number one two five three four that's specified now when creating a data gram that's actually going to now
be sent into a socket we have to specify where that data gram is destined and there we specify the destination IP address and the destination port number not the localhost port number now over on the receiving side when the receiving host receives the UDP segment it's going to check the destination port number and direct the UDP segment to that socket associated with that port number that's the active demultiplexing there now you may think for a second and say well wait a second we can have multiple clients sending data grams to the same UDP port number
at a destination if that happens the UDP data grams with the same destination port number even though they're coming from different source IP addresses and maybe different source port numbers these are going to be directed to the same socket at the receiving host because that demultiplexing only happens in the case of UDP on the basis of the destination port number let's now take a look at an example of UDP demultiplexing we've got our three hosts as usual on the left hand side we have process p3 which is created a data gram socket with local port
number nine 157 on the right hand side we have process p4 which has created a data gram socket with a local port number five seven seven five and in the middle we've got process p1 which has a data gram socket with an Associated port number of six four to eight now p1 and p3 will be communicating with each other p1 and p4 will be communicating them with each other in the first example here we see data grams being exchanged from p1 to p3 and from p1 back to p3 let's look at what p1 is sending
to p3 and in particular focus here on the source port number and the destination port number of the data gram shown at the bottom of the figure here the source port number nine one five seven is the port number associated with the socket being used by the sender by the source the destination well that's being destined to port six four to eight which is the port number associated with the data gram socket of process p1 in the middle now what happens when p1 replies back to p3 well we learned in Chapter two that the destination
port number nine 157 that was taken from the source port number from the data gram at the bottom the arriving Datagram to which the return data gram is a reply so we see the destination port of the upper most of the two data grams on the Left has a destination port number of nine hundred fifty-seven and the source port number of six four two eight which again is the port number associated with the data gram socket 4p1 well on the right-hand side we don't specify what the destination and source port numbers are see if you
can figure that out for yourselves it's really the mirror of what's happening on the left-hand side well that's how multiplexing and demultiplexing happens in UDP now let's take a look at TCP where we'll see that the decisions are a little bit more complicated well in the case of UDP we saw that demultiplexing was really pretty simple in the case of TCP it's connection oriented that means we've got a sending side and a receiving side and how are we going to identify the sender and the receiver well certainly on the basis of the IP address but
also on the basis of the sending port number and the receiving port number so a TCP socket when we instantiate a TCP socket it's going to be identified by a four tuple the source IP address the source port number that's on the sending side the destination IP address and the destination port number that's going to be on the receiving side of the connection now so when we do demultiplexing the receiver is going to use all four values in this four tuple to direct a segment to the appropriate socket we'll look at an example in just
a second let's let's wrap up here just by noting that a server can have many simultaneous TCP sockets each socket is going to be identified by its own four tuple and each socket is going to be associated with a different connecting client client process our TCP demultiplexing example here has again our familiar three hosts except this time in the center we've got an apache HTTP server that it's going to be exchanging HTTP messages over TCP with the host on the left-hand side which has IP address a or the host on the right-hand side which has
IP address C and the address of the HTTP server is B let's take a look now at the communication that's happening between process p3 running on host a on the left-hand side and process p4 running in the Apache server in a data gram flowing from left right we see the source IP address associated with that data Graham of course is a that's the center of the data Graham the IP address of a and the source port number is 9 157 that's actually the port number the local port number associated with the socket that p3 has
created what's in the destination fields there well the destination IP address is B that's the IP address of the apache server and the port number is 80 and you may recall that 80 is the port number associated with HTTP service let's now look at a reply coming back from p4 back to p3 here we see the source IP address is B the port number is 80 the destination of course is IP address a and the destination port number is nine one five seven we're also showing here on the right-hand side two data grams that are
flowing from host C to the HTTP server the apache web server in the middle here and so take a look at the field values there hopefully those are gonna make make sense to you the one thing we really want to highlight here though is let's look at the three data grams that are all arriving to the HTTP server in the middle notice that the destination port number in all three of these data grams here is 80 that's absolutely critical remember with UDP we would have demultiplex just on the basis of that destination port ID in
the case of TCP because it's connection oriented remember we D multiplex on the four tuple and each of those four tuples of those three messages arriving are unique so those are going to be D multiplexed correctly to p4 p5 and p6 well that wraps up our study of multiplexing and demultiplexing so let's just recap what we learned we learned that that process of demultiplexing at the transport layer is really that question of deciding what is the socket to which we need to take the segment payload and then deliver into that socket in the case of
multiplexing we that both TCP and UDP need to be able to take data from multiple sockets and then deliver that put that in segments and then deliver that down to the network layer below it we saw that in the case of UDP that de multiplexing is made on the basis of simply the destination port number and we saw that in TCP it's done on the basis of a four tuple and then finally we learn that multiplexing and demultiplexing happens in all layers of the protocol stack so we're gonna see this again when we get to
the network layer and also when we get to building Clair [Music]