AI&DS - 22CSL57 - CN Lab Manual
AI&DS - 22CSL57 - CN Lab Manual
// client.c
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
.sin_family = AF_INET,
.sin_port = htons(7891),
.sin_addr.s_addr = inet_addr("127.0.0.1")
};
scanf("%s", fname);
printf("Response:\n");
printf("%s", buffer);
} else {
perror("Connection failed");
close(soc);
return 0;
//server.c
#include <stdio.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
listen(welcome, 5);
if (fd < 0) {
} else {
int n;
close(fd);
close(new_soc);
close(welcome);
printf("Request completed.\n");
return 0;
OUTPUT
Client. C
SERVER.C
2. Implement the data link layer farming methods such as character, character stuffing
and bit stuffing.
void character_stuffing(const char *input, char *output, char ch, int pos) {
const char *dle = "dle";
strcpy(output, "dlestx");
int j = 6;
int main() {
char input[20], output[50];
int pos;
char ch;
Enter String:
haiarchana
Enter position:
4
Enter the Character
K
Frame after stuffing:
Dlestxhaidlekdlearchanadleetx
#include <stdio.h>
}
printf("After stuffing: ");
for (int i = 0; i < j; i++) printf("%d", output[i]);
printf("\n");
}
int main() {
int n, input[20];
printf("Enter frame length and bits: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &input[i]);
bit_stuffing(input, n);
return 0;
}
OUTPUT
3. Write a C program to implement on a data set characters the three CRC polynomials
– CRC 12, CRC 16, and CRC CCIP.
int main() {
int data[50], div[16], rem[16];
int datalen = 0, divlen = 0;
// Input data
printf("Enter the data (0s and 1s): ");
COMPUTER NETWORKS LABORATORY
22CSL57
char ch;
while ((ch = getchar()) != '\n')
data[datalen++] = ch - '0';
// Input divisor
printf("Enter the divisor (0s and 1s): ");
while ((ch = getchar()) != '\n')
div[divlen++] = ch - '0';
// Perform division
for (int i = divlen; i <= datalen + divlen - 1; i++) {
if (rem[0] == 1) {
for (int j = 1; j < divlen; j++)
rem[j - 1] = rem[j] ^ div[j];
} else {
for (int j = 1; j < divlen; j++)
rem[j - 1] = rem[j];
}
rem[divlen - 1] = data[i];
}
return 0;
}
OUTPUT:
typedef struct {
int num;
char str[50];
} Frame;
arr[i] = arr[j];
arr[j] = temp;
int main() {
int n;
COMPUTER NETWORKS LABORATORY
22CSL57
scanf("%d", &n);
Frame arr[n];
sort(arr, n);
printf("Sorted frames:\n");
return 0;
OUTPUT
5. Write a program for Hamming Code generation for error detection and correction.
#include<stdio.h>
void main() {
int data[7], dataAtRec[7], c1, c2, c3, c, i;
if (c == 0) {
printf("\nNo error detected.\n");
} else {
printf("\nError at position %d\n", c + 1);
// Correct the error
dataAtRec[7 - c] = (dataAtRec[7 - c] == 0) ? 1 : 0;
6. Take an example subnet graph with weights indicating delay between nodes. Now
obtain Routing table at each node using distance vector routing algorithm.
#include <stdio.h>
int main() {
int nodes, cost[10][10];
printf("Enter number of nodes: ");
scanf("%d", &nodes);
printf("Enter cost matrix:\n");
for (int i = 0; i < nodes; i++)
for (int j = 0; j < nodes; j++)
scanf("%d", &cost[i][j]);
}
OUTPUT
#include <stdio.h>
int main() {
scanf("%d", &line);
scanf("%d", &packetRate[i]);
COMPUTER NETWORKS LABORATORY
22CSL57
printf("Input from line %d with rate %d added to the bucket. Current size: %d\n", i + 1,
packetRate[i], currentSize);
} else {
printf("Input from line %d with rate %d discarded. Current size: %d\n", i + 1, packetRate[i],
currentSize);
currentSize = 0;
} else {
currentSize -= outputRate;
printf("Packets sent at output rate %d. Remaining bucket size: %d\n", outputRate, currentSize);
return 0;
OUTPUT
8) Using RSA algorightm Encrypt the text and Decrypt the Same.
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <stdio.h>
#include <string.h>
void handle_errors() {
ERR_print_errors_fp(stderr);
abort();
BN_free(bn);
return rsa;
COMPUTER NETWORKS LABORATORY
22CSL57
int main() {
printf("%02x", encrypted[i]);
printf("\n");
decrypted[decrypted_length] = '\0';
COMPUTER NETWORKS LABORATORY
22CSL57
// Clean up
RSA_free(rsa);
return 0;
9) a) Write a NS3 program to connect two nodes with a point-to-point link, which have a unique
interface. Analyze the network performance using UDP client server.
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
CommandLine cmd;
cmd.Parse(argc, argv);
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper pointToPoint;
COMPUTER NETWORKS LABORATORY
22CSL57
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
InternetStackHelper internet;
internet.Install(nodes);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
UdpServerHelper server(port);
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(10.0));
client.SetAttribute("MaxPackets", UintegerValue(320));
client.SetAttribute("Interval", TimeValue(Seconds(0.05)));
client.SetAttribute("PacketSize", UintegerValue(1024));
COMPUTER NETWORKS LABORATORY
22CSL57
clientApp.Start(Seconds(2.0));
clientApp.Stop(Seconds(10.0));
// Enable tracing
pointToPoint.EnablePcapAll("point-to-point-udp");
Simulator::Run();
Simulator::Destroy();
return 0;
9) b) Write NS3 program to configure two nodes on an 802.11b physical layer, 802.11b NICs in AD
hoc mode, and by default, sends one packet of 1000(applications) bytes to the other node. The
physical layer is configured to receive at a fixed RSS (regardless of distance and the transmit power);
therefore changing the position of the nodes has no effect. Analyze the performance.
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/mobility-helper.h"
COMPUTER NETWORKS LABORATORY
22CSL57
#include "ns3/wifi-module.h"
#include "ns3/applications-module.h"
CommandLine cmd;
cmd.Parse(argc, argv);
NodeContainer nodes;
nodes.Create(2);
phy.SetChannel(channel.Create());
phy.Set("RxGain", DoubleValue(0));
phy.Set("CcaMode1Threshold", DoubleValue(-64));
phy.Set("TxPowerStart", DoubleValue(16));
phy.Set("TxPowerEnd", DoubleValue(16));
phy.Set("RxNoiseFigure", DoubleValue(7));
phy.Set("FixedRssDbm", DoubleValue(-50));
WifiHelper wifi;
COMPUTER NETWORKS LABORATORY
22CSL57
wifi.SetStandard(WIFI_STANDARD_80211b);
WifiMacHelper mac;
mac.SetType("ns3::AdhocWifiMac");
InternetStackHelper internet;
internet.Install(nodes);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase("10.1.2.0", "255.255.255.0");
MobilityHelper mobility;
mobility.SetPositionAllocator("ns3::GridPositionAllocator",
"MinX", DoubleValue(0.0),
"MinY", DoubleValue(0.0),
"DeltaX", DoubleValue(5.0),
"DeltaY", DoubleValue(0.0),
"GridWidth", UintegerValue(2),
"LayoutType", StringValue("RowFirst"));
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(nodes);
UdpServerHelper server(9);
serverApp.Start(Seconds(1.0));
serverApp.Stop(Seconds(10.0));
client.SetAttribute("MaxPackets", UintegerValue(1));
client.SetAttribute("Interval", TimeValue(Seconds(1.0)));
client.SetAttribute("PacketSize", UintegerValue(1000));
ApplicationContainer clientApp=client.Install(nodes.Get(0));
clientApp.Start(Seconds(2.0));
clientApp.Stop(Seconds(10.0));
// Enable tracing
phy.EnablePcap("wifi-adhoc", devices);
// Run simulation
Simulator::Run();
Simulator::Destroy();
return 0