EXPERIMENT 1
STUDY OF NETWORK DEVICES IN DETAILED AND CONNECT THE COMPUTERS IN LOCAL AREA
NETWORK
1. Study of Network Devices
Network Interface Card (NIC):
A Network Interface Card is a hardware component installed in
computers that allows them to connect to a network. It can be wired
(Ethernet) or wireless (Wi-Fi). NIC acts as an interface between the
computer and the physical network.
Switch:
A switch is a network device that connects multiple computers within a
LAN. It intelligently forwards data only to the specific device it is
intended for, improving efficiency and security.
Hub:
A hub is a basic device that connects multiple computers but
broadcasts incoming data to all connected devices. This makes it less
efficient and secure compared to a switch.
Router:
A router connects a LAN to other networks, such as the internet. It
routes data between local devices and external networks and often
provides additional functions like DHCP and firewall.
Access Point (AP):
An access point extends wireless network coverage, allowing wireless
devices to connect to the LAN.
Firewall:
A firewall protects the network by monitoring and controlling incoming
and outgoing traffic based on security rules, helping to prevent
unauthorized access.
________________________________
2. Connecting Computers in a Local Area Network (LAN)
Objective:
To connect multiple computers within a small area (office, lab, or
home) to enable sharing of resources such as files, printers, and
internet access.
Materials Required:
Two or more computers with Network Interface Cards (NICs)
Switch (or Router with multiple LAN ports)
Ethernet cables (Cat5e or Cat6)
Optional: Router (for internet access)
Procedure:
Physical Connection:
Connect each computer to the switch using Ethernet cables. If using a
router, connect the switch to the router to enable internet access.
IP Address Configuration:
Configure each computer with an IP address in the same subnet. This
can be done automatically via DHCP (if enabled on the router) or
manually by assigning static IP addresses. For example:
Computer 1: IP - 192.168.1.2, Subnet Mask - 255.255.255.0
Computer 2: IP - 192.168.1.3, Subnet Mask - 255.255.255.0
Workgroup Configuration (for Windows OS):
Set all computers to the same workgroup name (e.g., "MYLAN") to
facilitate easier resource sharing.
Enable File and Printer Sharing:
On each computer, enable network discovery and file/printer sharing settings.
Testing the Network:
Use the command prompt to ping other computers in the network to
verify connectivity. For example:
ping 192.168.1.3
Successful replies indicate proper connection.
EXPERIMENT 2
Write a Program to implement the data link layer framing methods such as
i) Character Stuffing
ii) Bit Stuffing"
#include <stdio.h>
#include <string.h>
void characterStuffing(char data[]) {
char stuffed[100];
char start_end = 'F'; // flag byte
char esc = 'E'; // escape character
int i, j = 0;
stuffed[j++] = start_end; // Start Flag
for (i = 0; i < strlen(data); i++) {
if (data[i] == start_end || data[i] == esc) {
stuffed[j++] = esc; // insert escape character
}
stuffed[j++] = data[i];
}
stuffed[j++] = start_end; // End Flag
stuffed[j] = '\0';
printf("\nCharacter Stuffing Result: %s\n", stuffed);
}
void bitStuffing(char data[]) {
char stuffed[200];
int i, j = 0, count = 0;
stuffed[j++] = 'F'; // Start Flag
for (i = 0; i < strlen(data); i++) {
stuffed[j++] = data[i];
if (data[i] == '1') {
count++;
if (count == 5) {
stuffed[j++] = '0'; // Stuff a 0 after five consecutive 1s
count = 0;
}
} else {
count = 0;
}
}
stuffed[j++] = 'F'; // End Flag
stuffed[j] = '\0';
printf("\nBit Stuffing Result: %s\n", stuffed);
}
int main() {
char data[100];
printf("Enter the data (string for Character Stuffing): ");
scanf("%s", data);
characterStuffing(data);
printf("\nEnter the bit stream (only 0s and 1s for Bit Stuffing): ");
scanf("%s", data);
bitStuffing(data);
return 0;
}
OUTPUT
Enter the data (string for Character Stuffing): FEED
Character Stuffing Result: FFEEEDFF
Enter the bit stream (only 0s and 1s for Bit Stuffing): 1111101111
Bit Stuffing Result: F111110011110F
EXPERIMENT 3
WRITE A PROGRAM TO IMPLEMENT DATA LINK LAYER FARMING METHOD
CHECKSUM
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define FLAG 0x7E
#define ESC 0x7D
#define XOR_BYTE 0x20
uint8_t checksum(const uint8_t *data, size_t len) {
unsigned int sum = 0;
for (size_t i = 0; i < len; i++) sum += data[i];
return (uint8_t)(~(sum & 0xFF));
size_t stuff(const uint8_t *in, size_t len, uint8_t *out) {
size_t j = 0;
for (size_t i = 0; i < len; i++) {
if (in[i] == FLAG || in[i] == ESC) {
out[j++] = ESC;
out[j++] = in[i] ^ XOR_BYTE;
} else out[j++] = in[i];
return j;
size_t destuff(const uint8_t *in, size_t len, uint8_t *out) {
size_t j = 0;
for (size_t i = 0; i < len; i++) {
if (in[i] == ESC && i + 1 < len) {
out[j++] = in[++i] ^ XOR_BYTE;
} else out[j++] = in[i];
return j;
void print_hex(const uint8_t *data, size_t len) {
for (size_t i = 0; i < len; i++) printf("%02X ", data[i]);
puts("");
int main(void) {
const char *text = "Hello~World}";
size_t len = strlen(text);
uint8_t payload[256], stuffed[512], frame[514], destuffed[256];
memcpy(payload, text, len);
printf("Payload: %s\nHex: ", text);
print_hex(payload, len);
payload[len] = checksum(payload, len);
size_t payload_len = len + 1;
size_t stuffed_len = stuff(payload, payload_len, stuffed);
frame[0] = FLAG;
memcpy(frame + 1, stuffed, stuffed_len);
frame[stuffed_len + 1] = FLAG;
printf("\nTransmitted frame: ");
print_hex(frame, stuffed_len + 2);
size_t rx_len = destuff(frame + 1, stuffed_len, destuffed);
uint8_t rx_chk = destuffed[rx_len - 1];
printf("\nReceived payload: ");
for (size_t i = 0; i < rx_len - 1; i++) putchar(destuffed[i]);
printf("\nChecksum: %02X -> %s\n",
rx_chk,
(checksum(destuffed, rx_len - 1) == rx_chk) ? "OK" : "FAILED");
return 0;
OUTPUT
Payload: Hello~World}
Hex: 48 65 6C 6C 6F 7E 57 6F 72 6C 64 7D
Transmitted frame: 7E 48 65 6C 6C 6F 7D 5E 57 6F 72 6C 64 7D 5D A7 7E
Received payload: Hello~World}
Checksum: A7 -> OK
EXPERIMENT 4
WRITE A PROGRAM FOR HAMMING CODE GENERATION FOR ERROR DETECTION AND
CORRECTION
#include <stdio.h>
void generate(int d[4], int c[7]) {
c[2] = d[0]; c[4] = d[1]; c[5] = d[2]; c[6] = d[3];
c[0] = c[2] ^ c[4] ^ c[6]; // p1
c[1] = c[2] ^ c[5] ^ c[6]; // p2
c[3] = c[4] ^ c[5] ^ c[6]; // p4
}
int correct(int c[7]) {
int p1 = c[0] ^ c[2] ^ c[4] ^ c[6];
int p2 = c[1] ^ c[2] ^ c[5] ^ c[6];
int p4 = c[3] ^ c[4] ^ c[5] ^ c[6];
int pos = p1 * 1 + p2 * 2 + p4 * 4; // error position
if (pos) {
printf("Error at bit position %d → corrected.\n", pos);
c[pos - 1] ^= 1; // flip bit
} else {
printf("No error detected.\n");
}
return pos;
}
void printBits(const char *msg, int arr[], int n) {
printf("%s", msg);
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
puts("");
}
int main() {
int d[4], c[7], pos;
printf("Enter 4 data bits: ");
for (int i = 0; i < 4; i++) scanf("%d", &d[i]);
generate(d, c);
printBits("\nGenerated Hamming code: ", c, 7);
printf("Enter error position (1-7), 0 = no error: ");
scanf("%d", &pos);
if (pos >= 1 && pos <= 7) c[pos - 1] ^= 1; // inject error
printBits("Received code: ", c, 7);
correct(c);
printBits("Corrected code: ", c, 7);
printf("Decoded data bits: %d %d %d %d\n", c[2], c[4], c[5], c[6]);
return 0;
}
OUTPUT
Enter 4 data bits: 1 0 1 1
Generated Hamming code: 0 1 1 0 0 1 1
Enter error position (1-7), 0 = no error: 5
Received code: 0 1 1 0 1 1 1
Error at bit position 5 → corrected.
Corrected code: 0 1 1 0 0 1 1
Decoded data bits: 1 0 1 1
EXPERIMENT 8
WRITE A PROGRAM TO IMPLEMENT STOP AND WAIT PROTOCOL
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int frames, i;
srand(time(0)); // seed for random ACK/NACK
printf("Enter number of frames to send: ");
scanf("%d", &frames);
printf("\n--- Stop and Wait Protocol Simulation ---\n\n");
for (i = 1; i <= frames; i++) {
printf("Sender: Sending Frame %d...\n", i);
int ack = rand() % 2; // 0 = lost (NACK), 1 = received (ACK)
if (ack) {
printf("Receiver: Frame %d received successfully.\n", i);
printf("Receiver: Sending ACK for Frame %d.\n\n", i);
} else {
printf("Receiver: Frame %d lost or corrupted.\n", i);
printf("Sender: Timeout! Resending Frame %d...\n", i);
printf("Receiver: Frame %d received successfully.\n", i);
printf("Receiver: Sending ACK for Frame %d.\n\n", i);
} printf("--- All Frames Sent Successfully ---\n");
return 0;
OUTPUT
Enter number of frames to send: 4
--- Stop and Wait Protocol Simulation ---
Sender: Sending Frame 1...
Receiver: Frame 1 received successfully.
Receiver: Sending ACK for Frame 1.
Sender: Sending Frame 2...
Receiver: Frame 2 lost or corrupted.
Sender: Timeout! Resending Frame 2...
Receiver: Frame 2 received successfully.
Receiver: Sending ACK for Frame 2.
Sender: Sending Frame 3...
Receiver: Frame 3 received successfully.
Receiver: Sending ACK for Frame 3.
Sender: Sending Frame 4...
Receiver: Frame 4 received successfully.
Receiver: Sending ACK for Frame 4.
--- All Frames Sent Successfully ---
OUTPUT 2
Enter number of frames to send: 3
--- Stop and Wait Protocol Simulation ---
Sender: Sending Frame 1...
Receiver: Frame 1 lost or corrupted.
Sender: Timeout! Resending Frame 1...
Receiver: Frame 1 received successfully.
Receiver: Sending ACK for Frame 1.
Sender: Sending Frame 2...
Receiver: Frame 2 received successfully.
Receiver: Sending ACK for Frame 2.
Sender: Sending Frame 3...
Receiver: Frame 3 received successfully.
Receiver: Sending ACK for Frame 3.
--- All Frames Sent Successfully ---
EXPERIMENT 10
WRITE A PROGRAM TO IMPLEMENT DIJKSTRA’S ALGORITHM TO COMPUTE THE SHORTEST PATH
THROUGH A GRAPH
#include <stdio.h>
#include <limits.h>
#define V 10 // maximum number of vertices
int minDistance(int dist[], int visited[], int n) {
int min = INT_MAX, min_index = -1;
for (int v = 0; v < n; v++) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
return min_index;
void dijkstra(int graph[V][V], int n, int src) {
int dist[V]; // dist[i] will hold shortest distance from src to i
int visited[V]; // visited[i] = 1 if vertex i is finalized
for (int i = 0; i < n; i++) {
dist[i] = INT_MAX;
visited[i] = 0;
dist[src] = 0;
for (int count = 0; count < n - 1; count++) {
int u = minDistance(dist, visited, n);
visited[u] = 1;
for (int v = 0; v < n; v++) {
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
printf("\nVertex \t Distance from Source %d\n", src);
for (int i = 0; i < n; i++) {
printf("%d \t\t %d\n", i, dist[i]);
int main() {
int n;
int graph[V][V];
printf("Enter number of vertices: ");
scanf("%d", &n);
printf("Enter adjacency matrix (0 if no edge):\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &graph[i][j]);
int src;
printf("Enter source vertex: ");
scanf("%d", &src);
dijkstra(graph, n, src);
return 0;
}
OUTPUT
Enter number of vertices: 5
Enter adjacency matrix (0 if no edge):
0 10 0 5 0
00120
00004
03902
70600
Enter source vertex: 0
Vertex Distance from Source 0
0 0
1 8
2 9
3 5
4 7