Computer Network Lab Manual 2024 5 Pro
Computer Network Lab Manual 2024 5 Pro
LAB MANUAL
2024-2025
sample program on linux:
1.sample
#include
main()
{
printf("This is my first C program on ubuntu\n");
}
output:
gcc program.c
./a.out
this is my first c program on ubuntu
2. sample
#include<stdio.h>
#include<string.h>
int main() {
printf("hello world\n");
}
output:
gcc hello.c
./a.out
hello world
3. pattern of numbers
int main()
{
int n = 5, i, j, num = 1, gap;
gap = n - 1;
gap--;
return 0;
}
Output
1
232
34543
4567654
567898765
3.C program to add two numbers
#include <stdio.h>
int main() {
int A, B, sum = 0;
return 0;
}
Output
Enter two integers: 5 3
Sum: 8
Character Stuffing:
Character stuffing is a method of framing in which a special character (called the escape character) is
added to the data frame to indicate the start and end of the frame. If the data contains the escape
character, it is replaced with a sequence of two characters, the escape character followed by a
special character called the escape sequence.
#include <stdio.h>
#include <string.h>
#define ESCAPE_CHAR '$'
#define ESCAPE_SEQ '^'
int main() {
char data[] = "$Hello$World$";
char stuffedData[100];
characterStuffing(data, stuffedData);
printf("Original Data: %s\n", data);
printf("Stuffed Data: %s\n", stuffedData);
return 0;
}
Out put:
Original Data: $Hello$World$
Stuffed Data :$^Hello$^World$^
Bit Stuffing:
1.b bitstuffing
Bit stuffing is a method of framing in which a special bit sequence (called the flag sequence) is
added to the data frame to indicate the end of the frame. If the data contains a sequence of five
consecutive 1's, a 0 bit is inserted after the fifth 1.
#include <stdio.h>
#include <string.h>
int main() {
char data[] = "1111110";
char stuffedData[100];
bitStuffing(data, stuffedData);
printf("Original Data: %s\n", data);
printf("Stuffed Data: %s\n", stuffedData);
return 0;
}
Output:
Original Data: 1111110
Stuffed Data: 11111010e
2. write a c program to compute CRC code for polynomials CRC-12,CRC-16 AND CRC CCIP
#include<stdio.h>
#include<conio.h>
int gen[4],genl,frl,rem[4];
void main()
{
int i,j,fr[8],dupfr[11],recfr[11],tlen,flag;
clrscr();
frl=8;
genl=4;
printf("enter frame:");
for(i=0;i<frl;i++)
{
scanf("%d",&fr[i]);dupfr[i]=fr[i];
}
printf("enter generator:");
for(i=0;i<genl;i++)
scanf("%d",&gen[i]);
tlen=frl+genl-1;
for(i=frl;i<tlen;i++)
{
dupfr[i]=0;}
remainder(dupfr);
for(i=0;i<frl;i++)
{
recfr[i]=fr[i];
}
for(i=frl,j=1;j<genl;i++,j++)
{
recfr[i]=rem[j];
}
remainder(recfr);
flag=0;
for(i=0;i<4;i++)
{
if(rem[i]!=0)flag++;
}
if(flag==0)
{
printf("frame received correctly");
}
Else
{
printf("the received frame is wrong");
}
getch(); }
remainder(int fr[])
{
int k,k1,i,j;
for(k=0;k<frl;k++)
{
if(fr[k]==1)
{
k1=k;
for(i=0,j=k;i<genl;i++,j++)
{
rem[i]=fr[j]^gen[i];
}
for(i=0;i<genl;i++)
{
fr[k1]=rem[i];
k1++;
}
}
}
}
Output:
Enter frame:1
0
1
0
1
1
1
1
Enter generator:1
0
1
1
The received frame is wrong
Output2:
Enter frame :1
1
1
1
1
1
1
1
Enter generator:1
1
0
1
Frame received correctly
3. Here is a simple C program that demonstrates the data link layer using the Sliding Window
protocol for flow control and the Go-Back-N mechanism for loss recovery:
#include <stdio.h>
#include <stdlib.h>
#define WINDOW_SIZE 4
#define MAX_FRAME 10
// Go-Back-N mechanism
window_start = lost_frame;
window_end = window_start + WINDOW_SIZE;
return 0;
}
Output:
4. Here is a C program that implements Dijkstra's algorithm to compute the shortest path through a
network:
#include <stdio.h>
#include <limits.h>
visited[min_node] = 1;
int main() {
int graph[MAX_NODES][MAX_NODES] = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
int num_nodes = 9;
int start_node = 0;
return 0;
}
Output:
Shortest path from 0 to 0: 0 (distance: 0)
Shortest path from 0 to 1: 1 0 (distance: 4)
Shortest path from 0 to 2: 2 1 0 (distance: 12)
Shortest path from 0 to 3: 3 2 1 0 (distance: 19)
Shortest path from 0 to 4: 4 5 6 7 0 (distance: 21)
Shortest path from 0 to 5: 5 6 7 0 (distance: 11)
Shortest path from 0 to 6: 6 7 0 (distance: 9)
Shortest path from 0 to 7: 7 0 (distance: 8)
Shortest path from 0 to 8: 8 2 1 0 (distance: 14)
5.Writea Program to implement Broadcast tree by taking
subnet of hosts.
#include<stdio.h>
#include<conio.h>
int a[10][10],n;
void main()
{
int i,j,root;
printf("Enter no.of nodes:");
scanf("%d",&n);
printf("Enter adjacent matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("Enter connecting of %d-->%d::",i,j);
scanf("%d",&a[i][j]);
}
printf("Enter root node:");
scanf("%d",&root);
adj(root);
}
adj(int k)
{
int i,j;
printf("Adjacent node of root node::\n");
printf("%d\n\n",k);
for(j=1;j<=n;j++)
{
if(a[k][j]==1 || a[j][k]==1)
printf("%d\t",j);
}
printf("\n");
for(i=1;i<=n;i++)
{
if((a[k][j]==0) && (a[i][k]==0) && (i!=k))
printf("%d",i);
return(0);
}
}
Ouput: