So, guys, this slide shows the state machine diagram of a TCP server.
So we will going to discuss all the steps of implementing a typical TCP server.
The steps that we will going to discuss will completely in compliance with this state diagram.
We have already had a discussion of this state diagram.
So let's have a code walk through and see what are the different steps of implementing a TCP server.
So you can see that you need to follow these ten steps to implement a typical TCP server.
We will go through these steps one by one and see how these steps are implemented in socket programming.
So guys, let's have a code walk through.
So guys, I have written a file called TCP Server dot C, You can see here is a file, TCP server dot
C, and in this file I have implemented the functionality of a TCP server.
I will share this code with you in the resource section.
So let's open this file and see how TCP server are implemented.
So let's start with the main straightaway.
Right.
So let's start with the main function.
In the main function, you see that I do nothing, but I only call one function that is setup TCP server
communication.
So inside this function I implement the entire functionality of a TCP server.
So let's discuss this function.
So here is our start of this function.
So the step one is the initialization phase.
As you can see, the step one is only initializing the variables.
So in the step one, we have taken all the required variables that we will need in order to implement
a Dhcp server.
As we have already discussed that when the server boots up.
As we have already discussed, that when the server boots up, it creates a master socket file descriptor
and it places that master socket file descriptor in a set.
So we have taken a master socket file descriptor variable which is again an integer value, right?
And the rest of the variables we will discuss when we will be using them.
And we have taken a read set which is of data type FD, underscore set.
So it's a set and as our TCP server keeps on creating any file descriptors, our server will going to
add those file descriptors in this set.
Right.
And we have taken struct soc addr type variables which is server address as well as client address.
So this data type that is struct sockaddrin is used to store the machine identity.
That is the IP address and the port number of our server as well as of client.
So we will see how we make use of these variables.
Right.
So you can see in the step one, we need to create a master socket file descriptor and place it in read
set.
You can see that in the second step, we need to create a master socket file descriptor.
Right.
So let's come to the step two here and you can see that straight away we have created a master socket
file descriptor.
So master socket file descriptor is created using socket system call socket system call accepts three
arguments.
The first is the address family because we are dealing with IPv4 addresses and therefore we need to
create IPV four sockets.
So the first argument defines that.
What is the address family you are using?
So for IPV four networks always pass AF underscore Inet.
If it is, if you are working with IPV six addresses then.
You should have passed af underscore inet six.
So the second argument that you should pass is the socket stream.
If you want to create a TCP socket.
Then the second argument has to be socket stream for UDP sockets.
This value should be soc d gram.
Right.
And.
Then the third argument defines the protocol that you want to run on the top of the network layer.
So on the top of the network layer, we are trying to run the TCP protocol.
So the TCP protocol is identified using this constant IP, proto TCP.
So these all are so these socket stream and proto TCP both actually combined together to identify the
TCP protocol.
So in order to create a TCP socket, you need to pass the second argument to the socket system, call
as socket stream.
And the third argument has to be IP proto TCP.
Right.
And if you wanted to create a UDP socket instead of.
TCP socket.
Then the second argument would have been sock underscore d gram.
And the third argument would have been IP, proto UDP.
Right.
So after the second step, you will get the master socket file descriptor.
So after step two, the creation of master socket file descriptor is done.
Now the third step is the bind system call.
So bind system call is here.
This is the step three bind system call means that.
Your server process.
That is, this Dhcp server is actually conveying to the underlying operating system the criteria.
Of packet that the server wants to receive.
Right.
So you can see that the first argument of the bind system call is the master socket file descriptor
that you have created in the step two.
The second argument to the bind system call is actually the server identity, that is the server IP
address and port number.
So you can see here that the server has filled this variable that is server underscore addr.
With some information.
So let us see.
What is that information?
The first information that is filled in this structure is the SIM family.
As I said, that for IPV four networks you have to pass a f underscore inet.
Right.
So our server is actually our TCP server and therefore our server needs to run on a port number.
So here we have filled the server port number in server underscore addr dot sin port member just assign
the value port number of a server.
So this server port number is actually a hash defined constant and its value is 2000.
You can see it's a constant and its value is 2000.
Right.
Then the third thing that you have to fill up in this structure is the server IP address.
Now.
Here you can pass a particular IP address in integer format.
For example, if your server is interested in receiving a packet in which the destination IP address
is specified is the server IP address and that IP address happens to be 192.168.56.101.
It means that this is the IP address of a server itself.
So the integer equivalent of this IP address is this value.
So you had to pass this particular value to this member.
But here you can see I have passed something else which is called n underscore n.
So what does that mean?
It means that suppose you have a server machine.
And your server machine may have interfaces, the multiple interfaces, right.
And each interface may have the IP address like 2.1.1 slash 24.
2.1.2.1 slash 24.
And 2.1.3.1 slash 24.
Right.
So in any means, any IP address belong to any local interface of this server.
So whenever the server receives a packet whose destination IP address is equal to.
IP address of any local interface of this server, then this server will going to process that packet.
Right.
And instead of in any had you passed the integer equivalent of IP address?
2.1.2.1 that is this IP address.
Then only the packet with destination IP address.
2.1.2.1.
Then only the packets with destination IP address.
2.1.2.1 would have been delivered to your server process running in the application layer by the socket
layer.
So basically server underscore ADR is a structure which which is filled with the identity of the server
machine.
That is the IP address.
And the port number and because your server machine can have multiple IP addresses.
Which is the address of each interface of the server.
So it is the server choice that which IP address it wants to receive the packet with.
So if you pass in addr underscore any, it means that the server is interested in receiving the packet
with the destination IP address equal to IP address of any local interface of the server.
So as long as the IP address in the packet is equal to IP address of any local interface of this server,
the socket layer of the TCP IP stack will go in to deliver the packet to the server process running
in the application layer.
Right.
So this is something that you should understand.
Then you need to have a constant like underscore Len is equal to size of struct socket ADR.
So we will see these how this constant is used.
So here we have the step three where the bind system call is called.
The first argument to the bind system call, as I have said, is the master socket file descriptor.
And the second argument of the bind system call is the server information.
That is, the server is conveying its information to the operating system that I am a server and I am
interested in receiving the packet with IP address.
2.1.2.1 for example.
Right.
And you can see that we have filled this structure with the port number also.
So server is also conveying the operating system that I am interested in receiving a packet in which
the TCP port number is specified in the transport header is 2000.
Right.
So basically with bind system call, the server is conveying to the operating system.
Remember that the server is running in the application layer and the operating system is running below
the application layer.
So the server is conveying the information to the operating system that I am a server with IP address,
blah blah blah.
And if you receive a packet with IP address, blah blah blah and port number x, y, z then then please
deliver such packets to me.
So this is the concept of bind system call.
Then the next step is the listen system call.
So through listen system call the server process tells the operating system to maintain a queue of max
length.
Five because we have passed five as the second argument to the lesson system call and with lesson system
call the server process.
The server process is instructing the underlying operating system to maintain a queue of length five.
So what happens that it might be possible?
That the server may receive connection requests or data requests from client C1.
Client C2.
Client C3 and client say C9.
So you can see that the server may receive a connection request or data request from any number of clients
at the same time.
So when the request arrives at the same time, then the operating system will maintain a queue of length
five and because the nine requests are arrived at the same time, then the operating system will queue
only five of them and will drop the rest for.
Right, because you do not want the system on which the server process is running to get overwhelmed
or overloaded with large number of simultaneous requests from large number of clients.
So this is the purpose of listen system call.