So guys, in this slide, we will going to study the state machine for socket based client server communication.
Now understanding the state machine is important because once you understand the state machine, you
will be able to easily understand the code to implement socket based communication between the client
and the server process.
Second thing is that you will be able to picturise the implementation of client server design in your
mind.
So you can see in this diagram here you have a server process and the blank server process actually
represents that.
The server process is not aware that how to do socket programming.
So let us try to understand that, how actually the server process state machine is designed.
So you can see that the very first thing your server does that is when the server boots up.
That is, as soon as you run the server process, the very first thing the server is supposed to do
is to create a connection socket.
Now connection socket is also called as master socket file descriptor.
And this master socket file descriptor is created using socket system call.
So you can see that the first thing your server does is to create a master socket file descriptor.
And this master socket file descriptor is created using socket system call.
So now you know what is the use of socket system call?
The socket system call is used to create a master socket file descriptor.
Now why?
Actually, it is called master socket file descriptor.
Is it blessed with some special privileges as compared to other socket?
Let us try to understand this point.
Now suppose there is a client C1.
A client?
C1 could be a process that is running on the same machine, or it could be running somewhere else on
other machine in the network.
So you can see that as soon as you have a client.
C1 And client C1 wants to set up a communication with the server, the very first thing the client C1
does is to send a new connection initiation request to the server.
Right.
And you can see that as soon as the new connection initiation request is received by the server, the
operating system diverts such request to the master socket file descriptor, which is created by the
server.
So remember master socket file descriptor always receives the new connection initiation request from
the new client.
So in the next step, as soon as the server receives the new connection initiation request from the
new client, the next thing that the server does is to create a client handle so you can see that server
has created a handle.
C1 For the client.
C1.
Now what exactly handle is handle is nothing, but it is a connection identifier of the client.
C1 That is, the server knows that in order to carry out data exchange with the client.
C1 it has to use the handle.
C1.
So handle is nothing, but it is just a representation of a connection with the client.
Going forward.
Suppose there is another client, C2, that pops up from somewhere and this client, C2, also send
a new connection initiation request to the server.
The operating system that is running on the server diverts the new connection initiation request always
to the master socket file descriptor.
The master socket file descriptor.
Then create a new handle for the client C2.
Right.
So you can see that there will be as many handles, as many clients are there, and server has to maintain
a database of such handles which corresponds to the connected clients.
So you can conclude from here that M that is the master socket is actually the mother of all client
handles because it is the master socket which gives birth to the client handles.
Client handles are also called as data sockets.
So these are data sockets.
Right.
Now going forward.
Now, at this point of time, both the client C1 and C2.
Has successfully established a connection with the server.
Now they are in the position to send a second type of message called service request message to the
server.
So you can see that the client C2, has actually sent a service request message to the server.
When the server operating system receives the service request message from the connected client, the
operating system diverts service request message.
Always to the handle which corresponds to that client.
So you can see that the service request message from the client, C2, is actually diverted or received
by the handle corresponding to the client C2 only.
And once the server receives the service request message, it can now process the service request message
and response back to the client.
This response is called Service response.
Right, because at the end of the day, the server has to fulfill the request of the client and that
is what the goals of servers are to provide services to the clients.
So you can see that once the client handles are created for each client server carries out communication.
That is actual data exchange with the client using client handle.
So note that here the purpose of master socket file descriptor was to create only client handles.
Master socket file descriptor is not used to process service request messages received from the client.
That is a job of the corresponding client handles or data sockets.
Right.
And here you can see that because there are two clients which are connected to our server.
Therefore, our server had to create two client handles.
So basically our server has to maintain a database of active client handles.
Right?
So going forward, that is the master socket file descriptor is only used to create new client handles.
M is not used for any normal data exchange with already connected clients.
So note that this point, this is the speciality of the master socket file descriptor that they are
not used for data exchange with the client master socket file descriptors are used only to establish
new connection with the new clients.
Right now.
Now, next step is that except system call is used on the server side to create client handles.
So how actually the server creates the client handles server, creates a client handles using accept
system call.
And the argument to this accept system call is the master socket file descriptor.
Right.
So the end goal of except system call is to.
Complete the process of connection establishment between the server and the client.
So the first goal of accept system call is to complete the connection establishment process between
the server process and the client process.
And the second goal of Accept system call is to create a client handle using which the server can carry
out future data exchange with the client.
So in Linux terminology, you can see that handles are actually called file descriptors, which are
just positive integer numbers.
Right.
So when I talk about client handles, one client handles C2, they will be actually positive integer
numbers.
For example, it could be six, it could be seven.
In fact, master socket file descriptor could be nine and so on.
So end of the day, the file descriptors are nothing but integer numbers.
Client handles are called communication file descriptors or data sockets, and M is called master socket
file descriptors or, or connection sockets.
So now you have an understanding that how actually socket based communication between server and several
clients takes place.
Right for each particular client.
The server creates a temporary handle and it puts that handle in its database.
Here database could be any data structure which could store multiple client handles and let it be array,
link list, tree or anything.
Another thing to note here is that here operating system is doing one important task.
That is it is doing the task of diversion.
Diversion means when connection initiation request arrives from the client, the operating system diverts
that request to the master socket file descriptor.
And if service request message arrives from the already connected client, then the operating system
diverts those requests to the corresponding client handles.
So you will understand all these concepts better when you will be actually writing a socket based communication,
which will definitely cover as a part of this module.