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

CN Lab Manual

The document is a lab manual for B.Tech III CSE students, detailing various experiments related to network devices and data link layer protocols. It covers the study of routers, switches, hubs, access points, NICs, and modems, as well as practical implementations of data link layer techniques such as framing methods, checksums, Hamming code, and CRC. Additionally, it includes source code examples for implementing the Go-Back-N and Selective Repeat sliding window protocols.

Uploaded by

m.anand502
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CN Lab Manual

The document is a lab manual for B.Tech III CSE students, detailing various experiments related to network devices and data link layer protocols. It covers the study of routers, switches, hubs, access points, NICs, and modems, as well as practical implementations of data link layer techniques such as framing methods, checksums, Hamming code, and CRC. Additionally, it includes source code examples for implementing the Go-Back-N and Selective Repeat sliding window protocols.

Uploaded by

m.anand502
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

CN LAB MANUAL CSE DEPARTMENT B.

TECH III CSE

EXPERIMENT-1

Aim: Study of Network devices in detail and connect the computers in Local Area Network.

1. **Router:**

- A router is a networking device that connects multiple networks together, such as LANs and the
Internet.

- It forwards data packets between networks based on IP addresses.

- In a LAN, the router serves as the gateway to the Internet and handles the distribution of internet
traffic to connected devices.

2. **Switch:**

- A switch is a device that connects multiple devices within a LAN.

- It operates at the data link layer (Layer 2) of the OSI model and uses MAC addresses to forward
data packets between devices on the same network.

- Switches offer dedicated bandwidth to each connected device, improving network performance
compared to older hub-based networks.

3. **Hub:**

- A hub is an older networking device that operates similarly to a switch but lacks intelligence.

- It operates at the physical layer (Layer 1) and simply broadcasts data to all connected devices,
leading to inefficient use of network bandwidth.

- Hubs are mostly obsolete and have been replaced by switches.

4. **Access Point (AP):**

- An access point is a device that enables wireless devices to connect to a wired network.

- It acts as a bridge between wired and wireless networks, providing Wi-Fi connectivity to devices
like laptops, smartphones, and tablets.

5. **Network Interface Card (NIC):**

- A network interface card is a hardware component installed in computers to enable them to


connect to a network.

1
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

- It provides the necessary physical interface (Ethernet port for wired connections or Wi-Fi antenna
for wireless connections) to communicate with other devices on the network.

6. **Modem:**

- A modem stands for modulator-demodulator. It converts digital signals from a computer into
analog signals suitable for transmission over analog communication channels (like telephone lines).

- On the receiving end, it converts incoming analog signals back into digital signals for the computer
to understand.

- Modems are commonly used to provide internet connectivity over DSL (Digital Subscriber Line) or
dial-up connections.

Now, let's connect the computers in a Local Area Network using a combination of wired and wireless
connections:

1. **Wired Connection:**

- For a wired LAN, you would need a switch to connect multiple computers and devices together.

- Each computer would require a Network Interface Card (NIC) with an Ethernet port.

- Connect each computer's Ethernet port to one of the switch ports using an Ethernet cable.

- If you need to provide internet access, connect the router's Ethernet port to one of the switch ports
as well.

2. **Wireless Connection:**

- To add wireless connectivity to the LAN, you'll need an Access Point (AP).

- Connect the AP to one of the switch ports using an Ethernet cable.

- Configure the AP to provide Wi-Fi connectivity with a specific SSID (Wi-Fi network name) and
security settings (WPA2, WPA3, etc.).

- Laptops, smartphones, and other wireless devices can now connect to the LAN by joining the
configured Wi-Fi network.

By combining wired and wireless connections, you can create a LAN that accommodates both types of
devices, providing seamless connectivity and access to shared resources like printers and files.

2
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-2.(a)

AIM: Implement the data link layer framing methods such as and bit stuffing.

SOURCE CODE:

#include<stdio.h>

#include<string.h>

void main()

int a[20],b[30],i,j,k,count,n;

printf("Enter frame length:");

scanf("%d",&n);

printf("Enter input frame (0's & 1's only):");

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)

j++;

3
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

b[j]=0;

i=k;

}}

else

b[j]=a[i];

i++;

j++;

printf("After stuffing the frame is:");

for(i=0;i<j;i++)

printf("%d",b[i]);

OUTPUT:

Enter frame length:5

Enter input frame (0's & 1's only):

After stuffing the frame is:111110

4
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-2.(b)

AIM: Implement the data link layer framing methods such as character, character stuffing.

SOURCE CODE:

#include<stdio.h>

#include<string.h>

#include<process.h>

void main()

int i=0,j=0,n,pos;

char a[20],b[50],ch;

printf("enter string\n");

scanf("%s",&a);

n=strlen(a);

printf("enter position\n");

scanf("%d",&pos);

if(pos>n)

printf("invalid position, Enter again :");

scanf("%d",&pos);

printf("enter the character\n");

ch=getche();

b[0]='d';

b[1]='l';

b[2]='e';

5
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

b[3]='s';

b[4]='t';

b[5]='x';

j=6;

while(i<n)

if(i==pos-1)

b[j]='d';

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];

6
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

i++;

j++;

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);

OUTPUT:

enter string

MLRITM

enter position

enter the character

frame after stuffing:

dlestxMdldleLRITMdleetx

7
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-3

Aim: . Write a Program to implement data link layer farming method checksum.

SOURCE CODE:

#include <stdio.h>

unsigned char calculate_checksum(unsigned char data_frame[], int frame_size)

unsigned int checksum = 0;

for (int i = 0; i < frame_size; i++)

checksum += data_frame[i];

checksum = (checksum & 0xFF) + (checksum >> 8);

return ~checksum & 0xFF;

int main()

unsigned char data_frame[] = {0x11, 0x22, 0x33};

int frame_size = sizeof(data_frame) / sizeof(data_frame[0]);

unsigned char checksum = calculate_checksum(data_frame, frame_size);

printf("Data Frame: ");

for (int i = 0; i < frame_size; i++)

printf("0x%02X ", data_frame[i]);

8
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

printf("\n");

printf("Calculated Checksum: 0x%02X\n", checksum);

return 0;

OUTPUT:

Data Frame: 0x11 0x22 0x33

Calculated Checksum: 0x99

9
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-4

Aim: Write a program for Hamming Code generation for error detection and correction.

SOURCE CODE:

#include <stdio.h>

int calculate_num_parity_bits(int data_size)

int r = 1;

while ((1 << r) < (data_size + r + 1))

r++;

return r;

void set_parity_bits(int data[], int data_size, int hamming[], int hamming_size)

int r = calculate_num_parity_bits(data_size);

int j = 0;

for (int i = 1; i <= hamming_size; i++)

if ((i & (i - 1)) == 0)

hamming[i - 1] = 0;

} else

10
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

hamming[i - 1] = data[j];

j++;

for (int i = 0; i < r; i++)

int parity_bit_pos = (1 << i) - 1; int parity = 0;

for (int j = parity_bit_pos; j < hamming_size; j += (1 << (i + 1)))

for (int k = 0; k < (1 << i); k++)

parity ^= hamming[j + k];

hamming[parity_bit_pos] = parity;

void print_hamming_code(int hamming[], int hamming_size)

printf("Generated Hamming Code: ");

for (int i = 0; i < hamming_size; i++)

printf("%d", hamming[i]);

printf("\n");

11
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

int main() {

int data[] = {1, 0, 0, 1};

int data_size = sizeof(data) / sizeof(data[0]);

int r = calculate_num_parity_bits(data_size);

int hamming_size = data_size + r;

int hamming[hamming_size];

set_parity_bits(data, data_size, hamming, hamming_size);

print_hamming_code(hamming, hamming_size);

return 0;

OUTPUT:

Generated Hamming Code: 1001010

12
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT NO: 5

AIM: Implement on a data set of characters the three CRC polynomials – CRC 12, CRC 16 and CRC CCIP.

SOURCE CODE:

#include<stdio.h>

int gen[4],genl,frl,rem[4];

void main()

int i,j,fr[8],dupfr[11],recfr[11],tlen,flag;

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);

13
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

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");

remainder(int fr[])

14
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

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

enter generator:frame received correctly

15
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-6

Aim: Write a Program to implement Sliding window protocol for Goback N.

Source Code:

#include <stdio.h>

#include <stdbool.h>

#include <stdlib.h>

#include <time.h>

#define WINDOW_SIZE 4

#define MAX_SEQ_NO (2 * WINDOW_SIZE)

bool is_successful_transmission(double probability)

return ((double)rand() / RAND_MAX) < probability;

void sender()

int next_seq_num = 0;

int base = 0;

int num_packets = 10;

double transmission_probability = 0.8;

printf("Sending packets using Go-Back-N protocol...\n");

while (base < num_packets) {

if (next_seq_num < base + WINDOW_SIZE)

16
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

printf("Sending packet %d\n", next_seq_num);

if (is_successful_transmission(transmission_probability))

next_seq_num++;

} else {

printf("Packet %d lost, resending window...\n", next_seq_num);

if (next_seq_num == base + WINDOW_SIZE || next_seq_num == num_packets) {

printf("Acknowledgment received for packet %d to %d\n", base, next_seq_num - 1);

base = next_seq_num;

printf("All packets sent successfully!\n");

void receiver() {

int expected_seq_num = 0;

int num_packets = 10;

double acknowledgment_probability = 0.9;

printf("Receiving packets and sending acknowledgments...\n");

while (expected_seq_num < num_packets) {

if (is_successful_transmission(acknowledgment_probability))

printf("Received packet %d\n", expected_seq_num);

expected_seq_num++;

17
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

} else {

printf("Acknowledgment for packet %d lost, requesting retransmission...\n",


expected_seq_num);

printf("All packets received and acknowledged!\n");

int main() {

srand(time(NULL));

sender();

receiver();

return 0;

OUTPUT:

Sending packets using Go-Back-N protocol...

Sending packet 0

Sending packet 1

Sending packet 2

Sending packet 3

Acknowledgment received for packet 0 to 3

Sending packet 4

Sending packet 5

Sending packet 6

18
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

Sending packet 7

Acknowledgment received for packet 4 to 7

Sending packet 8

Sending packet 9

Acknowledgment received for packet 8 to 9

All packets sent successfully!

Receiving packets and sending acknowledgments...

Received packet 0

Received packet 1

Received packet 2

Received packet 3

Received packet 4

Received packet 5

Received packet 6

Received packet 7

Received packet 8

Received packet 9

All packets received and acknowledged!

19
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-7

Aim: Write a Program to implement Sliding window protocol for Selective repeat.

SOURCE CODE:

#include <stdio.h>

#include <stdbool.h>

#include <stdlib.h>

#include <time.h>

#define WINDOW_SIZE 4

#define MAX_SEQ_NO (2 * WINDOW_SIZE)

bool is_successful_transmission(double probability)

return ((double)rand() / RAND_MAX) < probability;

void sender()

int next_seq_num = 0;

int base = 0;

int num_packets = 10;

double transmission_probability = 0.8;

printf("Sending packets using Selective Repeat protocol...\n");

while (base < num_packets)

if (next_seq_num < base + WINDOW_SIZE)

20
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

printf("Sending packet %d\n", next_seq_num);

next_seq_num++;

printf("Waiting for acknowledgments...\n");

int received_ack[MAX_SEQ_NO] = {0};

for (int i = base; i < next_seq_num; i++)

if (is_successful_transmission(transmission_probability))

received_ack[i] = 1;

while (received_ack[base])

printf("Acknowledgment received for packet %d\n", base);

base++;

printf("All packets sent successfully!\n");

void receiver()

int expected_seq_num = 0;

int num_packets = 10;

double acknowledgment_probability = 0.9;

21
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

printf("Receiving packets and sending acknowledgments...\n");

while (expected_seq_num < num_packets)

if (is_successful_transmission(acknowledgment_probability))

printf("Received packet %d\n", expected_seq_num);

expected_seq_num++;

printf("All packets received and acknowledged!\n");

int main() {

srand(time(NULL));

sender();

receiver();

return 0;

OUTPUT:

Sending packets using Selective Repeat protocol...

Sending packet 0

Waiting for acknowledgments...

Acknowledgment received for packet 0

Sending packet 1

22
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

Sending packet 2

Sending packet 3

Waiting for acknowledgments...

Acknowledgment received for packet 1

Acknowledgment received for packet 2

Acknowledgment received for packet 3

Sending packet 4

Sending packet 5

Sending packet 6

Sending packet 7

Waiting for acknowledgments...

Acknowledgment received for packet 4

Acknowledgment received for packet 5

Acknowledgment received for packet 6

Acknowledgment received for packet 7

Sending packet 8

Sending packet 9

Waiting for acknowledgments...

Acknowledgment received for packet 8

Acknowledgment received for packet 9

All packets sent successfully!

Receiving packets and sending acknowledgments...

Received packet 0

Received packet 1

Received packet 2

23
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

Received packet 3

Received packet 4

Received packet 5

Received packet 6

Received packet 7

Received packet 8

Received packet 9

All packets received and acknowledged!

24
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-8

Aim: Write a Program to implement Stop and Wait Protocol.

SOUCE CODE:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

char send_buffer[100];

char recv_buffer[100];

int send_index = 0;

int recv_index = 0;

void send_packet();

void receive_packet();

int main()

memset(send_buffer, '0', sizeof(send_buffer));

memset(recv_buffer, '0', sizeof(recv_buffer));

while (1)

send_packet();

receive_packet();

return 0;

void send_packet()

25
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

char byte = send_buffer[send_index];

printf("Sending byte %c\n", byte);

send_index++;

void receive_packet()

char recv_byte;

recv_byte = getchar();

printf("Received byte %c\n", recv_byte);

recv_buffer[recv_index] = recv_byte;

recv_index++;

OUTPUT:

Sending byte 0

Received byte A

Sending byte 0

Received byte B

Sending byte 0

Received byte C

Sending byte 0

Received byte D

Sending byte 0

...

26
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-9

Am: Write a program for congestion control using leaky bucket algorithm.

SOURCE CODE:

#include <stdio.h>

#include <stdbool.h>

#include <unistd.h>

#define BUCKET_SIZE 10

#define OUT_RATE 3

void send_packet(int packet_num)

printf("Sending packet %d\n", packet_num);

int main()

int num_packets = 20;

int bucket_level = 0;

int packet_counter = 0;

printf("Congestion control using Leaky Bucket algorithm...\n");

for (int i = 0; i < num_packets; i++) {

if (bucket_level < BUCKET_SIZE) {

bucket_level++; // Add packet to the bucket

send_packet(packet_counter);

packet_counter++;

27
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

else

printf("Bucket overflow, packet dropped!\n");

usleep(1000000 / OUT_RATE);

if (bucket_level > 0)

bucket_level--;

printf("All packets transmitted successfully!\n");

return 0;

OUTPUT:

Congestion control using Leaky Bucket algorithm...

Sending packet 0

Sending packet 1

Sending packet 2

Bucket overflow, packet dropped!

Sending packet 3

Sending packet 4

28
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

Sending packet 5

Bucket overflow, packet dropped!

Sending packet 6

Sending packet 7

Sending packet 8

Bucket overflow, packet dropped!

Sending packet 9

Sending packet 10

Sending packet 11

Bucket overflow, packet dropped!

Sending packet 12

Sending packet 13

Sending packet 14

Bucket overflow, packet dropped!

Sending packet 15

Sending packet 16

Sending packet 17

Bucket overflow, packet dropped!

Sending packet 18

Sending packet 19

All packets transmitted successfully!

29
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-10

AIM: Write a Program to implement Dijkstra‘s algorithm to compute the Shortest path through a graph.

SOURCE CODE:

#include<stdio.h>

void main()

int path[5][5],i,j,min,a[5][5],p,st=1,ed=5,stp,edp,t[5],index;

printf("enter the cost matrix\n");

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

for(j=1;j<=5;j++)

scanf("%d",&a[i][j]);

printf("enter the paths\n");

scanf("%d",&p);

printf("enter possible paths\n");

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

30
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

for(j=1;j<=5;j++)

scanf("%d",&path[i][j]);

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

t[i]=0;

stp=st;

for(j=1;j<=5;j++)

edp=path[i][j+1];

t[i]=t[i]+a[stp][edp];

if(edp==ed)

break;

else

stp=edp;

min=t[st];index=st;

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

if(min>t[i])

min=t[i];

index=i;

31
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

printf("minimum cost %d",min);

printf("\n minimum cost path ");

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

printf("--> %d",path[index][i]);

if(path[index][i]==ed)

break;

OUTPUT:

enter the cost matrix

12345

12345

12345

12345

12345

enter the paths

enter possible paths

12345

12345

minimum cost 14

32
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT NO: 11

AIM: Obtain Routing table at each node using distance vector routing algorithm for a given subnet.

SOURCE CODE:

#include<stdio.h>

struct node

unsigned dist[20];

unsigned from[20];

}rt[10];

int main()

int dmat[20][20];

int n,i,j,k,count=0;

printf("\nEnter the number of nodes : ");

scanf("%d",&n);

printf("Enter the cost matrix :\n");

33
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

for(i=0;i<n;i++)

for(j=0;j<n;j++)

scanf("%d",&dmat[i][j]);

dmat[i][i]=0;

rt[i].dist[j]=dmat[i][j];

rt[i].from[j]=j;

do

count=0;

for(i=0;i<n;i++)

for(j=0;j<n;j++)

for(k=0;k<n;k++)

if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])

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<n;i++)

printf("\nState value for router %d is \n",i+1);

for(j=0;j<n;j++)

34
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

printf("\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);

printf("\n");

OUTPUT:

Enter the number of nodes : 2

Enter the cost matrix :

12

12

State value for router 1 is

node 1 via 1 Distance0

node 2 via 2 Distance2

State value for router 2 is

node 1 via 1 Distance1

node 2 via 2 Distance0

35
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT NO: 12

AIM: Implement broadcast tree for a given subnet of hosts.

SOURCE CODE:

#include<stdio.h>

int p,q,u,v,n;

int min=99,mincost=0;

int t[50][2],i,j;

int parent[50],edge[50][50];

main()

clrscr();

printf("\n Enter the number of nodes");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("%c\t",65+i);

parent[i]=-1;

printf("\n");

for(i=0;i<n;i++)

printf("%c",65+i);

for(j=0;j<n;j++)

scanf("%d",&edge[i][j]);

}for(i=0;i<n;i++)

36
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

for(j=0;j<n;j++)

if(edge[i][j]!=99)

if(min>edge[i][j])

min=edge[i][j];

u=i;

v=j;

p=find(u);

q=find(v);

if(p!=q)

t[i][0]=u;

t[i][1]=v;

mincost=mincost+edge[u][v];

sunion(p,q);

}else{

t[i][0]=-1;

t[i][1]=-1;

min=99;

printf("Minimum cost is %d\n Minimum spanning tree is\n" ,mincost);

for(i=0;i<n;i++)

37
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

if(t[i][0]!=-1 && t[i][1]!=-1)

printf("%c %c %d", 65+t[i][0], 65+t[i][1],

edge[t[i][0]][t[i][1]]);

printf("\n");

}}

sunion(int l,int m)

parent[l]=m;

find(int l)

if(parent[l]>0)

l=parent[l];

return l;

OUTPUT:

Enter the number of nodes3

ABC

A1 2 3 4

B1 2 3 4

C4 5 6 7

Minimum cost is 3

Minimum spanning tree is

CA

38
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-13

Aim: Write a program for Wireshark

i. Packet Capture Using Wire shark.

SOURCE CODE:

#include <stdio.h>

#include <pcap.h>

#define MAX_PACKETS_TO_CAPTURE 10

void packet_handler(u_char *user_data, const struct pcap_pkthdr *pkthdr, const u_char *packet)

printf("Packet captured, length: %d\n", pkthdr->len);

int main()

char errbuf[PCAP_ERRBUF_SIZE];

pcap_t *handle;

struct pcap_pkthdr header;

const u_char *packet;

printf("Wireshark Packet Capture Using libpcap\n");

handle = pcap_open_live("eth0", BUFSIZ, 1, 1000, errbuf);

if (handle == NULL)

fprintf(stderr, "Couldn't open device: %s\n", errbuf);

return 1;

for (int i = 0; i < MAX_PACKETS_TO_CAPTURE; i++)

39
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

packet = pcap_next(handle, &header);

if (packet == NULL)

fprintf(stderr, "Error reading packet\n");

return 1;

packet_handler(NULL, &header, packet);

pcap_close(handle);

return 0;

OUTPUT:

Wireshark Packet Capture Using libpcap

Packet captured, length: <length of packet 1>

Packet captured, length: <length of packet 2>

Packet captured, length: <length of packet 3>

Packet captured, length: <length of packet 4>

Packet captured, length: <length of packet 5>

40
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

AIM: Write a program for Wireshark

ii. Starting Wire shark

Source Code:

#include <stdio.h>

#include <stdlib.h>

int main()

printf("Starting Wireshark...\n");

system("wireshark");

printf("Wireshark has been closed.\n");

return 0;

OUTPUT:

Starting Wireshark...

Wireshark has been closed.

41
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

AIM: Write a program for Wireshark

iii. Viewing Captured Traffic

SOURCE CODE:

#include <stdio.h>

#include <stdlib.h>

int main()

char filename[256];

printf("Enter the path to the capture file: ");

scanf("%255s", filename);

printf("Viewing captured traffic in Wireshark...\n");

char command[512];

snprintf(command, sizeof(command), "wireshark %s", filename);

system(command);

return 0;

OUTPUT:

Enter the path to the capture file: [user input]

Viewing captured traffic in Wireshark...

42
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

AIM: Write a program for Wireshark

iv. Analysis and Statistics & Filters.

SOURCE CODE:

#include <stdio.h>

#include <stdlib.h>

int main() {

char filter[256];

printf("Enter the Wireshark filter (e.g., 'ip', 'tcp port 80'): ");

scanf("%255s", filter); // Read up to 255 characters for the filter

printf("Analyzing traffic in Wireshark with filter: %s...\n", filter);

char command[512];

snprintf(command, sizeof(command), "wireshark -f \"%s\"", filter);

system(command);

return 0;

OUTPUT:

Enter the Wireshark filter (e.g., 'ip', 'tcp port 80'): [user input]

Analyzing traffic in Wireshark with filter: [user input]...

43
CN LAB MANUAL CSE DEPARTMENT B.TECH III CSE

EXPERIMENT-15

AIM: Operating System Detection using Nmap

SOURCE CODE:

#include <stdio.h>

#include <stdlib.h>

int main() {

char target[256];

printf("Enter the target IP address or hostname: ");

scanf("%255s", target);

printf("Detecting operating system using Nmap...\n");

char command[512];

snprintf(command, sizeof(command), "nmap -O %s", target);

system(command);

return 0;

OUTPUT:
Enter the target IP address or hostname: [user input]

Detecting operating system using Nmap...

44

You might also like