CN Lab 2
CN Lab 2
CN Lab 2
Class: BESE-11A
CMS ID: 332608
Date: 16-2-2022
The call to the function ‘socket()’ creates an UN-named socket inside the kernel and
returns an integer known as socket descriptor.
This function takes domain/family as its first argument. For Internet family of IPv4
addresses we use AF_INET.
The third argument is generally left zero to let the kernel decide the default protocol to
use for this connection. For connection oriented reliable connections, the default
protocol used is TCP.
The call to the function ‘bind()’ assigns the details specified in the structure ‘serv_addr’
to the socket created in the step above. The details include, the family/domain, the
interface to listen on(in case the system has multiple interfaces to network) and the port
on which the server will wait for the client requests to come.
The call to the function ‘listen()’ with second argument as ’10’ specifies maximum
number of client connections that server will queue for this listening socket.
After the call to listen(), this socket becomes a fully functional listening socket.
In the call to accept(), the server is put to sleep and when for an incoming client request,
the three way TCP handshake* is complete, the function accept () wakes up and returns
the socket descriptor representing the client socket.
The call to accept() is run in an infinite loop so that the server is always running and the
delay or sleep of 1 sec ensures that this server does not eat up all of your CPU
processing.
As soon as server gets a request from client, it prepares the date and time and writes on
the client socket through the descriptor returned by accept().
Three-way hand shake is the procedure that is followed to establish a TCP connection
between two remote hosts.
Finally, we compile the code and run the server.
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
In the above program, we create a client which will connect to the server and receive date
and time from it. In the above piece of code:
We see that here also, a socket is created through call to socket() function.
Note that here we have not bind our client socket on a particular port as client generally
use port assigned by kernel as client can have its socket associated with any port but In
case of server it has to be a well-known socket, so known servers bind to a specific port
like HTTP server runs on port 80 etc. while there is no such restrictions on clients.
Once the sockets are connected, the server sends the data (date+time) on clients socket
through clients socket descriptor and client can read it through normal read call on the
its socket descriptor.
We can see that we successfully got the date and time from server. We need to send the IP
address of the server as an argument for this example to run. If you are running both server
and client example on the same machine for testing purpose, use the loop back ip address
as shown above.
Now one second terminal comple and execute the client as shown below.
g++ client.c -o cli.out
for executing
./cli.out 12.0.0.1
Task: