So welcome back, guys.
So next, we will discuss the steps to implement a TCP client.
So you can see that you need to implement the following eight steps in order to implement a TCP client
program.
So implementing a TCP client is a way much simpler than implementing a TCP server.
We will have a code walk and we will cover all these steps regarding how to implement a TCP client.
So you can see the following eight steps includes the initialization of variables.
Then specify the server credentials.
That is, the client program must know the server IP address and port number in order to connect with
the server process because the IP address will uniquely identify the server machine that could be running
anywhere in the network and the TCP port number will help identify a server process uniquely running
on that machine.
Right.
So once the TCP client knows that which server machine it needs to connect with the TCP client, then
create a communication socket.
And using this socket, the client machine carries out all the data exchange with the server.
Then in the fourth step, the client after creating a socket, sends the connection initiation request
to the server.
Right.
So after fourth step, the connection between the client and the server is fully established.
And after that, in the step fifth and step sixth, the TCP client can exchange its data with the TCP
server and after the client has finished all sorts of data exchange with the server, the TCP client
can choose to close the connection and if not, that is the TCP client wants to communicate with the
server and wants to exchange more data with the TCP server.
Then from step eight, the TCP client can go directly to the step five.
Right.
So this is a very simple state machine for a TCP client.
We will have a code walk and see how these eight steps are implemented as a TCP client program.
So let us go through these steps one by one.
So the TCP client functionality is implemented in TCP client dot C file.
I will share the path to this file so that you can download this file and execute it on your machine.
So I will share this code in the resource section.
So let's go through this file.
So let's start with the main function.
So the entire TCP client functionality is implemented inside the function setup TCP communication.
So let us go inside this function and see what we have implemented there.
So we have taken a local variables inside this function which we will be using to implement a TCP client
program.
Right.
So we will discuss the use of these variables when we will be actually using them.
Right.
And at this LAN is a constant value, which is nothing, but it is a size of struct socket.
DDR Right, Right.
So we don't really have anything to discuss in the initialization phase.
Then the second step is specify the server credentials.
That is, the TCP client should know that which server it wants to connect.
So credentials involve the TCP server IP address and the port number.
So you can see that in order to save the server IP address and port number, we have taken the variable
of the type struct sockaddrin, which is the standard C structure provided by C APIs, of course.
Right.
And inside this structure we will specify the server credentials.
So we need to fill the three members of this structure.
The first member is the Syn family.
That is, it is the type of address family that we will be using since we are implementing IPv4 sockets.
Therefore, you need to pass here a f underscore inet.
Right?
If you are using ipv6 sockets then you would have passed a f underscore inet six.
The next thing that you need to specify is the port number of the server.
So the port is nothing, but it is the server port number.
So you can see that the dest port has been initialized to value 2000.
Right.
So we have specified the server port number and the next thing that we need to specify is the server
IP address.
So the next two lines, that is the line number 40 and the line number 41 are used to specify the server
IP address.
So server IP address.
In this case we have taken as 127 .0.0.1.
So this is a special IP address that is.
It is a current machine IP address.
On which you will be executing this program.
Right.
So basically your server will run on IP address.
127.0.0.1 as well as your client program will go and connect with the server on this IP address.
So basically your server and client programs are actually running on the same machine.
Right.
You can run the client and the server programs in a separate terminals on your virtual machine.
And and for that you need to run the server with IP address.
127 .0.0.1.
Right.
So you can see that we have specified the server port number as well as the server IP address.
You should use this API in order to get the.
In order to specify the server IP address as it is.
Right.
So deaths in ADR eventually stores the server IP address in the integer format.
Then the next step is to create a communication socket.
So our client should now create a communication socket in order to carry out communication with the
server.
So in order to create a communication socket file descriptor, use a socket system, call.
The first argument that you will pass is the address family.
And since we are creating a TCP client, therefore the second and third argument has to be socket stream
and IP proto TCP.
Second argument actually identifies the type of socket that you are creating.
So you are creating a TCP socket.
So specify the socket stream and the transport layer protocol that you are trying to use is the TCP
protocol.
So the identifier of the TCP protocol is IP, proto TCP.
So these are actually the constants and it is again provided by the network APIs.
Then in the fourth step, you need to connect with the server.
Right.
Because our TCP client has now created a socket and now it is in a position that it can send a connect
request.
That is the connection initiation request to the server.
Because our server is now aware of the IP address and port number of the server to which it wants to
connect.
So the next step is nothing but simply our TCP client can send the connection initiation request to
the server using connect system call.
And you can see in the connect system call the first argument is the socket file descriptor, which
is created in the previous step.
The second argument contains the credentials of the server to which the client wants to connect.
That is the IP address and the port number.
And the third argument is just a constant which is size of this struct socket structure.
Right.
So after Connect System Call is invoked, the connection between the TCP client and the TCP server has
been established.
Now our client is in a position to exchange data with the TCP server.
So you can see that in the step five, our TCP client can send data to the server and in step six.
Our TCP client can actually receive the data from the server.
Then in the next step, the TCP client actually prompts the user to enter the values of A and B, so
the value of A and B serves as the TCP client data, which the TCP client will send to the server for
processing.
Right.
So once the user enters the value of A and B, the TCP client uses the send to system call in order
to send the data to the server for processing.
So you can see that in the send to system call.
The first argument is the communication file descriptor, which the TCP client has created using socket
system call.
And the second argument is the structure which carries the value of A and B.
The third argument is the size of the data that you are sending to the server.
And the fourth argument you can always assume as null.
The fifth argument is the credentials of the server.
That is the identity of the server to which the client is trying to send the data.
Right.
And the last argument is nothing but it is the constant value which is size of struct socket structure.
So you can see that the TCP client has sent the data to the TCP server using send to system call the
return value of send to system call is actually the number of bytes that the TCP client has sent to
the server.
Right now.
After sending the data to the server, the TCP client should wait for the response from the server.
For that, the TCP client invokes the receive from system call and remember receive from system call
is a blocking system call, meaning that the client program will not run past this point until the data
arrives on the socket from the server.
So the TCP client blocks here and it will stay block here unless the data from the TCP server arrives.
That is the response from the TCP server is received.
So you can see in the receive from system call.
The second argument is the memory buffer in which the response that will be received from the server
will be copied.
And who will copy this response?
The transport layer has the responsibility to copy the server response into this buffer.
Right.
So the return value of the receive from system call is again the number of bytes that is received from
the server.
And in the last we are just printing the value that is received from the server.
So remember the functionality of our TCP server was to receive the value of A and B from the client.
Add these two values and return the summation of these two values back to the client.
So here we are simply printing the result that is received from the Dhcp server, which is the addition
of values A and B, and after that our TCP client wants to communicate further with the TCP server.
That is, it wants to send another two values of A and B to the server.
So for that our TCP client goes inside the loop using go to system call.
You are free to implement this loop using for or using while loop or whatever way you want.
So again, the TCP client prompt the user for two values and after getting these two values, the TCP
client uses the send to system call and send these two values again to the server the server received.
These two values computes the result and sends the result back to the TCP client.
The TCP client receives the response using receive from system call.
And again, the TCP client goes back and again prompt the user for another two values and so on.
So you can see that the steps for implementing a TCP client is fairly easy.
Right in the TCP client.
You notice that we had not specified anywhere the TCP port number to be used by the TCP client.
So there is.
So there is no port number assignment by the programmer.
Remember when we created the TCP server, the user that is the programmer had to explicitly specify
the port number on which the TCP server is supposed to listen.
But this is not the case for TCP client.
The reason is that that the TCP client, when sends the correct system call to the server, the operating
system automatically assign one TCP port number to our TCP client.
And that value is dynamically chosen by the operating system.
So you will see when we will have a demo of this program, You will see that operating system dynamically
assigns a TCP port number to the TCP client.
And second thing to notice here is that there is no concept of bind system call on the TCP client side,
right?
And also there is no need to maintain any read set or any file descriptor set on the client side.
And also you notice that we had not used any file descriptor set in this code block.
The reason is that our TCP client is carrying out communication with only one TCP server, right?
If it was the case that our TCP client was talking to multiple TCP servers at the same time, then our
TCP client would have to maintain multiple socket file descriptors, each one of which is used to carry
out communication with one TCP server.
Right.
So it's not like that that Dhcp client do not have to use file descriptor set.
In fact, any machine, whether it is a client or it is a server, if they are communicating with other
multiple machines, then that machine has to maintain more than one communication file descriptors.
And in order to do that, that machine has to use file descriptor set.
And you can also notice that there is no concept of accept system call.
The accept system call is meant only for TCP servers to accept the client initiation request.
So guys, now let's have a demonstration of this Dhcp client and Dhcp server program that we have discussed.