So guys, let us have a code walk of our TCP server with multiplexing so you can see that we will start
with the main function and the entire TCP server functionality is implemented inside the function setup
TCP server communication.
So let's go inside that function and see what we have implemented there.
Right.
So because now you already aware of TCP server design and implementation.
So I will go fast through the steps which we have already discussed in, in the discussion of previous
TCP server design.
So this is the initialization phase where we will initialize all the variables that we will need to
use in the implementation of this Dhcp server.
And here we have taken our data structure which will store the communication file descriptors of all
the connected clients, right?
And in order to store the server and client information that is IP address and port number, we have
again taken the two structures that is server underscore ADR and client underscore ADR.
Right.
Now what does the TCP server do?
The very first thing that the very first thing that this Dhcp server do is to initialize, monitor,
underscore, set.
So let us see what this function does.
So you can see this is the implementation of this function.
So this function does nothing but it simply initialize the monitored file descriptor set array to minus
one.
Minus one is here.
Minus one represents the absence of communication file descriptor in this array.
So remember when we discussed the high level design, our TCP server s was maintaining this array,
which.
Which was used to store the communication file descriptor as well as master socket file descriptor.
So it is this array which we are talking about here.
This array is called.
So let us call this array as monitored file descriptor set.
So we have initialized this array with minus one, meaning that at this point of time the server do
not have any file descriptor to be monitored.
Right.
So we have just initialized this array in this function.
Right.
So the second thing that Dhcp server does is to create a master socket file descriptor, right?
We have already discussed this.
Then the TCP server uses the structure server underscore addr to specify its own credentials.
That is its TCP port number as well as its IP address.
Now, in the third step, the TCP server calls the bind system call using which it conveys its information
to the underlying operating system that in what type of packet or TCP server is interested.
So our TCP server is interested in receiving a packet in which the destination port number in the transport
header is equal to the server port number.
And the IP address specified in the IP header of the packet is equal to IP address of any local interface
of this server machine.
Right.
So these steps are not different from the steps which we had discussed in the.
Discussion of previous TCP server.
These are exactly same.
And then you have a lesson system call.
Right.
So at this point of time, that is by line number 136 or TCP server has created one file descriptor
and that is called master socket file descriptor.
So remember, as soon as our server creates any new file descriptor, whether it is a master socket
file descriptor or communication file descriptor, it has to add that file descriptor to this array.
Right.
So we have called this function add to monitor file descriptor set and we have passed the file descriptor
that server has created that is master socket file descriptor.
So using this function we are adding master socket file descriptor to the array.
So let's go inside this function and see.
So you can see the implementation of this function is fairly, very simple.
We are iterating over the array and as soon as we find the empty slot in the array, which is represented
by minus one value in that particular slot, we add the file descriptor, which is to be monitored to
that array.
Right.
So simple implementation.
So before entering the while one loop, our TCP server has booted up and it has created a master socket
file descriptor and it has added the master socket file descriptor to this array.
So you can see here that our TCP server is here and it is about to enter into the while loop to make
a call to the select system call so that it can monitor the file descriptor it is supposed to monitor.
So you can see as soon as the server enters into the infinite loop it re init read data set.
Now what does this function do?
This function does nothing.
It just creates a clone or a copy of this monitored file descriptor set array and it copy this array
into the read set because at the end of the day to the select system, call the data structure that
you need to pass is the read set.
So simply your read set has to be exactly the clone or the copy of your monitored file descriptor set
array.
So re init read set is the function that does this job.
So let us see the implementation of this function that what does this function do?
So this function does nothing but whatever red set you passed as an argument to this function, it simply
empty that red set first and then it iterate over the monitored file descriptor set array.
Right?
And for all the file descriptors that are present in that array, it simply copies all those file descriptors
from the array to the red set.
So basically this function is creating a copy or the clone of monitored file descriptor set array to
the red set.
Right, because monitored file descriptor set array contains all the file descriptors at any point of
time to be monitored by our TCP server.
And those file descriptors also needs to be present to the set.
Right.
So after copying and populating the read set with the required file descriptors.
Now it is a time for our TCP server to actually call the select system call.
So you can see that the select system call has been called on the read set, which contains all the
file descriptor to be monitored.
But at this point of time, our TCP server has only one file descriptor in this read set, which is
master socket file descriptor and it do not have any communication file descriptor as of now because
no client has yet connected to our TCP server.
So you can see in this diagram a TCP server has invoked the select system call on the set and now our
TCP server is blocked waiting for the connection initiation request from some client.
Right now.
Let us suppose that some client C1 comes and sends the connection initiation request to our TCP server.
So when this happens, our Dhcp server will get unblocked from the select system call and in the read
set the master socket file descriptor will be activated.
Right.
So we will check here that the master socket file descriptor is activated in the read set.
It means that our server has received a new client connection request and it is a time for our server
to actually accept the connection and create the communication file descriptor for that particular client.
So you can see in this diagram, as the client C1 sends the connection initiation request to TCP server
or TCP server invokes the Accept system call and creates the communication file descriptor.
Using this communication file descriptor, TCP server will be carrying out all the future communication
with the client C1.
Right now.
As I explained, the high level design that whatever communication file descriptor or TCP server creates,
it has to store those communication file descriptor into the monitored file descriptor set array.
Right.
So it's time to invoke this function and it will just add this communication file descriptor of the
client, which has just connected to our TCP server to the array.
Right.
And we have already discussed the implementation of this function.
It simply adds the file descriptor, which is passed as an argument to this function into the empty
slot of the array.
Right.
So at this point of time, the client C1 has just connected to our TCP server and our TCP server has
created the communication file descriptor for that client.
And as I said, that our TCP server will use this communication file descriptor to carry out all the
data exchange with this client.
C1 Right, And also this communication file descriptor has been added to the array, right?
So you can see that in the read set at this point of time.
Master socket file descriptor was already present.
Now the communication file descriptor is also present.
Right.
So remember, we are adding the communication file descriptor to the array and in the beginning of the
while one loop.
We were copying the entire array to the FDR set, so it is as good as you are.
Adding the communication file descriptor to the FDR set.
Right.
So, so far what we have seen that the client C1 has just come and it is connected to our server.
Our server has created the communication file descriptor for this client and our server has added this
communication file descriptor to the FDR set.
So the else part will not run.
And then we will go to the one loop that is our server will now go back to the while one loop.
So our server re-enters or reiterate the infinite loop that is while one loop and it calls re init read
set that is here.
It needs to refresh the read set.
So remember earlier in the previous iteration the read set contained only master socket file descriptor.
And now, because our server has established the connection with the new client, so our server now
has two file descriptors to monitor.
The first one is the master socket file descriptor and the second file descriptor is the communication
file descriptor of client c one.
So re init read will take care to re copy the new set of file descriptors to be monitored and to read
set right and then our server again blocks at the select system call.
And now at this point of time our server is monitoring the new data request from the client C one,
which is already connected to our server and our server is also in a position to process new connection
initiation requests from new clients.
Why?
Because it is monitoring master socket file descriptor as well as communication file descriptor of client
C one.
So again, the same thing repeats our TCP server gets unblocked from the select system call.
If either the client C1 sends data request to the TCP server or the client C2 sends connection initiation
request to our TCP server so our TCP server gets unblocked if any of these two events happen.
So you can see here in case if the new connection initiation request comes from the client C2, then
this condition will be true.
Right, because it is the master socket file descriptor that will be activated.
Right.
And if new data requests arrive from the client C1 which is already connected to our server, then else
part will execute.
Right, because.
Any file descriptor other than master socket file descriptor will be activated.
Right.
So when else part hits, then we need to find out that actually which communication file descriptor
is activated because there could be many clients that could be connected to the server at a given point
of time.
So for that we need to iterate over the monitored file descriptor set array and check that exactly which.
Client has sent the data request to our server so we can do this by iterating over the array.
Monitor file descriptor set array and check using a macro underscore is set and see that if this particular
file descriptor has been activated in the read set.
Right.
So if condition will be true only for those communication file descriptors which is activated in the
read set.
Right now.
The next thing that TCP server has to do is to receive the data from the client whose communication
file descriptor is activated and reply back the response.
So this is again, the same logic that you have already learned in the previous TCP server.
Prepare the data buffer.
Receive the data from the client.
Right.
And if the client suppose if the client has sent zero bytes of data to a TCP server, then our TCP server
can close the connection with that client.
And the moment our TCP server closes the connection with that client, then our TCP server need not
monitor this communication file descriptor in future invocation of select system calls.
And that is the reason that we need to remove this communication file descriptor from our monitored
file descriptor set array.
Otherwise, if the client has send a legitimate data to our TCP server to process, then our TCP server
will typecast the data into the client data.
That is, our TCP server.
Should know that in what format the client has sent the data.
Right.
And our TCP server simply adds the values that is sent by the client and calculate the summation of
the two values and send back the response to the client.
So this is the same logic as receiving the request from the client, processing the request and sending
back the result to the client.
So you can see that this entire logic is inside this for loop.
That is, we are checking all the activated communication file descriptors in the set and whatever communication
file descriptors are activated.
That means more than one client has sent data to our TCP server or TCP server is processing the data
request one by one from those clients and sending back the response.
So this is the entire logic of multiplexing is actually implemented in the else part.
So going forward in this diagram, you can see if the client C2 has sent the connection initiation request
to our TCP server.
Our TCP server will accept the connection.
We'll create another communication file descriptor and we'll add this communication file descriptor
to the set and it will use the respective communication file descriptor in order to carry out data exchange
with the corresponding clients.
So this is the complete state diagram of a TCP server with multiplexing.
And this diagram shows that how TCP server with multiplexing capabilities actually work.
So you can take the help of this code and I request you to write your own TCP server with multiplexing.
It will give you a good practice.
Don't just sit and watch this code, otherwise you will not feel confident at writing TCP server or
doing socket programming.
And upload your all codes on GitHub.
Of course I will share the paths to all these codes in the resource section for you.
So this was Dhcp server with multiplexing.
Next we will see the demonstration of Dhcp server with multiplexing and see that how Dhcp server can
communicate with multiple clients at the same time.