Welcome back, guys.
So next, we will going to discuss a new design of a TCP server.
And that design is called a TCP server with multiplexing.
So such a TCP server is called multiplexed TCP server.
So in the previous TCP server design, we discussed the limitation of the TCP server that a TCP server
cannot entertain any other client as long as it is not finished with the current client it is communicating
with.
Right.
But servers are not supposed to entertain only one client at a time.
The usual property of the server is that that it should be able to entertain multiple clients at the
same time.
So therefore it is important to understand the implementation of TCP server with multiplexing that is,
servers which could entertain and which could carry out communication with multiple clients at the same
time.
So you can see there are ten steps to implement the Dhcp server with multiplexing.
If we compare these ten steps with the ten steps of the TCP server that we implemented and discussed
previously, you can find that these ten steps are exactly same as those ten steps that we discussed,
right?
So we will see in what respect the multiplexed Dhcp server implementation is different from the previous
TCP server implementation.
So let me discuss the high level idea regarding implementing a TCP server with multiplexing.
So you have a server, say S, and suppose you have a client.
C1 Initially when the server boots up, it maintains only one file descriptor and that file descriptor
is called master socket file descriptor.
Right.
We have discussed this master socket file descriptor enough now.
So master socket file descriptor is used to detect new client connection requests.
Right?
So our TCP server is actually monitoring only the master socket file descriptor in the select system
call.
Now suppose the client C1 sends the connection initiation request to the server s, then the server
s creates a new communication file descriptor using which it can carry out communication with the client.
C1 So at this point of time the server s is maintaining two file descriptors the master socket file
descriptor and the communication file descriptor of client C1 Right.
Now suppose at some point of time that is later client C2 comes and it sends connection initiation request
to the server s so server s will create a communication file descriptor for the client C2 and it will
carry out communication with the client.
C2 using that communication file descriptor.
So you can see that every time a new client gets connected to the server, the server creates a communication
file descriptor and keeps an array of those communication file descriptors.
Right.
So our server is able to carry out communication with multiple connected clients at the same time because
our server is keeping track of the communication file descriptor of all the connected clients.
So this data structure could be as simple as an array, right?
So every time a new client gets connected to our TCP server, our server will create the communication
file descriptor of that particular client and add that communication file descriptor in the array.
Right.
And every time a select system call is called by the TCP server, it will copy this entire array into
read set.
It will copy.
And remember, the select system call is used to monitor all the file descriptors which are present
in read set.
Right.
So using select system call or TCP server is actually monitoring all the file descriptors of all the
connected clients.
Right.
And this is how our TCP server will be able to do multiplexing with multiple clients.
So I am just describing you a high level design that how TCP server with multiplexing capability can
be implemented in C.
When we will have a code walk, you will have a clear idea that how this particular idea is implemented
in network programming.
Also note that suppose the client c to terminate its connection with the server s then what will server
s will do?
It will simply delete the communication file descriptor for the client C2 from the array.
It will delete.
Right.
So in the read set, when this array will be copied in the read set, only the communication file descriptor
of connected clients will be present.
Right.
So it means that the server s will not be communicating with the client C2 anymore.
So let us discuss the code walk of TCP server with multiplexing and see how it differs from the previous
TCP server that we discussed.
So by now you have a fair idea that how TCP server with multiplexing capabilities actually works.