So going forward, we will be going to discuss the Accept system call. so we will going to write two applications
Client.C
And server.C
Client.C actually represent a client C and server.C actually represent a TCP server
So in file client.C, we will going to implement the functionality of the client
that is the client will going to initiate the request and
In server.C file, we will going to implement the functionality of the TCP server that is receiving the client connection request
receiving the client data
Processing the data and replying back to the client
We will going to implement server functionality in server.C file and client functionality in client.C files
Now let us discuss the accept system call
First of all accept system call is used by the TCP server
that is accept system call is used on the server side and
What is the purpose of accept system call? the accept system call is used to accept the new client connection request by the server
So you have a client C and you have a server S
The first thing that the client has to do is to establish the connection with the server
For that the client has to send the connection initiation request
So this is connection initiation request
In connection initiation request, the client says that hey I want to establish the TCP connection with you
So it says this information to the server
So the accept system call on the server side is used to accept the new client connection request
This is the purpose of accept system call
So remember TCP is a connection-oriented protocol
That is client and server need to establish a dedicated connection first
Before they can participate in any data exchange activity
Right, so the first thing that the client and a server needs to do is to establish bi-directional TCP connection
Before any data exchange happened between the client and the server
The client and the server has to establish the bi-directional TCP connection between them
the initiation of this connection is done by the client and
Client does this by sending the connection initiation request to the server. The
Accept system call on the server side is actually used to process the connection initiation request triggered by the client
Let us say, for example when you login into Facebook, your machine sends connection initiation request to Facebook servers
Facebook servers accept your connection request using accept system call
Now, you can send data to Facebook server and Facebook servers can update your page with notification and feeds
So the sole purpose of accept system call is just to establish the connection
between the two machines that is client machine and server machine and
It is done by carrying out TCP three-way handshake
So TCP connection is established
By using 3-way handshake protocol
Now again, the details of the 3-way handshake protocol is out of the scope of this course
If you study the TCP internal you will also study what is 3-way handshake protocol.
so again, I advise you or refer you to the
Standard textbook store to know about the internals of the TCP. For now,
The bi-directional TCP connection setup between the client and the server
requires a three-way handshake mechanism
So the except system call actually helps in achieving 3-way handshake
Mechanism
between the client and the server
So except system call is called only on the server side and it returns the handle to the connection with the client
So except system call first of all
except system call is used only on server side and it is not used on the client side and
Second thing is that when the server invokes the accept system call, the accept system call
Returns the handle to the connection with the client
Now what is handle? handle is nothing but it is just an integer value say 6
So 6 actually represent a dedicated connection between the client and the server
Using this handle
Server carries out all the future communication with that client
this handle is called communication file descriptor. why it is called communication file descriptor? because
actual communication between the client and the server
after the connection has been established is done using
this handle
That is why it is called communication file descriptor
So you can see in the diagram you have a client and the TCP server is running somewhere in the network
The first thing the client does is to send a connection initiation request to that TCP server
So this is the step 1 client sends the connection initiation request using connect system call
therefore connect system call is used only on the
Client side. when the server receives the connection initiation request from the client,
The first thing that the server does is to accept the client connection request using accept system call
As soon as the accept system call is invoked the accept system call returns communication file descriptor
Which is nothing but an integer value
Using this communication file descriptor the server carries out the data exchange with the client
So guys let us discuss the synopsis of the accept system call
so let us revise the accept system call returns a file descriptor, which is called communication file descriptor and
Which is just an integer value. In fact in Linux, every file descriptor is represented as integer
Right. So this communication file descriptor actually represents a dedicated connection
between
client and the server
Server carries out all the communication with the client using this returned integer value, which we call as communication file descriptor
So above you can see given the synopsis of the accept system call
the accept system call accepts three arguments
The first argument is the master socket TCP file descriptor, which the server has already created
when it boots up
The second argument to the accept system call is the pointer to the structure of type struct sockaddr
this structure is defined by standard c api
so the second argument that we pass to the accept system call is
actually an empty structure
Empty structure means we do not fill it with any data
when the client's connection request arrives
to the server and
When the server invokes the accept system call in order to accept,
The client connection requests the second argument to the accept system call
That is this structure gets filled up with the client identity
now what is client identity? The client identity is the IP address of the client as well as the
TCP port number of the client
Right. So the whole purpose of the second argument of the accept system call is to retain the client
identity
When the server receives and accepts the client connection request.
using this information the server can reply back to the client because the server
Remembers the client information that is IP address and port number
The third argument to the accept system call is actually a constant value
So this is the synopsis of accept system call
So let us have a formal look of the arguments that is passed to the accept system call
the master socket TCP file
descriptor is the file descriptor of the socket which server has opened to listen to new client connection request and
This master socket file descriptor was created using socket system call
so remember the purpose of master socket file descriptor is to listen to
new client connection requests from new clients
Client addr that is the second argument that is passed to the
Accept system call is the structure which will carry the client information
So as soon as the client connects to the server and the server
accepts the client connection request, the second argument to the accept system call gets filled up with the client IP address and
TCP port number
Right
The third argument to the accept system call is the size of predefined standard structure
that is the size of the struct sockaddr and
Therefore, the third argument is constant
So there are some more APIs like send to and receive from but their explicit discussion is not worth
When we will have a code discussion regarding how to implement a TCP client server program
then there itself we will discuss the send to and receive from APIs as
The name suggests these APIs are used for sending and receiving a data by the client as well as server process
So here we have just discussed the synopsis of the accept system call and how accept system call should be used
by the server in order to accept the client connection requests from new clients and
Remember the return value of accept system call is the communication file descriptor
Which is used by the server to carry out the rest of the data exchange or future communication with this client