So, guys, next we will going to discuss the implementation of Unix domain sockets on the client side.
So you can see in this diagram that the client state machine is pretty much very simple.
As soon as the client process starts up, the first thing that the client process do is to create a
socket file descriptor.
Right now, there is no concept of master socket file descriptor using socket system call only.
The client creates a communication file descriptor and using this communication file descriptor, the
client carries out all the data exchange with the server and the second step.
Once the client has created a socket, the client simply sends connection initiation request to the
server and this is done using connect system call.
So once the connect system call succeeds, the client can now carry out actual data exchange with the
server right?
So you can see that the client steps are pretty much simple and straightforward.
So let's have a code walk for the client implementation.
So guys, we have opened a file client dot C, so client dot c file is the file that implements the
client functionality.
So in line number nine, you can see that we have defined a socket name.
So you can see that this socket name is same as the socket name, which we have used while creating
a server process.
So the client process will use this socket name to send data to this socket.
Right.
And of course the client needs a memory buffer size in order to send the data to the server process
as well as receiving the result from the server process.
So you can see that in line number 123.
The first thing that the client processes do is to create a Unix domain sockets so you can see that
a client has created a Unix domain socket in the same way the server does.
Right.
The difference here is that on the client side, the socket file descriptor that is that is returned
by the socket system call is actually a data socket and not connection socket.
So data socket is a socket file descriptor which is used for actual data exchange.
So there is no concept of master socket file descriptors on the client side.
So the socket file descriptor that is returned by the socket system call itself is a communication file
descriptor and the client uses this data socket for carry out actual data exchange with the server.
Right.
So in line number 23, the client process has created a socket for communication.
Right now in the next step that is in line number 36.
You can see that the server is actually populating the structure called struct sock underscore UN.
And you can see that a client process is doing the same thing.
Which server did the client process is actually specifying the.
Identity of the Unix domain socket to which it wants to carry out communication.
So the first argument is the Unix domain socket family, which is AF underscore.
Unix.
And the second argument is actually the socket name using which the client will send the data to the
server.
So in line number 40 and 41, the client process knows that which server it wants to communicate with.
Right.
Now, in the next step, the client has to send the connection initiation request to the server.
Right.
So Linux provide a system called called connect.
Using connect system call.
The client sends the connection initiation request to the server.
Now the first argument to the connect system call is the data socket.
Right.
Which is returned by the socket system call.
The second argument is again the pointer to the structure which stores the information of the server
socket to which the client wants to send data.
And the third argument is a constant value, which is the size of this structure type.
So Connect system Call is not a blocking system call.
If there exists no server running, then the connect system call will actually return minus one immediately.
Right.
So Connect System call is not a blocking system call.
Now, remember, the functionality of our client process was that it continued to send the integer value
to the server.
So here we are entering into the loop, and in this loop, the client process will prompt the user to
enter the integer value.
And as soon as the user enters the integer value, our client process will send that value to our server.
So using a write system call again, our client process will send value to our server.
So to send a value to the server, our client process invokes the write system call.
The first argument to the right system call is the data socket file descriptor.
The second argument is a pointer to the buffer, which actually stores the data to be sent to the server.
So in this case, the our client process has to send only a single integer value to the server.
So you can directly pass the address of the memory location which actually stores the integer value.
And the third argument is the size of the data that the client process is sending to the server.
So you can see that when the user enters the value zero, the client process sends that value to the
server process.
And this while loop then terminates because the value of I is zero.
So it means that our client process will continue to send the value to the server until the user enters
zero.
Now our client process will execute line number 65 only when the user has entered the value zero as
an input.
So it is time for the client process now to receive the result from the server.
So it is for this reason that our client process has invoked the read system call.
Read system call is a blocking system call.
It means that the client process will get blocked on line number 66.
And it will stay blocked until it receives a reply from the server.
Right.
So let us assume that the client process has received the data from the server.
So in line number 72, the client process is simply printing the result or the message that is received
from the server.
So you can see that our client process is printing the result as it is, and after that our client process
simply closes the connection with the server and exit.
So you can see that the state machine of the client process is pretty much very straightforward and
simple.
So the client specific system calls.
Here is Connect system call.
It is invoked only on the client side and this connect system call is used to send the connection initiation
request message to the server.
And another thing to note here is that the client should know the identity of the server with which
it wants to communicate with.
So in line number 41, you can see that a client process knows the name of the socket, which actually
is the identity of the server Unix domain socket.
So our client process knows that which server it wants to communicate with because in the connect system,
call the second argument that you passes is actually the name of the Unix domain socket created by the
server, and the client should know the name of this Unix domain socket in order to establish the communication
with the server process.
So next, let us see the demonstration.
That is, we will execute the server and the client process and see how they communicate with each other.