Rajat CN_LAB_FILE
Rajat CN_LAB_FILE
COMPUTER NETWORK
DELHI TECHNOLOGICAL
UNIVERSITY
(FORMERLY Delhi College of Engineering)
Bawana Road, Delhi-110042
Aim : Write a program to implement Character stuffing , Bit stuffing , Byte Stuffing
Theory : These techniques are used in data link layer protocols to prevent
misinterpretation of data as control information.
• Character stuffing inserts special characters to distinguish data from
control flags.
• Bit stuffing adds a 0 after five consecutive 1s to prevent false flag
detection in bit- oriented protocols.
• Byte stuffing is similar to character stuffing but works with bytes,
commonly used in byte-oriented protocols like PPP.
#include
<iostream> #include
<string> using
namespace std;
stuffed += flag;
return stuffed;
}
int main() {
string data;
cout < "Enter the data: ";
cin > data;
return stuffed;
}
int main() {
string data;
cout < "Enter the binary
data: "; cin > data;
return 0;
}
OUTPUT:
CODE Byte Stuffing
#include
<iostream>
#include <string>
using namespace
std;
stuffed += flag;
return stuffed;
}
int main() {
string data;
cout < "Enter the data string (use capital
letters): "; cin > data;
return 0;
}
OUTPUT:
EXPERIMENT – 02
AIM: Execute the following networking command:
arp , ipconfig, hostname, netdiag, netstart, nslookup, pathping, ping,
route,tracert
Theory :
Comman Purpose
d
int main() {
cout < "Networking Commands Execution:\n";
runCommand("ipconfig");
runCommand("hostname");
runCommand("nslookup
google.com"); runCommand("ping
8.8.8.8"); runCommand("tracert
google.com");
runCommand("pathping
google.com"); runCommand("route
print"); runCommand("netstat");
runCommand("net start");
runCommand("arp -a");
return 0;
}
OUTPUT:
Windows IP Configuration
. :
IPv6 Address. . . . . . . . . . . : 2409:40d0:1039:8c93:fa4a:39a4:4953:2207
Temporary IPv6 Address. . . . . . :
2409:40d0:1039:8c93:dc3:bca6:a88:9891 Link-local IPv6 Address
. . . . . : fe80 9149:be89:f6e1:ce2d%18
IPv4 Address. . . . . . . . . . . : 192.168.124.216
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : fe80 20d7:f3ff:fe7a:1c7%18
192.168.124.46
Non-authoritative answer:
Name: google.com
Addresses: 2404:6800:4002:80f
200e 142.250.193.14
data:
Reply from 8.8.8.8: bytes=32 time=31ms TTL=53
Reply from 8.8.8.8: bytes=32 time=33ms
TTL=53 Reply from 8.8.8.8: bytes=32
time=26ms TTL=53 Reply from 8.8.8.8:
bytes=32 time=53ms TTL=53
1 2 ms 2 ms 2 ms 2409:40d0:1039:8c93 a0
2 34 ms 19 ms 14 ms 2405:200:5202:20:3924:0:
3:53
3 69 ms 14 ms 14 ms 2405:200:5202:20:3925
ff09
4 * * * Request timed out.
5 * * * Request timed out.
6 77 ms 37 ms 40 ms 2405:200:801:300 f54
7 * * * Request timed out.
EXPERIMENT – 03
AIM: Write a program to find the cyclic redundancy check for the given
data
CODE:
#include <iostream>
#include <string>
using namespace std;
// Modulo-2 Division
string mod2Division(string dividend, string divisor) {
int pick = divisor.length();
string temp = dividend.substr(0, pick);
// Final step
if (temp[0] == '1') {
temp = xorOperation(divisor, temp);
} else {
temp = xorOperation(string(pick, '0'), temp);
}
return temp;
}
return codeword;
}
int main() {
string data, key;
cout << "Enter binary data: ";
cin >> data;
return 0;
}
Output:
EXPERIMENT – 04
Theory:
• Purpose: Dijkstra's algorithm is used to find the shortest path from
a single source vertex to all other vertices in a weighted graph with
non-negative edge weights.
• Mechanism: It works by maintaining a set of visited nodes and
iteratively selecting the unvisited node with the smallest tentative
distance, updating the distances to its neighbors.
• Time Complexity: The algorithm has a time complexity of O(V^2) with a
simple implementation or O((V + E) log V) using a priority queue,
where V is the number of vertices and E is the number of edges.
• Application: It is widely used in network routing protocols and GPS
navigation systems to determine the most efficient paths.
CODE:
#include
<iostream>
#include <vector>
#include <climits>
using namespace
std;
int main() {
int V, E;
cout < "Enter number of
vertices: "; cin > V;
cout < "Enter number of
edges: "; cin > E;
int source;
cout < "Enter the source vertex:
"; cin >source;
Theory:
• Purpose: Kruskal's algorithm is used to find the minimum
spanning tree (MST) of a connected, undirected, and weighted
graph, ensuring the total edge weight is minimized.
• Mechanism: It works by sorting all edges by weight in ascending order
and greedily adding the smallest edge that does not create a cycle, using
a disjoint-set data structure to track connected components.
• Time Complexity: The algorithm has a time complexity of O(E log E) or
O(E log V) with an efficient union-find data structure, where E is the
number of edges and V is the number of vertices.
• Application: It is commonly applied in network design, such as
laying cables or building roads, to minimize costs.
CODE:
#include
<iostream>
#include
<vector>
#include
<algorithm>
using namespace
std;
struct Edge {
int u, v, weight;
};
int find(int x) {
if (parent[x] 2 x)
parent[x] = find(parent[x]); / Path
compression return parent[x];
}
parent.resize(V);
dsuRank.resize(V, 0);
for (int i = 0; i < V;
i+ ) parent[i] = i;
vector<Edge>
mst; int
totalWeight = 0;
cout < "Total weight of MST: " < totalWeight < endl;
}
int main() {
int V, E;
cout < "Enter number of
vertices: "; cin > V;
cout < "Enter number of
edges: "; cin > E;
vector<Edge> edges(E);
cout < "Enter edges as: source
destination weight\n"; for (int i = 0; i < E;
i+ ) {
cin > edges[i].u > edges[i].v >
edges[i].weight;
}
kruskal(V, edges);
return 0;
}
OUTPUT:
EXPERIMENT – 06
🔹 Working Principle:
• Based on the Bellman-Ford Algorithm.
• Routers exchange distance vectors with neighbors.
• They update their tables if a shorter route is discovered.
CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cout < "Enter number of routers:
"; cin > n;
return 0;
}
OUTPUT
EXPERIMENT-07
CODE:
#include
<iostream>
#include <vector>
#include
<climits> using
namespace std;
dist[src] = 0;
visited[u] = true;
int main() {
int V;
cout < "Enter number of
routers: "; cin > V;
return 0;
}
OUTPUT:
EXPERIMENT – 08
AIM: Write a program to simulate stop and wait protocol
Theory: The Stop and Wait Protocol is a simple flow control protocol
used in data communication. It ensures reliable transmission by sending
one frame at a time and waiting for an acknowledgment (ACK) before
sending the next.
🔹 Working:
1. Sender transmits one frame.
2. Waits for ACK from receiver.
3. If ACK is received → send next frame.
4. If no ACK (timeout) → resend the same frame.
It prevents frame loss or errors and maintains a one-at-a-time
transmission, making it simple but slower for long-distance or high-
speed networks.
CODE:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <unistd.h> / for sleep()
using namespace std;
if (ack)
{ "ACK received for Frame i < "\
cout < " < n";
} else "ACK lost! Resending i " \
{ Frame " < < . n";
cout Resend same frame
} < i-
cout < "\nAll frames sent successfully!\
n";
}
int main() {
srand(time(0));
int n;
cout < "Enter number of frames to
send: "; cin > n;
sender(n);
return 0;
}
OUTPUT:
EXPERIMENT- 09
AIM: Write a program to simulate the sliding window protocol
Theory: The Sliding Window Protocol is an efficient method of flow
control used in data communication systems. Unlike Stop-and-Wait, it
allows multiple frames to be sent before needing an ACK, improving
bandwidth utilization.
🔹 Key Concepts:
• Window Size (N): Number of frames that can be sent without waiting
for ACK.
• Sender Window: Slides forward as ACKs are received.
CODE:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <unistd.h> / For sleep()
using namespace std;
int main() {
srand(time(0));
int totalFrames, windowSize;
slidingWindow(totalFrames, windowSize);
return 0;
}
OUTPUT:
EXPERIMENT – 10
AIM: Write a program to implement a client-server architecture.
Theory: Client-Server Architecture is a network model where:
• The server provides services/resources.
• The client requests services from the server.
This architecture is widely used in networking for web services, database access,
etc.
🔹 Key Features:
• Client initiates communication.
• Server waits for incoming connections.
• Based on sockets, using protocols like TCP/IP.
• Common in chat apps, file transfers, websites, etc.
CODE:
server.cpp
#include <iostream>
#include <cstring>
#include <unistd.h>
#include
<sys/socket.h>
#include
<netinet/in.h> #define
PORT 8080
int main() {
int server_fd,
new_socket; char
buffer[1024] = {0};
struct sockaddr_in
address; int opt = 1;
int addrlen = sizeof(address);
/ Create socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd = 0) {
perror("Socket
failed");
exit(EXIT_FAILURE);
}
/ Bind address and port
address.sin_family = AF_INET;
address.sin_addr.s_addr =
INADDR_ANY; address.sin_port =
htons(PORT);
/ Listen for
connections
listen(server_fd, 3);
cout < "Server listening on port " < PORT < ". \
n";
/ Accept client
new_socket = accept(server_fd, (struct sockaddr *
&address, (socklen_t* &addrlen);
/ Send reply
const char *msg = "Hello from server!";
send(new_socket, msg, strlen(msg), 0);
close(server_fd);
Client.cpp
#include <iostream>
#include <unistd.h>
#include
<sys/socket.h>
#include
<arpa/inet.h>
#include <cstring>
#define PORT 8080
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
/ Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout < "Socket creation error\n";
return -1;
}
serv_addr.sin_family =
AF_INET; serv_addr.sin_port =
htons(PORT);
/ Connect to server
connect(sock, (struct sockaddr * &serv_addr,
sizeof(serv_addr));
/ Send message
const char *hello = "Hello from
client!"; send(sock, hello,
strlen(hello), 0);
/ Receive reply
read(sock, buffer,
1024);
cout < "Message from server: " < buffer < endl;
close(sock);
return 0;
OUTPUT: