SlideShare a Scribd company logo
1 | P a g e
Submitted To:- Submitted By:-
Sir Rohit Negi Uttam Singh Chaudhary
(17EMBIT055)
2 | P a g e
List of Experiments
S.no Experiment Name Page
no
1 Define different types of LAN.
(LAN, MAN, WAN)
4-6
2 Define and describe all the network
Equipment/Devices.
7-12
3 Define and describe different types of
network topology Star, Ring, Bus and
Mesh.
13-17
4 Write a program in c to implement
various type of framing method.
 Bit stuffing
 Byte stuffing
 Character Stuffing
18-27
5 Write a program in C to implement
Various routing algorithm.
 Simple redundancy check
 Cyclic redundancy check
 Checksum
 Hamming code
28-42
3 | P a g e
6 Write a program in C to implement
routing algorithm.
 Distance vector routing
 Link state routing
43-54
7 Write two program in C Hello_Client
and Hello_Server.
 The server listens for and accepts a
single TCP connection. It reads all
the data it can from that connection
and print it to screen then it close
the connection.
 The client connects to the server
sends the string ”COMPUTER
NETWORK” then closes the
connection.
55-66
4 | P a g e
Experiment:-1
Aim: Define different types of LAN. (LAN, MAN,
WAN)
LAN: Local Area Network
A LAN connects network devices over a relatively short
distance. A networked office building, school, or home
usually contains a single LAN, though sometimes one
building will contain a few small LANs (perhaps one per
room), and occasionally a LAN will span a group of
nearby buildings. In TCP/IP networking, a LAN is often
but not always implemented as a single IP subnet.
LANs are also typically owned, controlled, and managed
by a single person or organization. They also tend to use
certain connectivity technologies, primarily Ethernet and
Token Ring.
5 | P a g e
WAN: Wide Area Network
As the term implies, a WAN spans a large physical
distance. The Internet is the largest WAN, spanning
the Earth.
A WAN is a geographically-dispersed collection of
LANs. A network device called a router connects
LANs to a WAN. In IP networking, the router
maintains both a LAN address and a WAN address.
A WAN differs from a LAN in several important
ways. Most WANs (like the Internet) are not owned
by any one organization but rather exist under
collective or distributed ownership and management.
WANs tend to use technology like ATM, Frame
Relay and X.25 for connectivity over the longer
distances.
6 | P a g e
MAN: Metropolitan Area Network
A large computer network which extends to a city or to a
large university campus is termed as metropolitan area
network or MAN. The purpose of MAN (Metropolitan
Area Network) is to provide the link to the internet in the
long run. A MAN (Metropolitan Area Network) usually
incorporates a number of LANs to form a network. This
large network MANs (Metropolitan Area Network)
backbone comprises of an optical fiber set-up.
7 | P a g e
Experiment:-2
Aim:- Define and also describe different types of
network Equipment /devices.
Different networking devices:
Network Hub:
Network Hub is a networking device which is used to
connect multiple network hosts. A network hub is also
used to do data transfer. The data is transferred in terms of
packets on a computer network. So when a host sends a
data packet to a network hub, the hub copies the data
packet to all of its ports connected to. Like this, all the
ports know about the data and the port for whom the
packet is intended, claims the packet.
8 | P a g e
However, because of its working mechanism, a hub is not
so secure and safe. Moreover, copying the data packets on
all the interfaces or ports makes it slower and more
congested which led to the use of network switch.
Network Switch:
Like a hub, a switch also works at the layer of LAN
(Local Area Network) but you can say that a switch is
more intelligent than a hub. While hub just does the work
of data forwarding, a switch does 'filter and forwarding'
which is a more intelligent way of dealing with the data
packets.
So, when a packet is received at one of the interfaces of
the switch, it filters the packet and sends only to the
interface of the intended receiver.
9 | P a g e
For this purpose, a switch also maintains a CAM (Content
Addressable Memory) table and has its own system
configuration and memory. CAM table is also called as
forwarding table or forwarding information base (FIB).
Modem:
A Modem is somewhat a more interesting network device
in our daily life. So if you have noticed around, you get an
internet connection through a wire (there are different
types of wires) to your house. This wire is used to carry
our internet data outside to the internet world.
However, our computer generates binary data or digital
data in forms of 1s and 0s and on the other hand, a wire
carries an analog signal and that's where a modem comes
in.
10 | P a g e
A modem stands for (Modulator + Demodulator). That
means it modulates and demodulates the signal between
the digital data of a computer and the analog signal of a
telephone line.
Network Router:
A router is a network device which is responsible for
routing traffic from one to another network. These two
networks could be a private company network to a public
network. You can think of a router as a traffic police who
directs different network traffic to different directions.
11 | P a g e
Bridge:
If a router connects two different types of networks, then
a bridge connects two sub networks as a part of the same
network. You can think of two different labs or two
different floors connected by a bridge.
12 | P a g e
Repeater:
A repeateris an electronic device that amplifies the signal
it receives. In other terms, you can think of repeater as a
device which receives a signal and retransmits it at a
higher level or higher power so that the signal can cover
longer distances
.
13 | P a g e
Experiment:-3
Aim: Define and describe different types of
network topology Star, Ring, Bus and Mesh
STAR TOPOLOGY:
A star topology is a topology where every node in the
network is connected to one central node. Every device in
the network is directly connected to the central node and
indirectly connected to every other node.
14 | P a g e
The relationship between these elements is that the central
network device is a server and other devices are treated as
clients. The central node has the responsibility of
managing data transmissions across the network. The
central node or hub also acts as a repeater.
In star topologies, computersare connected with a coaxial
cable, twisted pair, or optical fiber cable.
RING TOPOLOGY:
In networks with a ring topology, computers are
connected to each other in a circular format. Every device
in the network will have two neighbors and no more or no
less. The first node is connected to the last node to link
the loop together.
Ring topologies are unidirectional but can also be made
bidirectional.
15 | P a g e
Ring topologies are unidirectional but can also be made
bidirectional.
To make ring topologies bidirectional you would need to
have two connections between network nodes to form
a Dual Ring Topology. Ring topologies can sustain large
networks much more effectively than bus topologies.
16 | P a g e
BUS TOPOLOGY:-
Bus topology is a type of network where every device is
connected to a single cable which runs from one end of
the network to the other. This type of type of topology is
often referred to as line topology. In a bus topology, data
is transmitted in one direction only. If the bus topology
has two endpoints then it is referred to as a linear bus
topology. Organizations using this type of topology will
generally use an RJ45 cable to link devices together.
17 | P a g e
MESH TOPOLOGY:-
A mesh topology is a point-to-point connectionwhere
nodes are interconnected. In this form of topology, data is
transmitted via two methods: routing and flooding.
Routing is where nodes use routing logic to work out the
shortest distance to the packet’s destination. In contrast
flooding, data is sent to all nodes within the network.
Floodingdoesn’t require any form of routing logic to
work. The interconnectivity of nodesmakes them
extremely resistant to failures.
18 | P a g e
Experiment:-4
1.Bit Stuffing
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int a[20],b[30],i,j,k,count,n;
printf("Enter frame size (Example: 8):");
scanf("%d",&n);
Aim: Write a program in c to implement various type
of framing method.
 Bit stuffing
 Byte stuffing
 Character Stuffing
19 | P a g e
printf("Enterthe frame in the form of 0 and 1 :");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
i=0;
count=1;
j=0;
while(i<n)
{
if(a[i]==1)
{
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5;k++)
{
j++;
b[j]=a[k];
count++;
if(count==5)
{
20 | P a g e
j++;
b[j]=0;
}
i=k;
}
}
else
{
b[j]=a[i];
}
i++;
j++;
}
printf("After Bit Stuffing :");
for(i=0; i<j; i++)
printf("%d",b[i]);
getch();
}
21 | P a g e
OUTPUT:
Enterframe size (Example: 8):12
Enterthe frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0
0 1
After Bit Stuffing :0101111101001
2.Byte Stuffing
#include<stdio.h>
#include<string.h>
void main(){
char frame[50][50],str[50][50];
char flag[10];
strcpy(flag,"flag");
char esc[10];
strcpy(esc,"esc");
int i,j,k=0,n;
strcpy(frame[k++],"flag");
22 | P a g e
printf("Enterno.ofString :t");
scanf("%d",&n);
printf("EnterString n");
for(i=0;i<=n;i++)
{
gets(str[i]);
}
printf("You entered :n");
for(i=0;i<=n;i++)
{
puts(str[i]);
}
printf("n");
for(i=1;i<=n;i++)
{
if(strcmp(str[i],flag)!=0 && strcmp(str[i],esc)!=0)
{
strcpy(frame[k++],str[i]);
}
else
{
strcpy(frame[k++],"esc");
strcpy(frame[k++],str[i]);
}
23 | P a g e
}
strcpy(frame[k++],"flag");
//frame[k++]='0';
printf("------------------------------n");
printf("Byte stuffing at sender side:nn");
printf("------------------------------n");
for(i=0;i<k;i++)
{
printf("%st",frame[i]); }
}
Output:-
Enter no.of String : 2
Enter String
Apple
cake
You entered :
Apple
cake -
--------------------------
Byte stuffing at sender side:
------------------------------
flag Apple cake flag
24 | P a g e
3.Character Stuffing
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int i=0,j=0,n,pos;
char a[20],b[50],ch;
clrscr();
printf("enter stringn");
scanf("%s",&a);
n=strlen(a);
printf("enter positionn");
scanf("%d",&pos);
if(pos>n)
{
25 | P a g e
printf("invalid position, Enter again :");
scanf("%d",&pos);
}
printf("enter the charactern");
ch=getche();
b[0]='d';
b[1]='l';
b[2]='e';
b[3]='s';
b[4]='t';
b[5]='x';
j=6;
while(i<n)
{
if(i==pos-1)
{
b[j]='d';
26 | P a g e
b[j+1]='l';
b[j+2]='e';
b[j+3]=ch;
b[j+4]='d';
b[j+5]='l';
b[j+6]='e';
j=j+7;
}
if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e')
{
b[j]='d';
b[j+1]='l';
b[j+2]='e';
j=j+3;
}
b[j]=a[i];
i++;
j++; }
27 | P a g e
b[j]='d';
b[j+1]='l';
b[j+2]='e';
b[j+3]='e';
b[j+4]='t';
b[j+5]='x';
b[j+6]='0';
printf("nframe after stuffing:n");
printf("%s",b);
getch();
}
INPUT:-
enter string:-
asdlefgh
enter position: 8
invalid position, enter again: 3
enter the character: k
OUTPUT:-
frame after stuffing:-
dlestx as dle k dle dle dlefgh dleetx
28 | P a g e
Experiment:-5
Aim:- Write a program in C to implement Various
routing algorithm.
 Simple redundancy check
 Cyclic redundancy check
 Checksum
 Hamming code
1.Simple Parity Check:
# include<bits/stdc++.h>
# define bool int
using namespace std;
// Function to get parity of number n. It returns1
// if n has odd parity, and returns 0 if n has even
// parity
bool getParity(unsigned int n)
{
bool parity = 0;
while (n)
29 | P a g e
{
parity = !parity;
n = n & (n - 1);
}
return parity;
}
/* Driver program to test getParity() */
int main()
{ unsigned int n = 7;
cout<<"Parity of no "<<n<<" = "<<(getParity(n)?
"odd": "even");
getchar();
return 0; }
OUTPUT
Even Parity
Odd Parity
30 | P a g e
2.Cyclic RedundancyCheck
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,keylen,msglen;
char input[100],
key[30],temp[30],quot[100],rem[30],key1[30];
clrscr();
printf("EnterData: ");
gets(input);
printf("EnterKey: ");
gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
31 | P a g e
for(i=0;i<keylen-1;i++)
{
input[msglen+i]='0';
}
for(i=0;i<keylen;i++)
temp[i]=input[i];
for(i=0;i<msglen;i++)
{
quot[i]=temp[0];
if(quot[i]=='0')
for(j=0;j<keylen;j++)
key[j]='0';
else
for(j=0;j<keylen;j++)
key[j]=key1[j];
for(j=keylen-1;j>0;j--)
{
if(temp[j]==key[j])
32 | P a g e
rem[j-1]='0';
else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("nQuotient is ");
for(i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("nRemainder is ");
for(i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("nFinal data is: ");
for(i=0;i<msglen;i++)
printf("%c",input[i]);
for(i=0;i<keylen-1;i++)
33 | P a g e
printf("%c",rem[i]);
getch();
}
Output:
EnterData: 11110110101
EnterKey: 111010
Quotient is 10010010000
Remainder is 00000
Final data is: 1111011010100000
34 | P a g e
3. CheckSum
#include<stdio.h>
#include<string.h>
int main()
{
char a[20],b[20];
char sum[20],complement[20];
int i,length;
printf("Enter first binary stringn");
scanf("%s",&a);
printf("Enter second binary stringn");
scanf("%s",&b);
if(strlen(a)==strlen(b)){
length = strlen(a);
35 | P a g e
char carry='0';
for(i=length-1;i>=0;i--)
{
if(a[i]=='0' && b[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(a[i]=='0' && b[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='0')
{
sum[i]='1';
36 | P a g e
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='0' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='1' && b[i]=='0' && carry=='1')
{
sum[i]='0';
37 | P a g e
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';
}
else
break;
}
38 | P a g e
printf("nSum=%c%s",carry,sum);
for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}
if(carry=='1')
carry='0';
else
carry='1';
printf("nChecksum=%c%s",carry,complement);
}
39 | P a g e
else {
printf("nWrong input strings");
}
}
OUTPUT
Enterfirst binary string
101101
Entersecond binary string
110010
Sum=1011111
Checksum=0100000
--------------------------------------
40 | P a g e
4.Hamming Code
#include<stdio.h>
#include<conio.h>
void main() {
int data[7],rec[7],i,c1,c2,c3,c;
printf("this works for message of 4bits in size nenter
message bit one by one: ");
scanf("%d%d%d%d",&data[0],&data[1],&data[2],&
data[4]);
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("nthe encoded bits are given below: n");
for (i=0;i<7;i++) {
printf("%d ",data[i]);
}
printf("nenter the received data bits one by one: ");
41 | P a g e
for (i=0;i<7;i++) {
scanf("%d",&rec[i]);
}
c1=rec[6]^rec[4]^rec[2]^rec[0];
c2=rec[5]^rec[4]^rec[1]^rec[0];
c3=rec[3]^rec[2]^rec[1]^rec[0];
c=c3*4+c2*2+c1 ;
if(c==0) {
printf("ncongratulationsthere is no error: ");
} else {
printf("nerron on the postion: %dnthe correct
message is n",c);
if(rec[7-c]==0)
rec[7-c]=1; else
rec[7-c]=0;
for (i=0;i<7;i++)
{
printf("%d ",rec[i]); } }
42 | P a g e
getch();
}
Output:-
this works for message of 4bits in size
enter message bit one by one:0
1
0
1
the encoded bits are given below:-
0101101
enter the received data bits one by one:-0
1
0
1
1
0
1
congratulationsthere is no error:
43 | P a g e
Experiment:-6
Aim:- Write a program in C to implement routing
algorithm.
 Distance vector routing
 Link state routing
1. Distancevector routing
#include<stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int costmat[20][20];
int nodes,i,j,k,count=0;
44 | P a g e
printf("nEnterthe number of nodes : ");
scanf("%d",&nodes);//Enterthe nodes
printf("nEnterthe cost matrix :n");
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
scanf("%d",&costmat[i][j]);
costmat[i][i]=0;
rt[i].dist[j]=costmat[i][j];//initialisethe distance
equal to cost matrix
rt[i].from[j]=j;
}
}
do
{
count=0;
for(i=0;i<nodes;i++)
45 | P a g e
//We choosearbitary vertex k and we calculatethe direct
distance from the nodei to k using the cost matrix
//and add the distance from k to nodej
for(j=0;j<nodes;j++)
for(k=0;k<nodes;k++)
if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j])
{//We calculatethe minimum distance
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<nodes;i++)
{
printf("nn For router%dn",i+1);
for(j=0;j<nodes;j++)
{
46 | P a g e
printf("tnnode%d via %d Distance %d
",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("nn");
getch();
}
OUTPUT
A sample run of the program works as:-
Enter the number of nodes:
3
Enterthe cost matrix:
0 2 7
2 0 1
7 1 0
47 | P a g e
For router1
node 1 via 1 Distance 0
node 2 via 2 Distance 2
node 3 via 3 Distance 3
For router2
node 1 via 1 Distance 2
node 2 via 2 Distance 0
node 3 via 3 Distance 1
For router3
node 1 via 1 Distance 3
node 2 via 2 Distance 1
node 3 via 3 Distance 0
48 | P a g e
2. Link State Routing
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
void lsr(int G[MAX][MAX],int n,int startnode);
int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enterno. of vertices:");
scanf("%d",&n);
printf("nEnterthe adjacency matrix:n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
49 | P a g e
scanf("%d",&G[i][j]);
printf("nEnterthe starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}
void lsr(int G[MAX][MAX],int n,int startnode)
{
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
//pred[]stores the predecessorof each node
//countgives the number of nodes seen so far
//createthe cost matrix
50 | P a g e
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
//initialize pred[],distance[]and visited[]
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
51 | P a g e
while(count<n-1)
{
mindistance=INFINITY;
//nextnodegives the nodeat minimum distance
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through
nextnode
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
52 | P a g e
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
//print the path and distance of each node
for(i=0;i<n;i++)
if(i!=startnode)
{
printf("nDistance of
node%d=%d",i,distance[i]);
printf("nPath=%d",i);
53 | P a g e
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
OUTPUT:
Enterno. of vertices: 5
Enterthe adjency matrix:
10 0 30 100
10 0 50 0 0
0 50 0 20 10
38 0 23 0 63
54 | P a g e
Enterthe starting node: 0
Distance of node1 = 10
Path = 1<-0
Distance of node2 = 50
Path= 2<-3<-0
Distance of node3 = 30
Path = 3<-0
Distance of node4 =60
Path = 4<-2<-3<-0
Process returned 5(0x5) execution time : 47.471s
Press any key to continue.
55 | P a g e
Experiment:-7
Aim: Write two programs in C Hello_Clientand
Hello_Server.
 The server listens for and accepts a single TCP
connection. It reads all the data it can form that
connectionand prints it to screen then it close
the connection
 The client connectsto the server sends the
string “COMPUTER NETWORK” then closes
the connection
1.TCP SERVER:-
#include<netdb.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
56 | P a g e
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Function designed for chat between client and
server.
void func(int sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it
in buffer
read(sockfd, buff, sizeof(buff));
// print buffer which containsthe client
57 | P a g e
contents
printf("From client: %st To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != 'n')
;
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and
chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...n");
break;
}
}
58 | P a g e
}
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM,
0);
if (sockfd == -1) {
printf("socket creation failed...n");
exit(0);
}
else
printf("Socket successfully created..n");
bzero(&servaddr, sizeof(servaddr));
59 | P a g e
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=
htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP
and verification
if ((bind(sockfd, (SA*)&servaddr,
sizeof(servaddr))) != 0) {
printf("socket bind failed...n");
exit(0);
}
else
printf("Socket successfully binded..n");
// Now server is ready to listen and verification
60 | P a g e
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...n");
exit(0);
}
else
printf("Server listening..n");
len = sizeof(cli);
// Accept the data packet from client and
verification
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {
printf("server acccept failed...n");
exit(0);
}
else
printf("server acccept the client...n");
// Function for chatting between client and server
func(connfd);
61 | P a g e
// After chattingclose the socket
close(sockfd);
}
Output
Server side:
Socket successfully created..
Socket successfully binded..
Server listening..
server acccept the client...
From client: hi
To client : hello
From client: exit
To client : exit
Server Exit...
62 | P a g e
3. TCP CLIENT:-
#include<netdb.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
63 | P a g e
while ((buff[n++] = getchar()) != 'n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...n");
break;
}
}
}
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
64 | P a g e
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...n");
exit(0);
}
else
printf("Socket successfully created..n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr))
!= 0) {
65 | P a g e
printf("connectionwith the server failed...n");
exit(0);
}
else
printf("connected to the server..n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}
66 | P a g e
Output
Client side:
Socket successfully created..
connectedto the server..
Enterthe string : hi
From Server : hello
Enterthe string : exit
From Server : exit
Client Exit...

More Related Content

PPTX
PPT
Introduction to Compiler design
PPT
Compiler Construction introduction
PPTX
PDF
structure chart.pdf
PPT
Unicode
PPT
Compiler Design
PPT
Chapter 13 software testing strategies
Introduction to Compiler design
Compiler Construction introduction
structure chart.pdf
Unicode
Compiler Design
Chapter 13 software testing strategies

What's hot (20)

PPT
Compiler Design Unit 1
PPT
Software Engineering (Project Planning & Estimation)
PDF
Code Smells and Its type (With Example)
PDF
Deep Dive Into Kafka Streams (and the Distributed Stream Processing Engine) (...
PPTX
Agile methodology in cloud computing
PPTX
Error Recovery strategies and yacc | Compiler Design
PPT
User Interface Design in Software Engineering SE15
PDF
Optimizer Hints
PDF
Nepal Police(Inspector) Computer Engineer/IT MCQ questions
DOCX
Software engineering model
PDF
Lecture 01 introduction to compiler
PPTX
Introduzione a Docker
PPT
Introduction to Compiler Construction
PPTX
Phases of Compiler
PPTX
Operating system 32 logical versus physical address
PPT
Software Engineering (Testing techniques)
PPT
Requirement analysis and specification, software engineering
PDF
The CAP Theorem
PPTX
Error detection recovery
PPT
Software Estimation Techniques
Compiler Design Unit 1
Software Engineering (Project Planning & Estimation)
Code Smells and Its type (With Example)
Deep Dive Into Kafka Streams (and the Distributed Stream Processing Engine) (...
Agile methodology in cloud computing
Error Recovery strategies and yacc | Compiler Design
User Interface Design in Software Engineering SE15
Optimizer Hints
Nepal Police(Inspector) Computer Engineer/IT MCQ questions
Software engineering model
Lecture 01 introduction to compiler
Introduzione a Docker
Introduction to Compiler Construction
Phases of Compiler
Operating system 32 logical versus physical address
Software Engineering (Testing techniques)
Requirement analysis and specification, software engineering
The CAP Theorem
Error detection recovery
Software Estimation Techniques
Ad

Similar to Data Communication and Networking(DCACN) (20)

PPTX
concept of security and network administration
PPTX
Networking Fundamentals
PPT
05 - Networking Components and Devices.ppt
PPT
05 - Networking Components and Devices F.ppt
DOCX
Ccna ccnp interview question 1
PDF
Siemens Industrial Training
PDF
Understanding Networking Devices – A Brief Introduction | TechLinu.com
PPT
Networking devices(siddique)
PDF
Data communication notes
PPTX
Computer networking
DOCX
Computer networks
PDF
Introduction To Computer Networks
PPTX
Network layer u3
PPT
Networking devices
PDF
Advanced computer network lab manual (practicals in Cisco Packet tracer)
PPT
Introduction to Computer Networks Lecture slides ppt
DOCX
What is networking
PDF
Class Note
PPTX
Network the 4th layer
concept of security and network administration
Networking Fundamentals
05 - Networking Components and Devices.ppt
05 - Networking Components and Devices F.ppt
Ccna ccnp interview question 1
Siemens Industrial Training
Understanding Networking Devices – A Brief Introduction | TechLinu.com
Networking devices(siddique)
Data communication notes
Computer networking
Computer networks
Introduction To Computer Networks
Network layer u3
Networking devices
Advanced computer network lab manual (practicals in Cisco Packet tracer)
Introduction to Computer Networks Lecture slides ppt
What is networking
Class Note
Network the 4th layer
Ad

More from Uttam Singh Chaudhary (6)

DOCX
Big Data Analytics Lab File
PDF
5 Pen PC Technology
DOCX
DataBase Management System Lab File
PPTX
Traffic woes in India
DOCX
Software Requirement Specification Of Hotel Management System
PPTX
Network Topologies
Big Data Analytics Lab File
5 Pen PC Technology
DataBase Management System Lab File
Traffic woes in India
Software Requirement Specification Of Hotel Management System
Network Topologies

Recently uploaded (20)

PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Welding lecture in detail for understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
UNIT 4 Total Quality Management .pptx
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Sustainable Sites - Green Building Construction
PPTX
OOP with Java - Java Introduction (Basics)
Embodied AI: Ushering in the Next Era of Intelligent Systems
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Operating System & Kernel Study Guide-1 - converted.pdf
Welding lecture in detail for understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Mechanical Engineering MATERIALS Selection
UNIT 4 Total Quality Management .pptx
R24 SURVEYING LAB MANUAL for civil enggi
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Sustainable Sites - Green Building Construction
OOP with Java - Java Introduction (Basics)

Data Communication and Networking(DCACN)

  • 1. 1 | P a g e Submitted To:- Submitted By:- Sir Rohit Negi Uttam Singh Chaudhary (17EMBIT055)
  • 2. 2 | P a g e List of Experiments S.no Experiment Name Page no 1 Define different types of LAN. (LAN, MAN, WAN) 4-6 2 Define and describe all the network Equipment/Devices. 7-12 3 Define and describe different types of network topology Star, Ring, Bus and Mesh. 13-17 4 Write a program in c to implement various type of framing method.  Bit stuffing  Byte stuffing  Character Stuffing 18-27 5 Write a program in C to implement Various routing algorithm.  Simple redundancy check  Cyclic redundancy check  Checksum  Hamming code 28-42
  • 3. 3 | P a g e 6 Write a program in C to implement routing algorithm.  Distance vector routing  Link state routing 43-54 7 Write two program in C Hello_Client and Hello_Server.  The server listens for and accepts a single TCP connection. It reads all the data it can from that connection and print it to screen then it close the connection.  The client connects to the server sends the string ”COMPUTER NETWORK” then closes the connection. 55-66
  • 4. 4 | P a g e Experiment:-1 Aim: Define different types of LAN. (LAN, MAN, WAN) LAN: Local Area Network A LAN connects network devices over a relatively short distance. A networked office building, school, or home usually contains a single LAN, though sometimes one building will contain a few small LANs (perhaps one per room), and occasionally a LAN will span a group of nearby buildings. In TCP/IP networking, a LAN is often but not always implemented as a single IP subnet. LANs are also typically owned, controlled, and managed by a single person or organization. They also tend to use certain connectivity technologies, primarily Ethernet and Token Ring.
  • 5. 5 | P a g e WAN: Wide Area Network As the term implies, a WAN spans a large physical distance. The Internet is the largest WAN, spanning the Earth. A WAN is a geographically-dispersed collection of LANs. A network device called a router connects LANs to a WAN. In IP networking, the router maintains both a LAN address and a WAN address. A WAN differs from a LAN in several important ways. Most WANs (like the Internet) are not owned by any one organization but rather exist under collective or distributed ownership and management. WANs tend to use technology like ATM, Frame Relay and X.25 for connectivity over the longer distances.
  • 6. 6 | P a g e MAN: Metropolitan Area Network A large computer network which extends to a city or to a large university campus is termed as metropolitan area network or MAN. The purpose of MAN (Metropolitan Area Network) is to provide the link to the internet in the long run. A MAN (Metropolitan Area Network) usually incorporates a number of LANs to form a network. This large network MANs (Metropolitan Area Network) backbone comprises of an optical fiber set-up.
  • 7. 7 | P a g e Experiment:-2 Aim:- Define and also describe different types of network Equipment /devices. Different networking devices: Network Hub: Network Hub is a networking device which is used to connect multiple network hosts. A network hub is also used to do data transfer. The data is transferred in terms of packets on a computer network. So when a host sends a data packet to a network hub, the hub copies the data packet to all of its ports connected to. Like this, all the ports know about the data and the port for whom the packet is intended, claims the packet.
  • 8. 8 | P a g e However, because of its working mechanism, a hub is not so secure and safe. Moreover, copying the data packets on all the interfaces or ports makes it slower and more congested which led to the use of network switch. Network Switch: Like a hub, a switch also works at the layer of LAN (Local Area Network) but you can say that a switch is more intelligent than a hub. While hub just does the work of data forwarding, a switch does 'filter and forwarding' which is a more intelligent way of dealing with the data packets. So, when a packet is received at one of the interfaces of the switch, it filters the packet and sends only to the interface of the intended receiver.
  • 9. 9 | P a g e For this purpose, a switch also maintains a CAM (Content Addressable Memory) table and has its own system configuration and memory. CAM table is also called as forwarding table or forwarding information base (FIB). Modem: A Modem is somewhat a more interesting network device in our daily life. So if you have noticed around, you get an internet connection through a wire (there are different types of wires) to your house. This wire is used to carry our internet data outside to the internet world. However, our computer generates binary data or digital data in forms of 1s and 0s and on the other hand, a wire carries an analog signal and that's where a modem comes in.
  • 10. 10 | P a g e A modem stands for (Modulator + Demodulator). That means it modulates and demodulates the signal between the digital data of a computer and the analog signal of a telephone line. Network Router: A router is a network device which is responsible for routing traffic from one to another network. These two networks could be a private company network to a public network. You can think of a router as a traffic police who directs different network traffic to different directions.
  • 11. 11 | P a g e Bridge: If a router connects two different types of networks, then a bridge connects two sub networks as a part of the same network. You can think of two different labs or two different floors connected by a bridge.
  • 12. 12 | P a g e Repeater: A repeateris an electronic device that amplifies the signal it receives. In other terms, you can think of repeater as a device which receives a signal and retransmits it at a higher level or higher power so that the signal can cover longer distances .
  • 13. 13 | P a g e Experiment:-3 Aim: Define and describe different types of network topology Star, Ring, Bus and Mesh STAR TOPOLOGY: A star topology is a topology where every node in the network is connected to one central node. Every device in the network is directly connected to the central node and indirectly connected to every other node.
  • 14. 14 | P a g e The relationship between these elements is that the central network device is a server and other devices are treated as clients. The central node has the responsibility of managing data transmissions across the network. The central node or hub also acts as a repeater. In star topologies, computersare connected with a coaxial cable, twisted pair, or optical fiber cable. RING TOPOLOGY: In networks with a ring topology, computers are connected to each other in a circular format. Every device in the network will have two neighbors and no more or no less. The first node is connected to the last node to link the loop together. Ring topologies are unidirectional but can also be made bidirectional.
  • 15. 15 | P a g e Ring topologies are unidirectional but can also be made bidirectional. To make ring topologies bidirectional you would need to have two connections between network nodes to form a Dual Ring Topology. Ring topologies can sustain large networks much more effectively than bus topologies.
  • 16. 16 | P a g e BUS TOPOLOGY:- Bus topology is a type of network where every device is connected to a single cable which runs from one end of the network to the other. This type of type of topology is often referred to as line topology. In a bus topology, data is transmitted in one direction only. If the bus topology has two endpoints then it is referred to as a linear bus topology. Organizations using this type of topology will generally use an RJ45 cable to link devices together.
  • 17. 17 | P a g e MESH TOPOLOGY:- A mesh topology is a point-to-point connectionwhere nodes are interconnected. In this form of topology, data is transmitted via two methods: routing and flooding. Routing is where nodes use routing logic to work out the shortest distance to the packet’s destination. In contrast flooding, data is sent to all nodes within the network. Floodingdoesn’t require any form of routing logic to work. The interconnectivity of nodesmakes them extremely resistant to failures.
  • 18. 18 | P a g e Experiment:-4 1.Bit Stuffing #include<stdio.h> #include<conio.h> #include<string.h> void main() { int a[20],b[30],i,j,k,count,n; printf("Enter frame size (Example: 8):"); scanf("%d",&n); Aim: Write a program in c to implement various type of framing method.  Bit stuffing  Byte stuffing  Character Stuffing
  • 19. 19 | P a g e printf("Enterthe frame in the form of 0 and 1 :"); for(i=0; i<n; i++) scanf("%d",&a[i]); i=0; count=1; j=0; while(i<n) { if(a[i]==1) { b[j]=a[i]; for(k=i+1; a[k]==1 && k<n && count<5;k++) { j++; b[j]=a[k]; count++; if(count==5) {
  • 20. 20 | P a g e j++; b[j]=0; } i=k; } } else { b[j]=a[i]; } i++; j++; } printf("After Bit Stuffing :"); for(i=0; i<j; i++) printf("%d",b[i]); getch(); }
  • 21. 21 | P a g e OUTPUT: Enterframe size (Example: 8):12 Enterthe frame in the form of 0 and 1 :0 1 0 1 1 1 1 1 1 0 0 1 After Bit Stuffing :0101111101001 2.Byte Stuffing #include<stdio.h> #include<string.h> void main(){ char frame[50][50],str[50][50]; char flag[10]; strcpy(flag,"flag"); char esc[10]; strcpy(esc,"esc"); int i,j,k=0,n; strcpy(frame[k++],"flag");
  • 22. 22 | P a g e printf("Enterno.ofString :t"); scanf("%d",&n); printf("EnterString n"); for(i=0;i<=n;i++) { gets(str[i]); } printf("You entered :n"); for(i=0;i<=n;i++) { puts(str[i]); } printf("n"); for(i=1;i<=n;i++) { if(strcmp(str[i],flag)!=0 && strcmp(str[i],esc)!=0) { strcpy(frame[k++],str[i]); } else { strcpy(frame[k++],"esc"); strcpy(frame[k++],str[i]); }
  • 23. 23 | P a g e } strcpy(frame[k++],"flag"); //frame[k++]='0'; printf("------------------------------n"); printf("Byte stuffing at sender side:nn"); printf("------------------------------n"); for(i=0;i<k;i++) { printf("%st",frame[i]); } } Output:- Enter no.of String : 2 Enter String Apple cake You entered : Apple cake - -------------------------- Byte stuffing at sender side: ------------------------------ flag Apple cake flag
  • 24. 24 | P a g e 3.Character Stuffing #include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> void main() { int i=0,j=0,n,pos; char a[20],b[50],ch; clrscr(); printf("enter stringn"); scanf("%s",&a); n=strlen(a); printf("enter positionn"); scanf("%d",&pos); if(pos>n) {
  • 25. 25 | P a g e printf("invalid position, Enter again :"); scanf("%d",&pos); } printf("enter the charactern"); ch=getche(); b[0]='d'; b[1]='l'; b[2]='e'; b[3]='s'; b[4]='t'; b[5]='x'; j=6; while(i<n) { if(i==pos-1) { b[j]='d';
  • 26. 26 | P a g e b[j+1]='l'; b[j+2]='e'; b[j+3]=ch; b[j+4]='d'; b[j+5]='l'; b[j+6]='e'; j=j+7; } if(a[i]=='d' && a[i+1]=='l' && a[i+2]=='e') { b[j]='d'; b[j+1]='l'; b[j+2]='e'; j=j+3; } b[j]=a[i]; i++; j++; }
  • 27. 27 | P a g e b[j]='d'; b[j+1]='l'; b[j+2]='e'; b[j+3]='e'; b[j+4]='t'; b[j+5]='x'; b[j+6]='0'; printf("nframe after stuffing:n"); printf("%s",b); getch(); } INPUT:- enter string:- asdlefgh enter position: 8 invalid position, enter again: 3 enter the character: k OUTPUT:- frame after stuffing:- dlestx as dle k dle dle dlefgh dleetx
  • 28. 28 | P a g e Experiment:-5 Aim:- Write a program in C to implement Various routing algorithm.  Simple redundancy check  Cyclic redundancy check  Checksum  Hamming code 1.Simple Parity Check: # include<bits/stdc++.h> # define bool int using namespace std; // Function to get parity of number n. It returns1 // if n has odd parity, and returns 0 if n has even // parity bool getParity(unsigned int n) { bool parity = 0; while (n)
  • 29. 29 | P a g e { parity = !parity; n = n & (n - 1); } return parity; } /* Driver program to test getParity() */ int main() { unsigned int n = 7; cout<<"Parity of no "<<n<<" = "<<(getParity(n)? "odd": "even"); getchar(); return 0; } OUTPUT Even Parity Odd Parity
  • 30. 30 | P a g e 2.Cyclic RedundancyCheck #include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,keylen,msglen; char input[100], key[30],temp[30],quot[100],rem[30],key1[30]; clrscr(); printf("EnterData: "); gets(input); printf("EnterKey: "); gets(key); keylen=strlen(key); msglen=strlen(input); strcpy(key1,key);
  • 31. 31 | P a g e for(i=0;i<keylen-1;i++) { input[msglen+i]='0'; } for(i=0;i<keylen;i++) temp[i]=input[i]; for(i=0;i<msglen;i++) { quot[i]=temp[0]; if(quot[i]=='0') for(j=0;j<keylen;j++) key[j]='0'; else for(j=0;j<keylen;j++) key[j]=key1[j]; for(j=keylen-1;j>0;j--) { if(temp[j]==key[j])
  • 32. 32 | P a g e rem[j-1]='0'; else rem[j-1]='1'; } rem[keylen-1]=input[i+keylen]; strcpy(temp,rem); } strcpy(rem,temp); printf("nQuotient is "); for(i=0;i<msglen;i++) printf("%c",quot[i]); printf("nRemainder is "); for(i=0;i<keylen-1;i++) printf("%c",rem[i]); printf("nFinal data is: "); for(i=0;i<msglen;i++) printf("%c",input[i]); for(i=0;i<keylen-1;i++)
  • 33. 33 | P a g e printf("%c",rem[i]); getch(); } Output: EnterData: 11110110101 EnterKey: 111010 Quotient is 10010010000 Remainder is 00000 Final data is: 1111011010100000
  • 34. 34 | P a g e 3. CheckSum #include<stdio.h> #include<string.h> int main() { char a[20],b[20]; char sum[20],complement[20]; int i,length; printf("Enter first binary stringn"); scanf("%s",&a); printf("Enter second binary stringn"); scanf("%s",&b); if(strlen(a)==strlen(b)){ length = strlen(a);
  • 35. 35 | P a g e char carry='0'; for(i=length-1;i>=0;i--) { if(a[i]=='0' && b[i]=='0' && carry=='0') { sum[i]='0'; carry='0'; } else if(a[i]=='0' && b[i]=='0' && carry=='1') { sum[i]='1'; carry='0'; } else if(a[i]=='0' && b[i]=='1' && carry=='0') { sum[i]='1';
  • 36. 36 | P a g e carry='0'; } else if(a[i]=='0' && b[i]=='1' && carry=='1') { sum[i]='0'; carry='1'; } else if(a[i]=='1' && b[i]=='0' && carry=='0') { sum[i]='1'; carry='0'; } else if(a[i]=='1' && b[i]=='0' && carry=='1') { sum[i]='0';
  • 37. 37 | P a g e carry='1'; } else if(a[i]=='1' && b[i]=='1' && carry=='0') { sum[i]='0'; carry='1'; } else if(a[i]=='1' && b[i]=='1' && carry=='1') { sum[i]='1'; carry='1'; } else break; }
  • 38. 38 | P a g e printf("nSum=%c%s",carry,sum); for(i=0;i<length;i++) { if(sum[i]=='0') complement[i]='1'; else complement[i]='0'; } if(carry=='1') carry='0'; else carry='1'; printf("nChecksum=%c%s",carry,complement); }
  • 39. 39 | P a g e else { printf("nWrong input strings"); } } OUTPUT Enterfirst binary string 101101 Entersecond binary string 110010 Sum=1011111 Checksum=0100000 --------------------------------------
  • 40. 40 | P a g e 4.Hamming Code #include<stdio.h> #include<conio.h> void main() { int data[7],rec[7],i,c1,c2,c3,c; printf("this works for message of 4bits in size nenter message bit one by one: "); scanf("%d%d%d%d",&data[0],&data[1],&data[2],& data[4]); data[6]=data[0]^data[2]^data[4]; data[5]=data[0]^data[1]^data[4]; data[3]=data[0]^data[1]^data[2]; printf("nthe encoded bits are given below: n"); for (i=0;i<7;i++) { printf("%d ",data[i]); } printf("nenter the received data bits one by one: ");
  • 41. 41 | P a g e for (i=0;i<7;i++) { scanf("%d",&rec[i]); } c1=rec[6]^rec[4]^rec[2]^rec[0]; c2=rec[5]^rec[4]^rec[1]^rec[0]; c3=rec[3]^rec[2]^rec[1]^rec[0]; c=c3*4+c2*2+c1 ; if(c==0) { printf("ncongratulationsthere is no error: "); } else { printf("nerron on the postion: %dnthe correct message is n",c); if(rec[7-c]==0) rec[7-c]=1; else rec[7-c]=0; for (i=0;i<7;i++) { printf("%d ",rec[i]); } }
  • 42. 42 | P a g e getch(); } Output:- this works for message of 4bits in size enter message bit one by one:0 1 0 1 the encoded bits are given below:- 0101101 enter the received data bits one by one:-0 1 0 1 1 0 1 congratulationsthere is no error:
  • 43. 43 | P a g e Experiment:-6 Aim:- Write a program in C to implement routing algorithm.  Distance vector routing  Link state routing 1. Distancevector routing #include<stdio.h> struct node { unsigned dist[20]; unsigned from[20]; }rt[10]; int main() { int costmat[20][20]; int nodes,i,j,k,count=0;
  • 44. 44 | P a g e printf("nEnterthe number of nodes : "); scanf("%d",&nodes);//Enterthe nodes printf("nEnterthe cost matrix :n"); for(i=0;i<nodes;i++) { for(j=0;j<nodes;j++) { scanf("%d",&costmat[i][j]); costmat[i][i]=0; rt[i].dist[j]=costmat[i][j];//initialisethe distance equal to cost matrix rt[i].from[j]=j; } } do { count=0; for(i=0;i<nodes;i++)
  • 45. 45 | P a g e //We choosearbitary vertex k and we calculatethe direct distance from the nodei to k using the cost matrix //and add the distance from k to nodej for(j=0;j<nodes;j++) for(k=0;k<nodes;k++) if(rt[i].dist[j]>costmat[i][k]+rt[k].dist[j]) {//We calculatethe minimum distance rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j]; rt[i].from[j]=k; count++; } }while(count!=0); for(i=0;i<nodes;i++) { printf("nn For router%dn",i+1); for(j=0;j<nodes;j++) {
  • 46. 46 | P a g e printf("tnnode%d via %d Distance %d ",j+1,rt[i].from[j]+1,rt[i].dist[j]); } } printf("nn"); getch(); } OUTPUT A sample run of the program works as:- Enter the number of nodes: 3 Enterthe cost matrix: 0 2 7 2 0 1 7 1 0
  • 47. 47 | P a g e For router1 node 1 via 1 Distance 0 node 2 via 2 Distance 2 node 3 via 3 Distance 3 For router2 node 1 via 1 Distance 2 node 2 via 2 Distance 0 node 3 via 3 Distance 1 For router3 node 1 via 1 Distance 3 node 2 via 2 Distance 1 node 3 via 3 Distance 0
  • 48. 48 | P a g e 2. Link State Routing #include<stdio.h> #include<conio.h> #define INFINITY 9999 #define MAX 10 void lsr(int G[MAX][MAX],int n,int startnode); int main() { int G[MAX][MAX],i,j,n,u; printf("Enterno. of vertices:"); scanf("%d",&n); printf("nEnterthe adjacency matrix:n"); for(i=0;i<n;i++) for(j=0;j<n;j++)
  • 49. 49 | P a g e scanf("%d",&G[i][j]); printf("nEnterthe starting node:"); scanf("%d",&u); dijkstra(G,n,u); return 0; } void lsr(int G[MAX][MAX],int n,int startnode) { int cost[MAX][MAX],distance[MAX],pred[MAX]; int visited[MAX],count,mindistance,nextnode,i,j; //pred[]stores the predecessorof each node //countgives the number of nodes seen so far //createthe cost matrix
  • 50. 50 | P a g e for(i=0;i<n;i++) for(j=0;j<n;j++) if(G[i][j]==0) cost[i][j]=INFINITY; else cost[i][j]=G[i][j]; //initialize pred[],distance[]and visited[] for(i=0;i<n;i++) { distance[i]=cost[startnode][i]; pred[i]=startnode; visited[i]=0; } distance[startnode]=0; visited[startnode]=1; count=1;
  • 51. 51 | P a g e while(count<n-1) { mindistance=INFINITY; //nextnodegives the nodeat minimum distance for(i=0;i<n;i++) if(distance[i]<mindistance&&!visited[i]) { mindistance=distance[i]; nextnode=i; } //check if a better path exists through nextnode visited[nextnode]=1; for(i=0;i<n;i++) if(!visited[i])
  • 52. 52 | P a g e if(mindistance+cost[nextnode][i]<distance[i]) { distance[i]=mindistance+cost[nextnode][i]; pred[i]=nextnode; } count++; } //print the path and distance of each node for(i=0;i<n;i++) if(i!=startnode) { printf("nDistance of node%d=%d",i,distance[i]); printf("nPath=%d",i);
  • 53. 53 | P a g e j=i; do { j=pred[j]; printf("<-%d",j); }while(j!=startnode); } } OUTPUT: Enterno. of vertices: 5 Enterthe adjency matrix: 10 0 30 100 10 0 50 0 0 0 50 0 20 10 38 0 23 0 63
  • 54. 54 | P a g e Enterthe starting node: 0 Distance of node1 = 10 Path = 1<-0 Distance of node2 = 50 Path= 2<-3<-0 Distance of node3 = 30 Path = 3<-0 Distance of node4 =60 Path = 4<-2<-3<-0 Process returned 5(0x5) execution time : 47.471s Press any key to continue.
  • 55. 55 | P a g e Experiment:-7 Aim: Write two programs in C Hello_Clientand Hello_Server.  The server listens for and accepts a single TCP connection. It reads all the data it can form that connectionand prints it to screen then it close the connection  The client connectsto the server sends the string “COMPUTER NETWORK” then closes the connection 1.TCP SERVER:- #include<netdb.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<sys/types.h>
  • 56. 56 | P a g e #define MAX 80 #define PORT 8080 #define SA struct sockaddr // Function designed for chat between client and server. void func(int sockfd) { char buff[MAX]; int n; // infinite loop for chat for (;;) { bzero(buff, MAX); // read the message from client and copy it in buffer read(sockfd, buff, sizeof(buff)); // print buffer which containsthe client
  • 57. 57 | P a g e contents printf("From client: %st To client : ", buff); bzero(buff, MAX); n = 0; // copy server message in the buffer while ((buff[n++] = getchar()) != 'n') ; // and send that buffer to client write(sockfd, buff, sizeof(buff)); // if msg contains "Exit" then server exit and chat ended. if (strncmp("exit", buff, 4) == 0) { printf("Server Exit...n"); break; } }
  • 58. 58 | P a g e } // Driver function int main() { int sockfd, connfd, len; struct sockaddr_in servaddr, cli; // socket create and verification sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket creation failed...n"); exit(0); } else printf("Socket successfully created..n"); bzero(&servaddr, sizeof(servaddr));
  • 59. 59 | P a g e // assign IP, PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr= htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); // Binding newly created socket to given IP and verification if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { printf("socket bind failed...n"); exit(0); } else printf("Socket successfully binded..n"); // Now server is ready to listen and verification
  • 60. 60 | P a g e if ((listen(sockfd, 5)) != 0) { printf("Listen failed...n"); exit(0); } else printf("Server listening..n"); len = sizeof(cli); // Accept the data packet from client and verification connfd = accept(sockfd, (SA*)&cli, &len); if (connfd < 0) { printf("server acccept failed...n"); exit(0); } else printf("server acccept the client...n"); // Function for chatting between client and server func(connfd);
  • 61. 61 | P a g e // After chattingclose the socket close(sockfd); } Output Server side: Socket successfully created.. Socket successfully binded.. Server listening.. server acccept the client... From client: hi To client : hello From client: exit To client : exit Server Exit...
  • 62. 62 | P a g e 3. TCP CLIENT:- #include<netdb.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #define MAX 80 #define PORT 8080 #define SA struct sockaddr void func(int sockfd) { char buff[MAX]; int n; for (;;) { bzero(buff, sizeof(buff)); printf("Enter the string : "); n = 0;
  • 63. 63 | P a g e while ((buff[n++] = getchar()) != 'n') ; write(sockfd, buff, sizeof(buff)); bzero(buff, sizeof(buff)); read(sockfd, buff, sizeof(buff)); printf("From Server : %s", buff); if ((strncmp(buff, "exit", 4)) == 0) { printf("Client Exit...n"); break; } } } int main() { int sockfd, connfd; struct sockaddr_in servaddr, cli;
  • 64. 64 | P a g e // socket create and varification sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket creation failed...n"); exit(0); } else printf("Socket successfully created..n"); bzero(&servaddr, sizeof(servaddr)); // assign IP, PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr= inet_addr("127.0.0.1"); servaddr.sin_port = htons(PORT); // connect the client socket to server socket if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
  • 65. 65 | P a g e printf("connectionwith the server failed...n"); exit(0); } else printf("connected to the server..n"); // function for chat func(sockfd); // close the socket close(sockfd); }
  • 66. 66 | P a g e Output Client side: Socket successfully created.. connectedto the server.. Enterthe string : hi From Server : hello Enterthe string : exit From Server : exit Client Exit...