Cn Lab Programs
Cn Lab Programs
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
Create a Socket:
if (sockfd < 0) {
exit(EXIT_FAILURE);
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
perror("Bind failed");
close(sockfd);
exit(EXIT_FAILURE);
if (listen(sockfd, 5) < 0) {
perror("Listen failed");
close(sockfd);
exit(EXIT_FAILURE);
int new_socket;
socklen_t addr_size;
addr_size = sizeof(client_address);
if (new_socket < 0) {
perror("Accept failed");
close(sockfd);
exit(EXIT_FAILURE);
close(new_socket);
close(sockfd);
server output:
Connection accepted.
Client Output:
#include <stdio.h>
#include <string.h>
// Function prototypes
int main() {
char input[100];
char stuffed[200];
char destuffed[100];
scanf("%s", input);
charStuffing(input, stuffed);
printf("Enter the binary data for Bit Stuffing (0s and 1s): ");
scanf("%s", input);
bitStuffing(input, stuffed);
bitDestuffing(stuffed, destuffed);
return 0;
int i = 0, j = 0;
stuffed[j++] = input[i++];
}
if (stuffed[i] == ESC) {
destuffed[j++] = stuffed[i++];
int i = 0, j = 0;
int count = 0;
stuffed[j++] = input[i];
if (input[i] == '1') {
count++;
if (count == 5) {
count = 0;
} else {
count = 0;
i++;
int i = 0, j = 0;
int count = 0;
if (stuffed[i] == '1') {
count++;
} else {
count = 0;
}
destuffed[j++] = stuffed[i];
count = 0;
i++;
Out put:
Enter the binary data for Bit Stuffing (0s and 1s): 1111101111110
#include <stdio.h>
#include <string.h>
return checksum;
char received_checksum_str[33];
received_checksum_str[32] = '\0';
int main() {
append_checksum(data);
if (verify_checksum(data)) {
} else {
return 0;
Out put:
4. write a program for hamming code generation for error detection and correction
#include <stdio.h>
#include <math.h>
#include <string.h>
int parity = 0;
int main() {
int n, r, i, j;
scanf("%s", data);
n = strlen(data);
r = 0;
r++;
code[i] = '0';
}
// Insert data bits into the code array
int dataIndex = 0;
continue;
} else {
code[i] = data[dataIndex++];
// Calculate parity bits and insert them into the code array
calculate_parity_bits(code, r, n + r);
printf("%c", code[i]);
printf("\n");
return 0;
Input:
Output:
#include <stdio.h>
#include <stdint.h>
// CRC-12 Polynomial: x^12 + x^11 + x^10 + x^9 + x^7 + x^4 + x^3 + x^2 + x + 1
} else {
crc <<= 1;
} else {
crc <<= 1;
return crc;
} else {
crc <<= 1;
return crc;
int main() {
return 0;
}
Output:
CRC-12: 0xF0F
CRC-16: 0x31C3
CRC-CCITT: 0xC1C2
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
typedef struct {
int frame_number;
bool ack_received;
} Frame;
Frame sender_window[WINDOW_SIZE];
Frame receiver_window[WINDOW_SIZE];
sender_window[window_start].frame_number = i;
sender_window[window_start].ack_received = false;
if (sender_window[window_start].ack_received) {
} else {
return NULL;
if (i == last_acked + 1) {
last_acked++;
sleep(1);
return NULL;
int main() {
sender_window[i].ack_received = false;
receiver_window[i].frame_number = -1;
pthread_join(sender_thread, NULL);
pthread_join(receiver_thread, NULL);
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
typedef struct {
} Frame;
Frame sender_window[MAX_FRAME];
Frame sender_buffer[MAX_FRAME];
// Receiver-side window
Frame receiver_window[MAX_FRAME];
void initialize_windows() {
sender_window[i].frame_id = -1;
sender_window[i].ack_received = false;
sender_buffer[i].frame_id = -1;
sender_buffer[i].ack_received = false;
receiver_window[i].frame_id = -1;
receiver_window[i].ack_received = false;
void check_for_timeouts() {
simulate_timeout(sender_window[i].frame_id);
}
}
void sliding_window_protocol() {
int next_frame_to_send = 0;
int expected_frame_to_receive = 0;
send_frame(next_frame_to_send);
next_frame_to_send++;
sleep(1); // Simulate the time taken to transmit and wait for acknowledgment
while (1) {
if (!sender_window[i % MAX_FRAME].ack_received) {
check_for_timeouts();
break;
sleep(1);
}
int main() {
initialize_windows();
sliding_window_protocol();
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
}
// Function to simulate receiver receiving the frame
int main() {
int frame_number = 1;
int ack;
int retries = 0;
while (1) {
sender(frame_number);
ack = receiver(frame_number);
if (ack) {
frame_number++;
} else {
retries++;
break;
sleep(1);
printf("Transmission complete.\n");
return 0;
Output:
9. write a program to implement for congestion control using leaky bucket algorithm
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define LEAK_RATE 1 // Data leaks out at the rate of 1 packet per second
typedef struct {
} LeakyBucket;
bucket->current_level = 0;
bucket->max_capacity = max_capacity;
bucket->leak_rate = leak_rate;
} else {
bucket->current_level += data;
if (bucket->current_level > 0) {
bucket->current_level -= bucket->leak_rate;
if (bucket->current_level < 0) {
int main() {
LeakyBucket bucket;
int data_rate, i;
// Initialize the bucket with maximum capacity and leak rate
data_rate = rand() % 3 + 1;
addData(&bucket, data_rate);
leakData(&bucket);
sleep(1);
return 0;
Output:
10. write a program to implement dijkstra's algorithm to compute the shortest path through a
graph
#include <stdio.h>
#include <limits.h>
#define MAX_NODES 10
min = dist[v];
minIndex = v;
return minIndex;
int dist[MAX_NODES]; // Array to store the shortest distance from the source
int sptSet[MAX_NODES]; // Shortest path tree set (True/False)
dist[i] = INF;
sptSet[i] = 0;
dist[src] = 0;
// Pick the minimum distance vertex from the set of vertices not yet processed
sptSet[u] = 1;
if (!sptSet[v] && graph[u][v] && dist[u] != INF && dist[u] + graph[u][v] < dist[v]) {
}
// Print the shortest distances
if (dist[i] == INF) {
printf("%d\t\tINF\n", i);
} else {
printf("%d\t\t%d\n", i, dist[i]);
int main() {
scanf("%d", &n);
printf("Enter the adjacency matrix (use 0 for no path and other values for weights):\n");
scanf("%d", &graph[i][j]);
if (graph[i][j] == 0 && i != j) {
scanf("%d", &src);
return 0;
Input :
Enter the adjacency matrix (use 0 for no path and other values for weights):
0 10 0 0 0
10 0 5 0 0
0 5 0 20 1
0 0 20 0 2
00120
0 0
1 10
2 15
3 17
4 16
11. write a program distance vector routing algorithm by obtaining routing table at each node
#include <stdio.h>
int i, j, k;
distance[i][j] = cost[i][j];
}
int updated;
do {
updated = 0;
via[i][j] = k;
updated = 1;
} while (updated);
printf("Destination\tNext Hop\tCost\n");
}
}
int main() {
int cost[MAX][MAX];
int n, i, j;
scanf("%d", &n);
scanf("%d", &cost[i][j]);
distanceVectorRouting(cost, n);
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct {
int adj[MAX_HOSTS][MAX_HOSTS];
int num_hosts;
} Network;
network->num_hosts = num_hosts;
network->adj[i][j] = 0;
}
network->adj[host1][host2] = 1;
network->adj[host2][host1] = 1;
queue[rear++] = root;
visited[root] = 1;
visited[i] = 1;
printf("Broadcast Tree:\n");
if (tree[i] != -1) {
if (!last_dot) return 0;
*end_ip = atoi(end);
return 1;
int main() {
Network network;
int tree[MAX_HOSTS];
char subnet[50];
scanf("%s", subnet);
return 1;
initialize_network(&network, num_hosts);
break;
printf("Enter the root host for the broadcast tree (e.g., 1):\n");
int root_host;
scanf("%d", &root_host);
display_tree(tree, num_hosts);
return 0;
Input:
192.168.1.1-192.168.1.4
Enter connections between hosts (e.g., 192.168.1.1 192.168.1.2). Type 'done' to finish:
192.168.1.1 192.168.1.2
192.168.1.2 192.168.1.3
192.168.1.2 192.168.1.4
done
192.168.1.1
Output:
Broadcast Tree:
13. wireshark
#include <stdio.h>
#include <pcap.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/if_ether.h>
int main() {
char errbuf[PCAP_ERRBUF_SIZE]; // Buffer to hold error messages
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
return 1;
net = 0;
mask = 0;
if (handle == NULL) {
fprintf(stderr, "Could not open device %s: %s\n", dev, errbuf);
return 1;
return 1;
return 1;
// Clean up
pcap_freecode(&fp);
pcap_close(handle);
return 0;
}
// Callback function for processing packets
void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
struct ip *ip_header;
printf("\nPacket captured:\n");
printf("Ethernet Header:\n");
if (ntohs(eth_header->ether_type) == ETHERTYPE_IP) {
printf("IP Header:\n");
} else {
}
Output:
Packet captured:
Ethernet Header:
IP Header:
Protocol: 6
#include <stdio.h>
#include <stdlib.h>
int main() {
char command[256];
printf("Starting Wireshark...\n");
if (status == -1) {
return 1;
return 0;
Output:
Starting Wireshark...
#include <stdio.h>
#include <pcap.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/if_ether.h>
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
return 1;
if (handle == NULL) {
return 1;
pcap_close(handle);
return 0;
void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
struct ip *ip_header;
printf("\nPacket captured:\n");
printf("Ethernet Header:\n");
if (ntohs(eth_header->ether_type) == ETHERTYPE_IP) {
printf("IP Header:\n");
if (ip_header->ip_p == IPPROTO_TCP) {
printf("TCP Header:\n");
printf("UDP Header:\n");
} else {
Output:
Packet captured:
Ethernet Header:
IP Header:
Protocol: 6
TCP Header:
#include <stdio.h>
#include <pcap.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/if_ether.h>
void packet_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
total_count++;
if (ntohs(eth_header->ether_type) == ETHERTYPE_IP) {
switch (ip_header->ip_p) {
case IPPROTO_TCP:
tcp_count++;
break;
case IPPROTO_UDP:
udp_count++;
break;
case IPPROTO_ICMP:
icmp_count++;
break;
default:
other_count++;
} else {
other_count++;
}
int main() {
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
return 1;
net = 0;
mask = 0;
if (handle == NULL) {
return 1;
return 1;
return 1;
// Clean up
pcap_freecode(&fp);
pcap_close(handle);
return 0;
Output:
TCP packets: 85
UDP packets: 15
ICMP packets: 3
Other packets: 2
#include <stdio.h>
#include <stdlib.h>
int main() {
char target[100];
char command[150];
// Prompt the user to enter the target IP or hostname
scanf("%99s", target);
if (status == -1) {
return 1;
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
int main() {
char target[100];
char command[150];
scanf("%99s", target);
if (status == -1) {
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
int main() {
char command[100];
if (status == -1) {
return 1;
} else {
return 0;
Output:
nam out.nam
#include <stdio.h>
#include <string.h>
int main() {
FILE *traceFile;
char line[256];
int droppedPackets = 0;
if (traceFile == NULL) {
return 1;
if (strstr(line, "d")) {
droppedPackets++;
fclose(traceFile);
return 0;
}
Output:
#include <stdio.h>
#include <string.h>
int main() {
FILE *traceFile;
char line[256];
int tcpDrops = 0;
if (traceFile == NULL) {
return 1;
tcpDrops++;
}
fclose(traceFile);
return 0;
Output:
#include <stdio.h>
#include <string.h>
int main() {
FILE *traceFile;
char line[256];
int congestionDrops = 0;
if (traceFile == NULL) {
return 1;
}
congestionDrops++;
fclose(traceFile);
return 0;
Output:
#include <stdio.h>
#include <string.h>
int main() {
FILE *traceFile;
char line[256];
if (traceFile == NULL) {
return 1;
totalSent++;
totalReceived++;
fclose(traceFile);
// Compute data rate and throughput
// Display results
return 0;
Output: