So if you see that in this step, we have filled the data structures sock adr underscore u n with the
identity of the Unix domain socket.
The Unix domain socket is identified with the address family, which is a of underscore Unix and the
name of the socket.
Right?
So in line number 52 and 53, we have actually filled this structure with the identity of the Unix domain
socket.
Now the next step is to call the bind system.
Call.
Now what does actually bind system call do?
The purpose of the bind system call is that the application that is this server process is dictating
the underlying operating system.
That is, this application is running over the operating system and using bind system call.
This application is actually telling the operating system the criteria of receiving the data.
Now what does that mean?
See, your server process will going to receive the data which was sent by the client and that client
is also running on the same machine.
So here, Bind System Call is telling the operating system that if a sender process so sender process
is a client process which will run on the same machine.
So the bind system call is being used by this server process to tell the operating system that if sender
process sends the data which is destined to the socket with the name, the following that is demo socket.
Remember the name of our Unix domain socket is demo socket.
Right.
So if send a process that is the client process sends the data which is destined, which is destined
to the socket which has this name, then such data needs to be delivered to this server process, right?
So here actually the server process is dictating the operating system, the criteria of the data that
it wants to receive.
So let me elaborate a little bit more on it.
So here you have the application layer and on the application layer you have a server process running
as and on the same machine you have a client process C, which is also running.
And because and you know that the application layer runs on the top of the operating system, right?
So using bind system call, the server process is telling the operating system that I'm interested in
receiving the packet which is assigned to the socket name demo socket.
So let me just write DS that stands for demo socket.
Now, if the client C sends the data to the server and in order to send the data, the client C uses
the name of the socket as demo socket that is DS, Then operating system will take the client data and
it will redirect that data to the server process.
S why it is redirecting that data to the server process.
S Because the server process s has told the operating system using bind system call that it is actually
interested in receiving the data which is sent by the client process.
C And the client process C has sent the data destined to the socket.
Demo socket.
So remember when the client C sends the data to the Unix domain socket, the client C has to specify
the name of the Unix domain socket in order to send the data.
So this is the functionality of bind system call.
So you can see the synopsis of the bind system call.
The first argument to the bind system call is actually the master socket file descriptor which we have
already created as in the first step.
The second argument to the bind system call is actually the pointer to the structure which we have already
filled with the credentials or identity of the Unix domain socket.
So you can see that in the second argument of the bind system call, we are actually.
We are actually supplying the identity of the Unix domain socket that is the name of the Unix domain
socket.
And the third argument, you just have to pass the constant value, which is nothing but the size of
this structure.
So it is actually the size of the second argument that you have passed to the bind system call.
So this is the entire purpose of bind system call.
So going forward, if the bind system call returns minus one, it means that for some reason the bind
system call has failed.
Right.
So by line number 68, the bind system call is succeeded.
Now let us discuss what is the next step.
The next step is the listen system call.
Now, what does listen system call do?
Let us try to understand that.
So you can see that the server process in the next step has made a call to the listen system call.
The first argument to the listen system call is the master socket file descriptor, which you have already
created.
And in the second argument you need to pass some numeric value.
Now what is this numeric value?
This numeric value means that if 20 client sends the data to this server process at the same time,
the operating system will queue the request or data of those 20 clients and deliver those requests to
our server process one by one for processing.
If more than 20 clients requests or data arrives to this server process, the operating system will
going to drop those requests or data from extra clients.
So this is what lesson system call does using lesson system call.
Our server process is telling the operating system to maintain a queue of maximum size 20.
And again, if listen, system call fails, then it returns minus one.
And in case of failure, then we just exit.
Now, in the next step, you can see that the server process is making a call to the accept system call.
Right.
So before making a call to the accept system call, the server has to enter into the infinite loop because
all server process usually runs 24 into seven.
So it is a property of a server process that good servers are those which are always usually up 24 into
seven.
So good servers should always up and running and should never go down.
Have you ever seen Facebook or Google page fail to load because of their respective servers are down?
No.
Right.
So it is a common practice.
That server process is that server logic is always encapsulated inside an infinite loop, right?
So once we enter into the infinite loop, you can see that our server process will print that.
It is waiting for the accept system call.
So the synopsis of the system call is simple that in the first argument you just have to pass the master
socket file descriptor, which is also called as connection socket.
And the second and third argument for Unix domain socket, you can simply pass as null null.
Their discussion is not worth for this course.
So remember the return value of accept system call is actually a communication file descriptor or data
socket using this data socket or communication file descriptor.
This server will going to exchange the data with the client for the rest of the lifetime of the connection.
So remember the accept system call is first of all a blocking system call.
Now, what does that mean?
It means that the moment your server process invokes the accept system call the server process execution
will get blocked.
That is, it will not execute line number 92.
And it will stay blocked until the server process receives the connection initiation request from the
client.
It means that by invoking the accept system call, our server process is now waiting for some client
to get connected with server process.
So therefore accept system call is actually a blocking system call.
Blocking system calls are those system calls which, when executed, gets blocked until certain events
occur.
So in this case the accept system call will stay blocked until some client sends connection initiation
request to our server process.
So let us suppose that there exists some client on the same machine and that particular client has sent
the connection initiation request to our server.
So the moment our server will receive the connection initiation request, this accept system call will
return and it will return us the data socket that will be used to carry out communication with the client,
which is just connected to the server.
So the code execution will now proceed.
So the moment the client gets connected to our server successfully in line number 97, our server will
print.
That connection has been accepted from the client.
So remember the functionality of a server that it will keep on doing the summation of the values of
the non-zero values that are sent by the client which is connected to the server.
Now the server is again entering into the infinite loop.
Right.
It is entering into this infinite loop because our server will going to accept all the integer values
that is being sent by the client and it will going to process all those values and keep calculating
the summation of those values unless until the client sends zero.
So it is for this reason that our server is now entering into the infinite loop again.
Now remember, our server has to accept some data from the client and to accept that data our server
process need a memory.
So buffer is the memory pointer and using mem set a server process is actually.
Removing any garbage value that was left in this buffer for some reason.
Right.
So in the next step, you can see that our server process is now actually invoking the read system call.
The synopsis of the read system call is that it Is that the first argument that is passed to the system
call is actually the communication file descriptor.
So remember.
It is the data socket that is used for carrying out actual data exchange with the client.
Therefore, the job of the master socket file descriptor was over at the accept system call because
master socket file descriptors are used only to accept and create new connections with the new client,
whereas actual data exchange happens on the data socket, which is actually the return value of the
accept system call.
So all this theory we have already covered in the basics of socket programming design and when we were
discussing the design of socket programming.
So read System Call is again a blocking system call.
It means that the execution of this server program will halt at this line and it will stay halt or blocked
at this line until the client that has just connected to our server has actually sent some data to our
server.
So the moment the connected client sends the data to our server, our server will read that data into
this buffer which is passed as the second argument to the read system call.
And and after receiving the data, the read system call will get unblocked and the execution of this
program will proceed further.
The third argument to the read system call is actually the size of the buffer, which is passed as the
third argument to the read system call.
Right.
So the return value of read system call is the number of bytes that is read or received by the server.
So if the return value of the read system call is negative, it means some error has taken place and
our server process is just terminating in case of some error happens.
Now remember that that our server process is actually expecting only the integer values from the client.
So buffer is actually a memory and only integer values are expected to receive from the client.
So it is for this reason that we are actually copying only four bytes of memory.
That is size of int is four bytes.
So we are copying only the integer value that is received in the buffer into the data type variable.
Now this data type variable is actually an integer type variable.
So basically we are copying the integer value that is received from the buffer into the integer type
variable.
And now we are checking that if data is zero, then we are just break.
That is, our server process will come out of the inner loop.
Otherwise, if the data value that is the integer value that is sent by the client is not zero, then
remember that our server process will keep on doing the summation of the values that is sent by the
client.
Right.
So let us suppose that the client has sent a non-zero value.
It means that our server process will execute this line.
That is, it will do summation and a server process will iterate once more inside the inner infinite
loop.
It will prepare the buffer again in order to receive the next data from the client and our server process
will again get blocked on the read system call.
That is, our server process is now waiting for the next data item from the client.
So you can see that our server process is kind of an infinite loop and it will continue to wait for
the next data item from the client until the client sends you, until the client sends zero.
As the value to the server, right?
And therefore you can see that a server process checks that if the client has sent the value zero to
our server, then we will break.
So we will be in line number 123.
When a client has sent a value zero to our server.
So the next thing that our server is supposed to do is to actually send back the result.
And what is the result?
The result is the summation of all values that a server process has computed so far.
So using printf command, we are actually preparing a formatted string.
And this formatted string will be the result that our server process will send back to the client.
Right.
So in line number 124, our server process has actually prepared the result that is supposed to be sent
back to the client.
So in line number 127, our server process is actually sending back the result to the client.
So it is done using right system call.
So the synopsis of the right system call is exactly same as the read system call.
The only thing to note here is that that the right system call is a non-blocking system call that is
write system call, execute immediately and it do not blocks for any reason.
So in line number 127, our server has actually sent back the result to the client successfully.
Right.
And after sending the result to the client successfully, our server process has actually closed the
connection with the client.
Right.
So it means that our server process is done with this client and now our server process should be in
a position to entertain the new client.
So in line number 135, we are actually going to iterate the outer infinite loop once again.
Right.
So the server process will now again invoke the accept system call and remember, accept system call
is a blocking system call.
So our server process is now actually waiting for the new connection request from the new client.
So you can see that this explains the entire state machine or of our of our server process.
You are requested to type out these lines on your own machine and please do not copy paste.
Just type out these lines and try to understand the logic of this server program and try to understand
the synopsis of different system calls we have used in this server program so that you get a fair idea
and practice regarding how to implement a Unix domain server process.
So I have explained all the steps that are required to implement a Unix domain server process.
So we have just discussed all the steps that are required to implement a Unix domain server process.
Now the next we will going to discuss the steps required to implement a client process.
Now note that the implementation of client process is very simple and straightforward.