List of Simple Programs
Simple Program 1
Sliding Window Protocol
AIMDevelop a simple data link layer that performs flow control using the sliding window protocol, and loss recovery using the Go-Back-N mechanism.
#include <stdio.h>
int main()
{
int w, i, f, frames[50];
printf("Enter window size: ");
scanf("%d", &w);
printf("\nEnter number of frames to transmit: ");
scanf("%d", &f);
printf("\nEnter %d frames: ", f);
for (i = 1; i <= f; i++)
scanf("%d", &frames[i]);
printf("\nWith sliding window protocol the frames will be sent in the following manner (assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the receiver\n\n", w);
for (i = 1; i <= f; i++)
{
if (i % w == 0)
{
printf("%d\n", frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ", frames[i]);
}
if (f % w != 0)
printf("\nAcknowledgement of above frames sent is received by sender\n");
return 0;
}
Enter window size: 3 Enter number of frames to transmit: 5 Enter 5 frames: 12 5 89 4 6 With sliding window protocol the frames will be sent in the following manner (assuming no corruption of frames) After sending 3 frames at each stage sender waits for acknowledgement sent by the receiver 12 5 89 Acknowledgement of above frames sent is received by sender 4 6 Acknowledgement of above frames sent is received by sender
Simple Program 2
Broadcast Tree
AIMTake an example subnet of hosts and obtain a broadcast tree for the subnet.
#include <stdio.h>
int a[11][11], n;
void adj(int k)
{
int i;
printf("\nAdjacent nodes of root node:\n%d\n", k);
for (i = 1; i <= n; i++) /* nodes directly connected to root */
if (i != k && (a[k][i] == 1 || a[i][k] == 1))
printf("%d\t", i);
printf("\n");
for (i = 1; i <= n; i++) /* remaining nodes (next level) */
if (i != k && a[k][i] == 0 && a[i][k] == 0)
printf("%d\t", i);
printf("\n");
}
int main()
{
int i, j, root;
printf("Enter no. of nodes: ");
scanf("%d", &n);
printf("Enter adjacency matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
printf("Enter root node: ");
scanf("%d", &root);
adj(root);
return 0;
}
Enter no. of nodes: 5 Enter adjacency matrix: 0 1 1 0 0 1 0 1 1 0 1 1 0 0 0 0 1 0 0 1 0 0 0 1 0 Enter root node: 2 Adjacent nodes of root node: 2 1 3 4 5
Simple Program 3
Data Encryption and Decryption
AIMImplement data encryption and data decryption.
#include <stdio.h>
int main()
{
int i, key;
char str[100];
printf("Enter a string: ");
scanf(" %[^\n]", str);
printf("Enter the key: ");
scanf("%d", &key);
for (i = 0; str[i] != '\0'; i++) /* encryption: shift each char by key */
str[i] = str[i] + key;
printf("\nEncrypted string: %s\n", str);
for (i = 0; str[i] != '\0'; i++) /* decryption: shift back */
str[i] = str[i] - key;
printf("Decrypted string: %s\n", str);
return 0;
}
Enter a string: Computer Networks Enter the key: 3 Encrypted string: Frpsxwhu#Qhwzrunv Decrypted string: Computer Networks
Simple Program 4
Frame Sorting
AIMWrite a program for frame sorting techniques used in buffers.
#include <stdio.h>
#include <stdlib.h>
struct frame
{
int seq;
char data[20];
} arr[10], temp;
int main()
{
int n, i, j;
printf("Enter the number of frames: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
arr[i].seq = rand() % 50; /* random sequence number */
printf("Enter the frame contents for sequence number %d: ", arr[i].seq);
scanf("%s", arr[i].data);
}
/* bubble sort on sequence number */
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j].seq > arr[j + 1].seq)
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
printf("\nThe frames in sequence:\n");
for (i = 0; i < n; i++)
printf("%d\t%s\n", arr[i].seq, arr[i].data);
return 0;
}
Enter the number of frames: 5 Enter the frame contents for sequence number 33: CMR Enter the frame contents for sequence number 36: College Enter the frame contents for sequence number 27: of Enter the frame contents for sequence number 15: Engineering Enter the frame contents for sequence number 43: Technology The frames in sequence: 15 Engineering 27 of 33 CMR 36 College 43 Technology
Simple Program 5
Wireshark & Nmap
AIMWireshark — i. Packet Capture Using Wireshark ii. Starting Wireshark iii. Viewing Captured Traffic iv. Analysis and Statistics & Filters. v. How to run Nmap scan.
Software Required Wireshark network protocol analyzer, Nmap (Zenmap GUI optional).
Procedure
1Capture Network Packets
- Open the Wireshark application.
- Go to View → Wireless Toolbar, if required.
- Select the required wireless channel and channel width.
- Choose the appropriate network interface.
- Click Start Capture to begin capturing packets.
- Observe the packets and click Stop after some time.
2Save the Captured Packets
- Click File → Save.
- Enter a suitable file name and select the save location.
- Save the captured packets in
.pcapor.pcapngformat.
3Start Wireshark Capture
- Open Wireshark and select the required network interface.
- Click Capture → Start or use the Start button.
- Observe the captured packets in the packet list, including their source, destination, protocol, and length.
4View Captured Traffic
- Select a packet from the packet list.
- Choose a TCP packet to view its details.
- Expand the protocol layers to examine Ethernet, IP, TCP, and other available information.
- Open the packet in a separate window if required.
5Analyze Network Traffic
- Open the Analyze menu and select the required filtering option.
- Apply a suitable display filter to view specific packets.
- Right-click a TCP packet and select Follow → TCP Stream to view the TCP conversation.
- Examine the packet details to understand the communication between devices.
6Run an Nmap Scan
- Install Nmap: Windows/Mac use the installer from nmap.org; Linux:
sudo apt-get install nmap(check withnmap --version). - Ping scan – list the hosts that are online:
nmap -sP 192.168.1.0/24 - Host scan – latency, MAC address and description of each host:
nmap -sP <target IP range> - DNS / list scan – names associated with an IP:
nmap -sL <IP address> - Port scans:
-sSTCP SYN,-sTTCP connect,-sUUDP,-sYSCTP INIT,-sNTCP NULL (e.g.nmap -sS 192.168.1.1). - Version / OS detection:
nmap -sV 192.168.1.1,nmap -O 192.168.1.1(Zenmap gives the same in a GUI). - Keep Wireshark capturing during the scan to see the probe packets. Only scan machines/networks you have permission to scan.
ResultNetwork packets were successfully captured, saved, viewed and analyzed using Wireshark, and an Nmap scan was run to discover hosts and open ports.
↑ Index
List of Complex Programs
Complex Program 1
Framing Methods: Bit Stuffing and Character Stuffing
AIMImplement the data link layer framing methods such as character, character-stuffing and bit stuffing.
(a) Bit Stuffing
#include <stdio.h>
int main()
{
int a[20], b[30], i, j = 0, count = 0, 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]);
for (i = 0; i < n; i++)
{
b[j++] = a[i];
if (a[i] == 1)
count++;
else
count = 0;
if (count == 5) /* five 1's -> stuff a 0 */
{
b[j++] = 0;
count = 0;
}
}
printf("After stuffing the frame is: ");
for (i = 0; i < j; i++)
printf("%d", b[i]);
printf("\n");
return 0;
}
Enter frame length: 5 Enter input frame (0's & 1's only): 1 1 1 1 1 After stuffing the frame is: 111110
Enter frame length: 12 Enter input frame (0's & 1's only): 1 1 1 1 1 1 1 1 1 1 1 1 After stuffing the frame is: 11111011111011
(b) Character Stuffing
#include <stdio.h>
#include <string.h>
int main()
{
char a[20], b[50], ch;
int i, j, n, pos;
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");
scanf(" %c", &ch);
strcpy(b, "dlestx"); /* start of frame */
j = 6;
for (i = 0; i < n; i++)
{
if (i == pos - 1) /* insert dle ch dle */
{
strcpy(&b[j], "dle");
j += 3;
b[j++] = ch;
strcpy(&b[j], "dle");
j += 3;
}
if (a[i] == 'd' && a[i + 1] == 'l' && a[i + 2] == 'e')
{
strcpy(&b[j], "dle"); /* stuff extra dle */
j += 3;
}
b[j++] = a[i];
}
strcpy(&b[j], "dleetx"); /* end of frame */
printf("\nframe after stuffing:\n%s\n", b);
return 0;
}
Enter string CMRCET Enter position 2 Enter the character A frame after stuffing: dlestxCdleAdleMRCETdleetx
(c) Character Count
#include <stdio.h>
#include <string.h>
int main()
{
char data[10][20];
int n, i;
printf("Enter number of frames: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("Enter frame %d: ", i);
scanf("%s", data[i]);
}
/* each frame = count (length + 1) followed by the data */
printf("\nData as frames:\n");
for (i = 1; i <= n; i++)
printf("Frame %d: %d%s\n", i, (int) strlen(data[i]) + 1, data[i]);
printf("\nAt the receiver:\n");
for (i = 1; i <= n; i++)
printf("Frame %d: %s\n", i, data[i]);
return 0;
}
Enter number of frames: 3 Enter frame 1: COMPUTER Enter frame 2: NETWORKS Enter frame 3: LAB Data as frames: Frame 1: 9COMPUTER Frame 2: 9NETWORKS Frame 3: 4LAB At the receiver: Frame 1: COMPUTER Frame 2: NETWORKS Frame 3: LAB
(d) Byte Stuffing and De-stuffing
#include <stdio.h>
#include <string.h>
int main()
{
char a[50], b[150] = "dlestx", c[50] = "";
int i, n;
printf("Enter the data: ");
scanf("%49s", a);
/* ---------- Byte Stuffing ---------- */
for (i = 0; a[i] != '\0'; i++)
{
if (strncmp(a + i, "dle", 3) == 0) /* dle in data -> add extra dle */
strcat(b, "dle");
strncat(b, &a[i], 1);
}
strcat(b, "dleetx");
printf("\nFrame after stuffing : %s\n", b);
/* ---------- Byte De-stuffing ---------- */
n = strlen(b);
for (i = 6; i < n - 6; i++) /* skip dlestx and dleetx */
{
if (strncmp(b + i, "dledle", 6) == 0) /* keep only one dle */
i += 3;
strncat(c, &b[i], 1);
}
printf("Frame after de-stuffing: %s\n", c);
return 0;
}
Enter the data: CMRCET Frame after stuffing : dlestxCMRCETdleetx Frame after de-stuffing: CMRCET
Enter the data: helodlehi Frame after stuffing : dlestxhelodledlehidleetx Frame after de-stuffing: helodlehi
Complex Program 2
Distance Vector Routing
AIMImplement distance vector routing algorithm for obtaining routing tables at each node.
#include <stdio.h>
struct node
{
unsigned dist[20];
unsigned from[20];
} rt[10];
int main()
{
int dmat[20][20], n, i, j, k, count;
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
printf("\nEnter the cost matrix:\n");
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] = dmat[i][k] + rt[k].dist[j];
rt[i].from[j] = k;
count++;
}
} while (count != 0);
for (i = 0; i < n; i++)
{
printf("\n\nState value for router %d is:\n", i + 1);
for (j = 0; j < n; j++)
printf("\nNode %d via %d Distance %d", j + 1, rt[i].from[j] + 1, rt[i].dist[j]);
}
printf("\n\n");
return 0;
}
Enter the number of nodes: 3 Enter the cost matrix: 0 2 7 2 0 1 7 1 0 State value for router 1 is: Node 1 via 1 Distance 0 Node 2 via 2 Distance 2 Node 3 via 2 Distance 3 State value for router 2 is: Node 1 via 1 Distance 2 Node 2 via 2 Distance 0 Node 3 via 3 Distance 1 State value for router 3 is: Node 1 via 2 Distance 3 Node 2 via 2 Distance 1 Node 3 via 3 Distance 0
Complex Program 3
Cyclic Redundancy Check (CRC)
AIMWrite a program to compute CRC code for the polynomials CRC-12, CRC-16 and CRC-CCITT.
#include <stdio.h>
int main()
{
int data[50], gen[20], temp[70];
int dl, gl, n, i, j, ch, err = 0;
printf("1.generate 2.check\nchoice: ");
scanf("%d", &ch);
printf("Enter the length of data: ");
scanf("%d", &dl);
printf("Enter the data: ");
for (i = 0; i < dl; i++)
scanf("%d", &data[i]);
printf("Enter the length of generator: ");
scanf("%d", &gl);
printf("Enter the generator: ");
for (i = 0; i < gl; i++)
scanf("%d", &gen[i]);
/* generate: append (gl - 1) zeros to the data */
n = (ch == 1) ? dl + gl - 1 : dl;
for (i = 0; i < n; i++)
temp[i] = (i < dl) ? data[i] : 0;
/* modulo-2 division using XOR */
for (i = 0; i <= n - gl; i++)
if (temp[i] == 1)
for (j = 0; j < gl; j++)
temp[i + j] ^= gen[j];
if (ch == 1)
{
printf("crc: ");
for (i = 0; i < dl; i++)
printf("%d", data[i]);
for (i = dl; i < n; i++) /* remainder */
printf("%d", temp[i]);
}
else
{
for (i = n - gl + 1; i < n; i++)
if (temp[i] != 0)
err = 1;
printf(err ? "Error detected" : "No error detected");
}
printf("\n");
return 0;
}
1.generate 2.check choice: 1 Enter the length of data: 6 Enter the data: 1 0 1 1 0 1 Enter the length of generator: 4 Enter the generator: 1 1 0 1 crc: 101101010
1.generate 2.check choice: 2 Enter the length of data: 9 Enter the data: 1 0 1 1 0 1 0 1 0 Enter the length of generator: 4 Enter the generator: 1 1 0 1 No error detected
1.generate 2.check choice: 2 Enter the length of data: 9 Enter the data: 1 0 1 1 1 1 0 1 0 Enter the length of generator: 4 Enter the generator: 1 1 0 1 Error detected
Complex Program 4
Dijkstra's Shortest Path Algorithm
AIMImplement Dijkstra's algorithm to compute the shortest path through a network.
#include <stdio.h>
#define INFINITY 9999
#define MAX 10
void dijkstra(int G[MAX][MAX], int n, int start)
{
int cost[MAX][MAX], dist[MAX], pred[MAX], visited[MAX];
int count, min, next = start, i, j;
/* 0 means no edge -> infinity */
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cost[i][j] = (G[i][j] == 0) ? INFINITY : G[i][j];
for (i = 0; i < n; i++)
{
dist[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
}
dist[start] = 0;
visited[start] = 1;
for (count = 1; count < n - 1; count++)
{
/* node at minimum distance */
min = INFINITY;
for (i = 0; i < n; i++)
if (dist[i] < min && !visited[i])
{
min = dist[i];
next = i;
}
visited[next] = 1;
/* check if a better path exists through next */
for (i = 0; i < n; i++)
if (!visited[i] && min + cost[next][i] < dist[i])
{
dist[i] = min + cost[next][i];
pred[i] = next;
}
}
for (i = 0; i < n; i++)
if (i != start)
{
printf("\nDistance of node %d = %d", i, dist[i]);
printf("\nPath = %d", i);
j = i;
do
{
j = pred[j];
printf("<-%d", j);
} while (j != start);
}
printf("\n");
}
int main()
{
int G[MAX][MAX], i, j, n, u;
printf("Enter no. of vertices: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &G[i][j]);
printf("\nEnter the starting node: ");
scanf("%d", &u);
dijkstra(G, n, u);
return 0;
}
Enter no. of vertices: 5 Enter the adjacency matrix: 0 10 0 30 100 10 0 50 0 0 0 50 0 20 10 30 0 20 0 60 100 0 10 60 0 Enter the starting node: 0 Distance of node 1 = 10 Path = 1<-0 Distance of node 2 = 50 Path = 2<-3<-0 Distance of node 3 = 30 Path = 3<-0 Distance of node 4 = 60 Path = 4<-2<-3<-0
Complex Program 5
Leaky Bucket Algorithm
AIMWrite a program for congestion control using Leaky bucket algorithm.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int packet[5], bucket, rate, store = 0, i, t;
for (i = 0; i < 5; i++)
packet[i] = rand() % 10 + 1; /* random packet sizes */
printf("Enter output rate: ");
scanf("%d", &rate);
printf("Enter bucket size: ");
scanf("%d", &bucket);
for (i = 0; i < 5; i++)
{
if (packet[i] + store > bucket)
{
if (packet[i] > bucket)
printf("\nIncoming packet size %d is greater than bucket capacity\n", packet[i]);
else
printf("\nBucket size exceeded, packet %d dropped\n", packet[i]);
continue;
}
store += packet[i];
printf("\n--------------------------------\n");
printf("Incoming packet: %d\n", packet[i]);
printf("Transmission left: %d\n", store);
t = rand() % 5 + 1;
printf("Next packet will come at %d\n", t);
for (; t > 0 && store > 0; t--) /* send 'rate' bytes every second */
{
printf("Time left %d --- Transmitted\n", t);
store = (store < rate) ? 0 : store - rate;
printf("Bytes remaining: %d\n", store);
}
}
return 0;
}
Enter output rate: 3 Enter bucket size: 10 -------------------------------- Incoming packet: 4 Transmission left: 4 Next packet will come at 1 Time left 1 --- Transmitted Bytes remaining: 1 -------------------------------- Incoming packet: 7 Transmission left: 8 Next packet will come at 2 Time left 2 --- Transmitted Bytes remaining: 5 Time left 1 --- Transmitted Bytes remaining: 2 -------------------------------- Incoming packet: 8 Transmission left: 10 Next packet will come at 3 Time left 3 --- Transmitted Bytes remaining: 7 Time left 2 --- Transmitted Bytes remaining: 4 Time left 1 --- Transmitted Bytes remaining: 1 -------------------------------- Incoming packet: 6 Transmission left: 7 Next packet will come at 5 Time left 5 --- Transmitted Bytes remaining: 4 Time left 4 --- Transmitted Bytes remaining: 1 Time left 3 --- Transmitted Bytes remaining: 0 -------------------------------- Incoming packet: 4 Transmission left: 4 Next packet will come at 2 Time left 2 --- Transmitted Bytes remaining: 1 Time left 1 --- Transmitted Bytes remaining: 0
Additional Programs
Additional Program 1
Selective Repeat ARQ
AIMWrite a program to implement the Selective Repeat Automatic Repeat Request (ARQ) protocol for error control and retransmission of lost frames.
#include <stdio.h>
int main()
{
int n, w, x, i, j, e;
printf("Enter total number of frames: ");
scanf("%d", &n);
printf("Enter window size: ");
scanf("%d", &w);
printf("Enter the frame number to be lost (0 for none): ");
scanf("%d", &x);
for (i = 1; i <= n; i += w)
{
e = (i + w - 1 < n) ? i + w - 1 : n; /* last frame in window */
printf("\n--- Window: frames %d to %d ---\n", i, e);
for (j = i; j <= e; j++)
{
printf("Sender : Frame %d sent\n", j);
if (j == x)
printf("Receiver: Frame %d LOST\n", j);
else
printf("Receiver: Frame %d received, ACK %d sent\n", j, j);
}
/* only the lost frame is sent again */
if (x >= i && x <= e)
{
printf("Receiver: NAK %d sent (other frames are buffered)\n", x);
printf("Sender : Retransmitting only frame %d\n", x);
printf("Receiver: Frame %d received, ACK %d sent\n", x, x);
}
}
printf("\nAll %d frames delivered successfully.\n", n);
return 0;
}
Enter total number of frames: 6 Enter window size: 3 Enter the frame number to be lost (0 for none): 2 --- Window: frames 1 to 3 --- Sender : Frame 1 sent Receiver: Frame 1 received, ACK 1 sent Sender : Frame 2 sent Receiver: Frame 2 LOST Sender : Frame 3 sent Receiver: Frame 3 received, ACK 3 sent Receiver: NAK 2 sent (other frames are buffered) Sender : Retransmitting only frame 2 Receiver: Frame 2 received, ACK 2 sent --- Window: frames 4 to 6 --- Sender : Frame 4 sent Receiver: Frame 4 received, ACK 4 sent Sender : Frame 5 sent Receiver: Frame 5 received, ACK 5 sent Sender : Frame 6 sent Receiver: Frame 6 received, ACK 6 sent All 6 frames delivered successfully.
Additional Program 2
Token Bucket Algorithm
AIMWrite a program to implement Congestion control using the Token Bucket algorithm.
#include <stdio.h>
int main()
{
int c, r, n, i, t = 0, a[20];
printf("Enter bucket capacity (max tokens): ");
scanf("%d", &c);
printf("Enter token generation rate (tokens per second): ");
scanf("%d", &r);
printf("Enter number of seconds to simulate: ");
scanf("%d", &n);
printf("Enter packet size arriving at each second:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\nTime\tTokens\tPacket\tStatus\t\tTokens Left\n");
for (i = 0; i < n; i++)
{
t = (t + r > c) ? c : t + r; /* add tokens */
printf("%d\t%d\t%d\t", i + 1, t, a[i]);
if (a[i] <= t) /* enough tokens -> send */
{
t -= a[i];
printf("Sent\t\t%d\n", t);
}
else
printf("Dropped\t\t%d\n", t);
}
return 0;
}
Enter bucket capacity (max tokens): 10 Enter token generation rate (tokens per second): 4 Enter number of seconds to simulate: 6 Enter packet size arriving at each second: 3 5 8 2 12 4 Time Tokens Packet Status Tokens Left 1 4 3 Sent 1 2 5 5 Sent 0 3 4 8 Dropped 4 4 8 2 Sent 6 5 10 12 Dropped 10 6 10 4 Sent 6
Additional Program 3
IPv4 Class Identification and Subnet Calculation
AIMWrite a program to implement IPv4 class identification and subnet calculation.
#include <stdio.h>
void show(char *s, unsigned x)
{
printf("%s: %u.%u.%u.%u\n", s, x >> 24, (x >> 16) & 255, (x >> 8) & 255, x & 255);
}
int main()
{
unsigned a, b, c, d, p, ip, m, net, bc;
char cl;
char *dm[] = {"255.0.0.0", "255.255.0.0", "255.255.255.0"};
printf("Enter IP address (e.g. 192.168.10.25): ");
scanf("%u.%u.%u.%u", &a, &b, &c, &d);
printf("Enter prefix length (CIDR, e.g. 26): ");
scanf("%u", &p);
/* class from the first octet */
cl = (a < 128) ? 'A' : (a < 192) ? 'B' : (a < 224) ? 'C' : (a < 240) ? 'D' : 'E';
printf("\nClass : %c\n", cl);
if (cl <= 'C')
printf("Default mask : %s\n", dm[cl - 'A']);
ip = (a << 24) | (b << 16) | (c << 8) | d;
m = p ? ~0u << (32 - p) : 0; /* subnet mask */
net = ip & m;
bc = net | ~m;
show("Subnet mask ", m);
show("Network address ", net);
show("Broadcast address", bc);
if (p <= 30)
{
show("First host ", net + 1);
show("Last host ", bc - 1);
printf("Usable hosts : %u\n", (1u << (32 - p)) - 2);
}
return 0;
}
Enter IP address (e.g. 192.168.10.25): 192.168.10.75 Enter prefix length (CIDR, e.g. 26): 26 Class : C Default mask : 255.255.255.0 Subnet mask : 255.255.255.192 Network address : 192.168.10.64 Broadcast address: 192.168.10.127 First host : 192.168.10.65 Last host : 192.168.10.126 Usable hosts : 62
Enter IP address (e.g. 192.168.10.25): 10.20.30.40 Enter prefix length (CIDR, e.g. 26): 8 Class : A Default mask : 255.0.0.0 Subnet mask : 255.0.0.0 Network address : 10.0.0.0 Broadcast address: 10.255.255.255 First host : 10.0.0.1 Last host : 10.255.255.254 Usable hosts : 16777214
Additional Program 4
Hamming Code
AIMWrite a program to implement Hamming Code for error detection and correction in data transmission.
#include <stdio.h>
int main()
{
int c[8], r[8], i, p;
printf("Enter 4 data bits (d1 d2 d3 d4): ");
scanf("%d %d %d %d", &c[3], &c[5], &c[6], &c[7]); /* data positions */
c[1] = c[3] ^ c[5] ^ c[7]; /* parity bits */
c[2] = c[3] ^ c[6] ^ c[7];
c[4] = c[5] ^ c[6] ^ c[7];
printf("\nEncoded data (7 bits): ");
for (i = 1; i <= 7; i++)
printf("%d", c[i]);
printf("\n\nEnter received 7 bits: ");
for (i = 1; i <= 7; i++)
scanf("%d", &r[i]);
/* check bits give the error position */
p = (r[1] ^ r[3] ^ r[5] ^ r[7])
+ (r[2] ^ r[3] ^ r[6] ^ r[7]) * 2
+ (r[4] ^ r[5] ^ r[6] ^ r[7]) * 4;
if (p == 0)
printf("\nNo error detected\n");
else
{
printf("\nError detected at bit position %d\n", p);
r[p] ^= 1;
printf("Corrected data: ");
for (i = 1; i <= 7; i++)
printf("%d", r[i]);
printf("\n");
}
return 0;
}
Enter 4 data bits (d1 d2 d3 d4): 1 0 1 1 Encoded data (7 bits): 0110011 Enter received 7 bits: 0 1 1 0 1 1 1 Error detected at bit position 5 Corrected data: 0110011
Enter 4 data bits (d1 d2 d3 d4): 1 0 1 1 Encoded data (7 bits): 0110011 Enter received 7 bits: 0 1 1 0 0 1 1 No error detected
Additional Program 5
RSA Algorithm
AIMWrite a program to implement the RSA algorithm for encryption and decryption of messages.
#include <stdio.h>
#include <string.h>
long gcd(long a, long b) { return b ? gcd(b, a % b) : a; }
long pw(long b, long e, long m) /* (b^e) % m */
{
long r = 1;
for (b %= m; e > 0; e /= 2, b = b * b % m)
if (e % 2)
r = r * b % m;
return r;
}
int main()
{
long p, q, n, f, e, d, c[100];
char s[100];
int i, len;
printf("Enter two prime numbers (p q): ");
scanf("%ld %ld", &p, &q);
n = p * q;
f = (p - 1) * (q - 1); /* phi(n) */
for (e = 2; gcd(e, f) != 1; e++); /* public exponent */
for (d = 1; (d * e) % f != 1; d++); /* private exponent */
printf("\nn = %ld, phi(n) = %ld\n", n, f);
printf("Public key (e, n) = (%ld, %ld)\n", e, n);
printf("Private key (d, n) = (%ld, %ld)\n", d, n);
printf("\nEnter message: ");
scanf("%99s", s);
len = strlen(s);
printf("\nEncrypted message: ");
for (i = 0; i < len; i++)
printf("%ld ", c[i] = pw(s[i], e, n));
printf("\nDecrypted message: ");
for (i = 0; i < len; i++)
printf("%c", (char) pw(c[i], d, n));
printf("\n");
return 0;
}
Enter two prime numbers (p q): 61 53 n = 3233, phi(n) = 3120 Public key (e, n) = (7, 3233) Private key (d, n) = (1783, 3233) Enter message: HELLO Encrypted message: 1087 155 83 83 913 Decrypted message: HELLO