0% found this document useful (0 votes)
26 views

Computer Network Lab Manual 2024 5 Pro

Computer network Computer

Uploaded by

vaddearjunarjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Computer Network Lab Manual 2024 5 Pro

Computer network Computer

Uploaded by

vaddearjunarjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

COMPUTER NETWORK

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

/ C program to illustrate the above


// given pattern of numbers.
#include <stdio.h>

int main()
{
int n = 5, i, j, num = 1, gap;

gap = n - 1;

for (j = 1; j <= n; j++) {


num = j;

for (i = 1; i <= gap; i++)


printf(" ");

gap--;

for (i = 1; i <= j; i++) {


printf("%d", num);
num++;
}
num--;
num--;
for (i = 1; i < j; i++) {
printf("%d", num);
num--;
}
printf("\n");
}

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;

// Ask user to enter the two numbers


printf("Enter two integers: ");

// Read two numbers from the user || A = 2, B = 3


scanf("%d %d", &A, &B);

// Calculate the addition of A and B


// using '+' operator
sum = A + B;

// Print the sum


printf("Sum: %d", sum);

return 0;
}

Output
Enter two integers: 5 3
Sum: 8

1.Here is a simple implementation of character stuffing and bit stuffing methods in C:

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 '^'

void characterStuffing(char *data, char *stuffedData) {


int i, j = 0;
for (i = 0; i < strlen(data); i++) {
if (data[i] == ESCAPE_CHAR) {
stuffedData[j++] = ESCAPE_CHAR;
stuffedData[j++] = ESCAPE_SEQ;
} else {
stuffedData[j++] = data[i];
}
}
stuffedData[j] = '\0';
}

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>

void bitStuffing(char *data, char *stuffedData) {


int i, j = 0;
for (i = 0; i < strlen(data); i++) {
if (i > 0 && data[i] == '1' && data[i - 1] == '1' && data[i - 2] == '1' && data[i - 3] == '1' && data[i -
4] == '1' &&data[i-5]=='1') {
stuffedData[j++] = '0';
}
stuffedData[j++] = data[i];
}
stuffedData[j] = 'e';
}

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

// Structure to represent a frame


typedef struct {
int data;
int seq_num;
} Frame;

// Function to send frames using Sliding Window protocol


void send_frames(Frame frames[], int num_frames) {
int window_start = 0;
int window_end = WINDOW_SIZE;

while (window_end <= num_frames) {


// Send frames in the current window
for (int i = window_start; i < window_end; i++) {
printf("Sending frame %d\n", frames[i].seq_num);
}

// Simulate acknowledgement receipt


int ack_num = window_start;
printf("Received ACK %d\n", ack_num);

// Slide the window


window_start = ack_num + 1;
window_end = window_start + WINDOW_SIZE;
}
}

// Function to perform loss recovery using Go-Back-N mechanism


void loss_recovery(Frame frames[], int num_frames) {
int window_start = 0;
int window_end = WINDOW_SIZE;

while (window_end <= num_frames) {


// Send frames in the current window
for (int i = window_start; i < window_end; i++) {
printf("Sending frame %d\n", frames[i].seq_num);
}

// Simulate loss of a frame


int lost_frame = window_start + 2;
printf("Lost frame %d\n", lost_frame);

// Go-Back-N mechanism
window_start = lost_frame;
window_end = window_start + WINDOW_SIZE;

// Resend lost frame and subsequent frames


for (int i = window_start; i < window_end; i++) {
printf("Resending frame %d\n", frames[i].seq_num);
}
}
}
int main() {
Frame frames[MAX_FRAME];

// Initialize frames with sequence numbers


for (int i = 0; i < MAX_FRAME; i++) {
frames[i].seq_num = i;
frames[i].data = i * 2;
}

printf("Sliding Window protocol:\n");


send_frames(frames, MAX_FRAME);

printf("\nGo-Back-N loss recovery:\n");


loss_recovery(frames, MAX_FRAME);

return 0;
}
Output:

sliding Window protocol:


Sending frame 0
Sending frame 1
Sending frame 2
Sending frame 3
Received ACK 0
Sending frame 1
Sending frame 2
Sending frame 3
Sending frame 4
Received ACK 1
Sending frame 2
Sending frame 3
Sending frame 4
Sending frame 5
Received ACK 2
Sending frame 3
Sending frame 4
Sending frame 5
Sending frame 6
Received ACK 3
Sending frame 4
Sending frame 5
Sending frame 6
Sending frame 7
Received ACK 4
Sending frame 5
Sending frame 6
Sending frame 7
Sending frame 8

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>

#define MAX_NODES 100

// Structure to represent a node


typedef struct {
int distance;
int predecessor;
} Node;

// Function to find the shortest path using Dijkstra's algorithm


void dijkstra(int graph[MAX_NODES][MAX_NODES], int num_nodes, int start_node) {
Node nodes[MAX_NODES];
int visited[MAX_NODES] = {0};

// Initialize distances and predecessors


for (int i = 0; i < num_nodes; i++) {
nodes[i].distance = INT_MAX;
nodes[i].predecessor = -1;
}
nodes[start_node].distance = 0;

// Find shortest path


for (int i = 0; i < num_nodes; i++) {
int min_distance = INT_MAX;
int min_node = -1;

// Find node with minimum distance


for (int j = 0; j < num_nodes; j++) {
if (!visited[j] && nodes[j].distance < min_distance) {
min_distance = nodes[j].distance;
min_node = j;
}
}

visited[min_node] = 1;

// Update distances and predecessors


for (int j = 0; j < num_nodes; j++) {
if (!visited[j] && graph[min_node][j] != 0) {
int distance = nodes[min_node].distance + graph[min_node][j];
if (distance < nodes[j].distance) {
nodes[j].distance = distance;
nodes[j].predecessor = min_node;
}
}
}
}

// Print shortest path


for (int i = 0; i < num_nodes; i++) {
printf("Shortest path from %d to %d: ", start_node, i);
int node = i;
while (node != -1) {
printf("%d ", node);
node = nodes[node].predecessor;
}
printf("(distance: %d)\n", nodes[i].distance);
}
}

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;

dijkstra(graph, num_nodes, start_node);

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:

Enter no.of nodes:1


Enter adjacent matrix
Enter connecting of 1-->1::1
Enter root node:1
Adjacent node of root node::
1

You might also like