CN Lab 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Department of Computing

EE353: Computer Networks

Name: Muhammad Ali Usman

Class: BESE-11A
CMS ID: 332608

Lab 2: Socket Programming for Linux

Date: 16-2-2022

Instructor: Dr. Arsalan Ahmed


Lab Engineer: Syed Muhammad Ali Musa

EE353: Computer Networks Page 1


Lab 2: Socket Programming for Linux

Socket Server Example:


This server continuously runs and sends the date and time to client as soon as a client
connects to it.
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n",
ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
}
}

 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.

EE353: Computer Networks Page 2


 The second argument ‘SOCK_STREAM’ specifies that the transport layer protocol that
we want should be reliable ie it should have acknowledgement techniques. For example
: TCP

 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.

Socket Client Example:

#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>

EE353: Computer Networks Page 3


#include <arpa/inet.h>
int main(int argc, char
*argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
}
}
if(n < 0)
{
printf("\n Read error \n");
}
return 0;
}

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.

EE353: Computer Networks Page 4


 Information like IP address of the remote host and its port is bundled up in a structure
and a call to function connect() is made which tries to connect this socket with the
socket (IP address and port) of the remote host.

 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.

Compiling and running C files on ububtu


 Open text editor in Ubuntu and paste server code and save with c extension
 Create another c file name Client .c and paste client code in it.
 Now open two terminals run server.c code on first terminal and client.c on the
other.
 Following are the terminal commands to compile and Run server side C code

 g++ server.c -o serv.out


for executing
 ./serv

 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:

Output of the server code:

EE353: Computer Networks Page 5


Output of the client code:

EE353: Computer Networks Page 6

You might also like