So by line number 140, the server has actually received the data to be processed from the client.
Now the server has to understand that what data the client has actually sent.
So we have assumed here that the that suppose the client has sent the data.
Of the type test struct t that is, the client has sent two unsigned integer variables.
A and B, right.
So the server will typecast the data that is received from the client and to the same type of structure.
So that our server can read the data that is sent by the client.
So remember, the server needs to know that what data the client has sent.
So here in step nine, we are checking the spatial condition that the moment the client sends both the
integer values as zero, then the server will treat this as a spatial condition and the server will
going to close the connection with the client.
The moment the server sees that the client has sent both the values as zero zero.
Right?
So let us suppose in this example that the client has not send both the values as zero.
So what will the server do?
The server will do nothing, but it will process the data that is sent by the client and send back the
result to the client.
So our server is a very simple server.
It has the functionality that whatever data it receives from the client, that is whatever values of
A and B it receives from the client or server will just calculate the sum of these two values and reply
back the sum to the client.
Right.
So the server has taken a variable called struct result.
Right.
So let us see the definition of this structure, which is very simple.
It will just going to store the result that is the addition of values A and B that is sent by the client.
Right.
So here in line number 154, the server is calculating the sum of values of A and B and storing the
result in.
Result struct t structure.
And once the server has finished processing the data that is sent by the client, the server is then
invoking the send to system call because the server is now wants to send the result back to the client.
So remember, the send to system call is also invoked on the communication socket file descriptor because
the server is now communicating with the client and carrying out data exchange with the client.
The second argument to the send to system call is actually a pointer to the reply buffer that the server
is sending back to the client.
And the third argument is the size of the buffer that the server is sending back to the client.
And the fourth argument is always null.
You can assume the fifth argument is again the client identity to which the server is carrying out this
communication.
Right.
And this structure was already filled by accept system call.
And the final argument is nothing but it is a size of struct socket structure.
So basically using send to system call our server has send the reply back to the client.
And the return value of the send to system call is again number of bytes that is sent by the server.
So the server is simply printing the number of bytes sent.
Now the server has sent the result back to the client and you can see that the server from here goes
back to the start of the inner one loop, right?
It means that the server, after sending a response to the client is now again prepares the data buffer
and now again waits for the next data from the client.
Right because it will invoke the receive from system call again.
So basically our server is an infinite loop.
That is our server.
After sending back the response to the client or server again waits for the next data request from the
client and so on.
So our server is an infinite cycle of communication with that client.
Now, let us suppose that this particular client wants to terminate a connection with our server.
So to do so, let the client sends the value of A and B as zero to the server.
So here you can see that the server will detect that the values of A and B that is sent by the client
are zero.
So the server closes the connection with the client and the server comes out of the inner loop.
So you can see.
So where will the control go after this break?
The control will go to the begin of the outer, while one loop that is the server will drain out the
read set.
The server will again populate the read set with the master socket file descriptor and the server is
now blocked on the select system call.
It means that the server is now done with the previous client and now the server is waiting for the
connection initiation request from another client.
Now that is the server has finished servicing the previous client and now the server is waiting for
the new connection request from the new client.
The new client could be as same as the previous client, but the older client now has to establish the
connection afresh with the server.
So you can see that this is the server which can handle only one client at a time because the server
was spending its time inside the inner while one loop and once it respond back to the client with the
result, the server again comes to the start of the inner while one loop and again gets blocked on the
receive from system call.
That is, our server is now working as a slave to the client.
It receives the client data, it processes the data and send back the result to the client.
And it again blocks itself to receive the new data from the client.
And this cycle continues forever unless the client sends the value of A and B as zero.
When the server receives the value of A and B as zero, the server closes this connection with this
client and the server goes to the start of the outer, while one loop where the server itself gets blocked
in the select system call that is now the server is ready to service the new client.
So here we have discussed the step number nine and ten also that is the server closes the connection
of the client when the server receives the value of A and B as zero zero.
And once it closes the connection, the server goes to the step five.
That is the start of the outer while one loop.
Right.
So we have discussed all the ten steps of implementing a typical TCP server.
In the next example, we will improve the design of this server and we will discuss the implementation
of a TCP server which can handle multiple clients at the same time.
That is, our server can.
Process the requests from multiple clients at the same time and can reply back to these clients at the
same time.
In this particular design, we see that our server could not service or entertain the new client unless
it is done with the current client it is carrying out communication with.
So this is a big drawback of this Dhcp server design.