So guys, we are done until the lesson system call.
Then the next step that we need to discuss is initialize and fill the set.
So you can see that we are done with the lesson system call Now server enters into the infinite loop
for servicing the client.
Now server has the property that they are always has to be in the position to service the client.
So server has to be up 24 into seven and that is the reason that the server logic are usually implemented
inside a infinite loop.
That is while one loop.
Right now.
Remember we set.
Now, remember, Reed is nothing, but it is a set of file descriptors.
So using the macro FD underscore zero, this is the standard macro which is provided by.
C library so we can use this macro to empty or drain out the read set.
That is, we are initializing the file descriptor set.
After that, we are adding only the master socket file descriptor to this set.
So fd underscore set is the macro we should use.
And this macro will add master socket file descriptor to the set.
Right.
So coming to the line number 91, we are now invoking the select system call.
Right.
So in so by the time we reach line number 91, the read set contains only one file descriptor and that
is master socket file descriptor.
Right now.
Let us discuss the synopsis of the Select system call.
The synopsis of the Select System Call says that the first argument of the select system call should
be one plus X.
One plus x, where x is the maximum value that is present in the read set.
Right.
So this is the first argument of the select system call.
So here you can see that in the read set, the only file descriptor that is present is the master socket
file descriptor.
So x is equal to master socket file descriptor.
So the first argument is master socket file descriptor plus one.
So such is the synopsis of the select system call.
Right.
And the next three arguments of the select system call, you can pass as null, null, null.
We don't need to discuss the rest of the three arguments of the select system.
Call in this course.
So remember that the Select system call is a blocking system call.
The moment the select system call is invoked, this process or the execution of this program halts at
line number 91.
You can see that the Linux operating system keeps this process blocked until the data arrives on any
of the file descriptors present in the data set.
Now, in this example, the only file descriptor that is present in the read set is the master socket
file descriptor.
And master socket file descriptors are used to detect new connection requests from new clients.
So it means that the select system call is monitoring only the master socket file descriptor and because
only the master socket file descriptor is being monitored, it means that the select system call is
blocked until the new client connection request arrives to this server.
Right.
So our server is blocked in line number 91.
Now, let us say at some point of time in future, some new client has sent the connection initiation
request to our server.
When this happens, our server process will get unblocked from line number 91 and the execution of this
program resumes.
So in line number 94, we are checking using the macro FD underscore is set.
So this macro is used to test which file descriptor present in read set is activated.
Now, in this example, the only possible file descriptor that could be activated is the master socket
file descriptor.
Therefore, this if condition will always be true.
When we will be discussing the more complicated implementation of Dhcp server.
In the next example, then you will see that FD underscore is set is a useful macro and it is necessary
for us to check that the select system call is unblocked because which file descriptor is activated.
So we need to find that particular file descriptor which is activated.
So in this example, this looks pretty much obvious that we are checking the master socket file descriptor
that we are checking whether is it the master socket file descriptor that is activated.
And it looks obvious because in read set, the only file descriptor that is present is master socket
file descriptor.
So anyway, when this condition is true, it means that our server has received new connection requests
from the new client.
Right in this diagram.
We are here and we are checking that is master socket file descriptor activated.
So in this example, yes, the master socket file descriptor is activated.
So the next thing that a server needs to do is to establish the connection with the client by calling
the accept system call.
Right.
So we are on step seven now that our server is now invoking the Accept system call and after the accept
system call is invoked, then the connection with the client is supposed to be established.
So the return value of the accept system call is the communication socket file descriptor and using
this communication socket file descriptor, our server will carry out all the future communication with
the client.
So basically this communication file descriptor represents the connection with this particular client
which has just connected to our server.
The second argument of the accept system call is the.
Empty structure.
We discussed it when we discussed the synopsis of the XRP system call.
And as soon as the client comes and gets connected to the to our server, this structure gets filled
up with the IP address and port number of the client.
Right.
So now our server knows that with whom it is talking to.
Right.
So our server will use this information to reply back to the client because at the end of the day our
server needs the client IP address and port number to send the packet back to the client.
And the third argument is a constant, which is the size of struct sockaddr structure.
So when the accept system call fails for some reason it returns a negative value.
So we are just checking here that if the accept system call fails, then then just terminate the server
process.
Right.
So we are here.
And at this point of time, the connection with the client is established.
So our server process simply printing the client identity.
So it is printing the client IP address as well as the client port number.
So the C APIs provides us this inbuilt functions that is init and to a it is used to print the IP address
in a dot b dot c dot d format.
Right.
And we are printing the port number of the client.
So you can see that in these steps we are finished till step seven.
That is, we have invoked the select system call and waited for the client connection request and the
server has accepted the connection of the client.
Now the next thing that our server needs to do is to wait for the data request from the client, and
when it receives the data request, it processes the data of the client and accordingly it reply back
to the client with the result.
So let us see how Step eight is implemented.
Now the server is again entering into the infinite loop because our server will going to talk to this
client indefinitely until or unless the client sends some special data to the server.
And by analyzing that spatial data, our server then chooses to terminate the connection with the client.
Right.
So at that point of time, we will just break out of this loop.
So let us see that.
How?
Server Exchange the data with the client.
So the first thing that the server does is to prepare a memory.
In which a server can store the data that is sent by the client.
Right.
So once the data buffer is now ready to fill up with the data that is received from the server, the
server now invokes the receive from system call.
Now receive from system call is again a blocking system call.
If you discuss the synopsis of the received from system call, the first argument to the receive from
system call is the communication file descriptor.
So remember all the data exchanged between the server and the client happens on the communication file
descriptor and not on master socket file descriptor.
The second argument to the receive from system call is the.
Data buffer in which the server will going to store the data that is sent by the client.
The third argument is the size of this data buffer.
That is, how many bytes long is this data buffer?
The next argument you can always pass as null.
We don't need to discuss this argument and.
Fifth argument is actually the client information which the accept system call had filled with the client
information.
And the last argument is again the size of struct structure.
So when the server invokes the receive from system call, it means that the server is now waiting for
the data request from the client specified in this argument.
Right.
And unless the server receives some data from this client, the server will stay block on receive from
system call.
So basically at this point of time, that is in line number 125.
Our server is now actually waiting for some data to arrive from this client.
Right now.
Let us suppose that the client has actually sent some data to our server.
So when our server will receive the data from this client, our server will going to get unblocked from
the receive from system call.
The return value of the receive from system call is the number of bytes that is sent by the client.
So here we are just printing that the number of bytes that is received from the client is these many
bytes and we are printing the client IP address and the port number.
Now here we have taken it as a special condition that in case the client sent us the zero number of
bytes, then the server will assume it as a termination condition.
That is, the server will conclude that the client do not want to communicate with the server anymore.
So the server closes the connection with that particular client and the server comes out of the inner
while loop.
But in this case, let us suppose that the client has really sent some data to the server.
So the data that is sent to the server is actually stored in the data buffer.
Right now.