Dccn lab 2024 (1)
Dccn lab 2024 (1)
Assistant Professor
OF COMPUTER SCIENCE DEPARTMENT
&
Ms.S.SIREESHA(SWTA)
OF COMPUTER SCIENCE DEPARTMENT
Module II: Network simulator (NS)/ C++
**Error detection** involves identifying whether an error has occurred during transmission.
One common method used is the **parity check**. In this technique, a parity bit is added to
the data before transmission. The parity bit can be either even or odd, depending on the chosen
method. After the data is received, the parity is checked to see if it matches the expected value.
If it does not, an error is detected. Another popular error detection method is the **Cyclic
Redundancy Check (CRC)**, where a sequence of redundant bits is appended to the data,
which the receiver then uses to detect errors.
**Error correction** goes a step further by not only detecting the error but also correcting it.
The **Hamming code** is an example of an error correction method. It works by adding
redundant bits to the data in a way that allows the receiver to determine both the presence and
the location of an error, enabling the system to correct the error automatically. This process
ensures that the data is accurate without needing to resend it.
These techniques are crucial in various fields, such as telecommunications, data storage, and
computer networks, where the accuracy of data is paramount. By using error detection and
correction techniques, systems can significantly reduce the impact of errors, ensuring that
communication is more reliable and efficient.
CODE:
1. Parity Bit (Error Detection)
#include <iostream>
#include <string>
char calculateParityBit(std::string data) {
int count = 0;
for (char bit : data) {
if (bit == '1') {
count++;
}
}
return (count % 2 == 0) ? '0' : '1';
}
int main() {
std::string data = "1010101";
char parityBit = calculateParityBit(data);
return hammingCode;
}
int detectAndCorrectError(std::vector<int>& hammingCode) {
int n = hammingCode.size();
int r = 0;
while ((1 << r) < (n + 1)) {
r++;
}
int errorPosition = 0;
return errorPosition;
}
int main() {
std::vector<int> data = {1, 0, 1, 1};
std::vector<int> hammingCode = createHammingCode(data);
std::cout << "Hamming Code: ";
for (int bit : hammingCode) {
std::cout << bit;
}
std::cout << "\n";
hammingCode[5] ^= 1;
if (errorPosition == 0) {
std::cout << "No errors detected.\n";
} else {
std::cout << "Error detected at position: " << errorPosition << "\n";
std::cout << "Corrected Hamming Code: ";
for (int bit : hammingCode) {
std::cout << bit;
}
std::cout << "\n";
}
return 0;
}
O/P:
Hamming Code: 1010011
Error detected at position: 4
Corrected Hamming Code: 1011001
if (tmp[0] == '1') {
tmp = xorStrings(divisor, tmp);
} else {
tmp = xorStrings(std::string(pick, '0'), tmp);
}
return tmp;
}
int main() {
std::string data = "1101011111";
std::string divisor = "10011";
std::string encodedData = encodeCRC(data, divisor);
std::cout << "Encoded Data: " << encodedData << "\n";
bool isValid = verifyCRC(encodedData, divisor);
std::cout << "CRC Verification: " << (isValid ? "No Error" : "Error Detected") <<
"\n";
encodedData[4] = (encodedData[4] == '0') ? '1' : '0';
isValid = verifyCRC(encodedData, divisor);
std::cout << "CRC Verification after Error: " << (isValid ? "No Error" : "Error
Detected") << "\n";
return 0;
}
O/P:
Encoded Data: 11010111110010
CRC Verification: No Error
CRC Verification after Error: Error Detected
Sender Receiver
Sending Frame : 1
---
Timeout
Sending Frame : 1 Received Frame : 1
Delayed Ack
Sending Frame : 1 ---
Timeout
Sending Frame : 1 ---
Timeout
Sending Frame : 1 Received Frame : 1 Duplicate
Acknowledgement : 1
Sending Frame : 2 Received Frame : 2
Acknowledgement : 2
Sending Frame : 3
B)Sliding Window Protocol - Selective Repeat:
#include <iostream>
#include <thread>
#include <chrono>
#include <unordered_map>
using namespace std;
const int WINDOW_SIZE = 4;
#include<bits/stdc++.h>
#include<ctime>
#define ll long long int
using namespace std;
int main() {
ll tf, N, tt = 0;
srand(time(NULL));
cout << "Enter the Total number of frames : ";
cin >> tf;
cout << "Enter the Window Size : ";
cin >> N;
ll i = 1;
transmission(i, N, tf, tt);
cout << "Total number of frames which were sent and resent are : " << tt <<
endl;
return 0;
}
O/P:
nter the Total number of frames : 55
Enter the Window Size : 5
Sending Frame 1...
Sending Frame 2...
Sending Frame 3...
Sending Frame 4...
Sending Frame 5...
Timeout!! Frame Number : 1 Not Received
Retransmitting Window...
Total number of frames which were sent and resent are : 199
B) SELECTIVE REPEAT:
#include <iostream>
#include <thread>
#include <chrono>
#include <unordered_map>
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
int main() {
std::string data = "10101010";
std::string frame = addHDLCFrame(data);
return 0;
}
O/P:
HDLC Frame: 011111100000000100000011101010100010010101111110
Extracted Data: 000000010000001110101010
1 ECHO
CODE: ECHO SERVER
import java.io.*;
import java.net.*;
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.equalsIgnoreCase("EXIT")) {
System.out.println("Client requested to exit.");
break;
}
String response = processCommand(inputLine);
out.println(response);
}
} catch (IOException e) {
System.err.println("Error handling client: " + e.getMessage());
}
}
} catch (IOException e) {
System.err.println("Error starting server: " + e.getMessage());
}
}
while (true) {
System.out.print("Enter command (PING, TALK, ECHO, EXIT): ");
userInputLine = userInput.readLine();
if (userInputLine == null || userInputLine.equalsIgnoreCase("EXIT")) {
out.println("EXIT");
break;
}
out.println(userInputLine);
String response = in.readLine();
System.out.println("Server response: " + response);
}
} catch (IOException e) {
System.err.println("Error connecting to server: " + e.getMessage());
}
}
}
Echo Server:
import java.io.*;
import java.net.*;
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.equalsIgnoreCase("EXIT")) {
System.out.println("Client requested to exit.");
break;
}
String response = processCommand(inputLine);
out.println(response);
}
} catch (IOException e) {
System.err.println("Error handling client: " + e.getMessage());
}
}
} catch (IOException e) {
System.err.println("Error starting server: " + e.getMessage());
}
}
while (true) {
System.out.print("Enter command (PING, TALK, ECHO, EXIT): ");
userInputLine = userInput.readLine();
if (userInputLine == null || userInputLine.equalsIgnoreCase("EXIT")) {
out.println("EXIT");
break;
}
out.println(userInputLine);
String response = in.readLine();
System.out.println("Server response: " + response);
}
} catch (IOException e) {
System.err.println("Error connecting to server: " + e.getMessage());
}
}
}
PING:
Echo Server
import java.io.*;
import java.net.*;
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.equalsIgnoreCase("EXIT")) {
System.out.println("Client requested to exit.");
break;
}
String response = processCommand(inputLine);
out.println(response);
}
} catch (IOException e) {
System.err.println("Error handling client: " + e.getMessage());
}
}
} catch (IOException e) {
System.err.println("Error starting server: " + e.getMessage());
}
}
private static String processCommand(String command) {
switch (command.toUpperCase()) {
case "PING":
return "PONG";
case "TALK":
return "Hello! How can I help you?";
case "ECHO":
return "ECHO Command Received";
default:
return "Unknown Command";
}
}
}
Client:
import java.io.*;
import java.net.*;
while (true) {
System.out.print("Enter command (PING, TALK, ECHO, EXIT): ");
userInputLine = userInput.readLine();
if (userInputLine == null || userInputLine.equalsIgnoreCase("EXIT")) {
out.println("EXIT");
break;
}
out.println(userInputLine);
String response = in.readLine();
System.out.println("Server response: " + response);
}
} catch (IOException e) {
System.err.println("Error connecting to server: " + e.getMessage());
}
}
}
int main() {
vector<vector<int>> costMatrix = {
{0, 1, INF, 3},
{1, 0, 1, 7},
{INF, 1, 0, 2},
{3, 7, 2, 0}
};
vector<vector<int>> distanceTable(NUM_ROUTERS,
vector<int>(NUM_ROUTERS, INF));
initializeTable(distanceTable, costMatrix);
cout << "Initial Distance Vector Table:\n";
printTable(distanceTable);
updateTable(distanceTable, costMatrix);
cout << "\nUpdated Distance Vector Table:\n";
printTable(distanceTable);
return 0;
}
O/P:
Initial Distance Vector Table:
Router 0 1 2 3
0 0 1 INF 3
1 1 0 1 7
2 INF 1 0 2
3 3 7 2 0
int main() {
vector<vector<int>> graph = {
{0, 1, INF, 3},
{1, 0, 1, 7},
{INF, 1, 0, 2},
{3, 7, 2, 0}
};
int src = 0;
dijkstra(graph, src);
return 0;
}
O/P:
Vertex Distance from Source
0 0
1 1
2 2
3 3
EXPERIMENT: 01
AIM: Study of different types of network cables and to practically implement the cross-wired cable and
straight through cable using clamping tool.
H/W & S/W REQUIRED: RJ 45 Connector, Crimp Tool, Ethernet Cable (Twisted Pair Cable).
THEORY:
A twisted pair cable consists of two insulated conductors twisted together in the shape of a spiral. It can be
shielded or unshielded. The unshielded twisted pair cables are very cheap and easy to install. But they are
very badly affected by the electromagnetic noise interference. Twisting of wires will reduce the effect of
noise or external interference. The induced emf into the two wires due to interference tends to cancel each
other due to twisting. Number of twists per unit length will determine the quality of cable. More twists
mean better quality. There are 3 types of UTP cables:
1. Straight-through cable
2. Crossover cable
3. Roll-over cable
Straight-through cable
Straight-Through refers to cables that have the pin assignments on each end of the cable. In other words,
Pin 1 connector A goes to Pin 1 on connector B, Pin 2 to Pin 2 etc. Straight-Through wired cables are most
commonly used to connect a host to client. When we talk about cat5e patch cables, the Straight-through
wired cat5e patch cable is used to connect computers, printers and other network client devices to the router,
switch or hub (the host device in this instance).
Crossover cable
Crossover wired cables (commonly called crossover cables) are very much like Straight-through cables
with the exception that TX and RX lines are crossed (they are at opposite positions on either end of the
cable. Using the 568-B standard as an example, you will see that Pin 1 on connector A goes to Pin 3 on
connector B. Pin 2 on connector A goes to Pin 6 on connector B etc. Crossover cables are most commonly
used to connect two hosts directly. Examples would be connecting a computer directly to another computer,
connecting a switch directly to another switch, or connecting a router to a router. Note: While in the past
when connecting two host devices directly a crossover cable was required. Now days most devices have
auto sensing technology that detects the cable and device and crosses pairs when needed.
Roll-over cable
Rollover wired cables most commonly called rollover cables, have opposite pin assignments on each end
of the cable or in other words it is "rolled over". Pin 1 of connector A would be connected to Pin 8 of
connector B. Pin 2 of connector A would be connected to Pin 7 of connector B and so on. Rollover cables,
sometimes referred to as Yost cables are most commonly used to connect to a devices console port to make
programming changes to the device. Unlike crossover and straight-wired cables, rollover cables are not
intended to carry data but instead create an interface with the device.
Crimp Tool
Following is the crimp tool for RJ 45 cable:
ACTIVITY:
STEPS
1. Start by stripping off about 2 inches of the plastic jacket off the end of the cable. Be very careful at
this point, as to not nick or cut into the wires, which are inside. Doing so could alter the
characteristics of your cable, or even worse render is useless. Check the wires, one more time for
nicks or cuts. If there are any, just whack the whole end off, and start over.
2. Spread the wires apart, but be sure to hold onto the base of the jacket with your other hand. You do
not want the wires to become untwisted down inside the jacket. Category 5 cable must only have
1/2 of an inch of 'untwisted' wire at the end; otherwise it will be 'out of spec'. At this point, you
obviously have ALOT more than 1/2 of an inch of un-twisted wire.
3. You have 2 end jacks, which must be installed on your cable. If you are using a pre-made cable,
with one of the ends whacked off, you only have one end to install - the crossed over end. Below
are two diagrams, which show how you need to arrange the cables for each type of cable end.
RESULT:
Straight-through cable and crossover cable have been successfully implemented.
SUPPLEMENT MATERIAL:
• Ethernet Cables, UTP vs STP, Straight vs Crossover, CAT 5,5e,6,7,8 Network Cables
• How to Make an Ethernet Cable! - FD500R
EXPERIMENT: 02
AIM: To connect two or more PCs or Laptops in a Local Area Network (LAN).
S/W REQUIRED: CISCO Packet Simulator
ACTIVITY 1:
Connecting two computers (point-to-point connection) using an Ethernet crossover cable.
STEPS
1. Open Cisco Packet Tracer (CPT).
2. Click on “End Devices” and select “PC”. Repeat this to create another “PC”.
3. Click on “Connections” and select “Copper Cross-Over” and connect the two PCs. The network
design will be as shown below:
4. Click on the first PC to see the PC controls. Click on “Desktop” tab. Click on “IP Configuration”.
Give the IP address value as “192.168.1.10”. Close the window.
5. Repeat the above step for the second PC and assign the IP address as “192.168.1.20”.
6. Enter “Simulation” mode.
7. Click on “Add Simple PDU” and click on one PC as the source and click on another PC to make it
the destination.
8. Run the simulation.
RESULT:
Two PCs were connected using a crossover cable and communication between the two PCs was observed.
ACTIVITY 2:
Creating a star topology using 4 PCs and a hub.
STEPS
1. Open CPT.
2. Click on “End Devices” and select “PC”. Repeat this to create another 3 PCs.
3. Click on “Network Devices” and select “Hubs” sub category. Now select “PT-Hub” and place it
on the workspace.
4. Name the hub as “Hub” and the 4 PCs as “A”, “B”, “C”, and “D”.
5. Connect the 4 PCs with the hub using a Copper Straight-Through cable. The network design will
be as shown below:
6. Now assign IP address to the PCs A, B, C, and D as 192.168.2.2, 192.168.2.3, 192.168.2.4, and
192.168.2.5 respectively.
7. Click on “Add Simple PDU” and click on PC “A” as the source and click on another PC “D” to
make it the destination.
8. Run the simulation and observe the behavior of the hub.
RESULT:
4 PCs were connected to a hub using a straight-through cable forming a star topology and communication
between the two PCs was observed. Also, behavior of the hub was observed.
ACTIVITY 3:
Creating a star topology using 4 PCs and a switch.
STEPS
Repeat the steps of the above activity. But, instead of a hub use a switch. While selecting a switch, you can
select 2960 switch which a well-known and used switch. The network design will look as shown below:
RESULT:
4 PCs were connected to a switch using a straight-through cable forming a star topology and communication
between the two PCs was observed. Also, behavior of the switch was observed.
EXPERIMENT: 03
AIM: To study basic network commands and network configuration commands.
S/W REQUIRED: Command Prompt/Terminal, Cisco Packet Tracer
ACTIVITY 1:
STEPS
1. Make sure that your system is connected to Internet.
2. Open command prompt and type “ping google.com”. The output would be as shown below:
3. Open command prompt and type “tracert google.com”. The output would be as shown below:
4. Open command prompt and type “nslookup google.com”. The output would be as shown below:
5. Open command prompt and type “pathping google.com”. The output would be as shown below:
ACTIVITY 2:
STEPS
1. Open Cisco Packet Tracer.
2. Click on “Network Devices” and click on “Routers” and then click on the router “2901” to create
a router in the logical workspace.
3. Click on the router again to open the options panel.
4. Click on “CLI” (Command Line Interface) tab.
5. Type the different commands given above and observe the output.
RESULT:
Networking commands and network configuration commands were successfully practiced.
EXPERIMENT: 04
AIM: To configure a network topology using packet tracer.
S/W REQUIRED: Cisco Packet Tracer
ACTIVITY:
STEPS
1. Open Cisco Packet Tracer.
2. Create a LAN containing a switch (2960) and 3 PCs. Name the PCs as A, B, and C.
3. Assign IP addresses to PCs created in the above step as 10.0.1.2, 10.0.1.3, and 10.0.1.4.
4. Connect all the 3 PCs to the switch.
5. Create another LAN containing a switch and 3 PCs. Name the PCs as D, E, and F.
6. Assign IP addresses to PCs created in the above step as 192.168.1.2, 192.168.1.3, and 192.168.1.4.
7. Connect all the 3 PCs to the switch.
8. Create a router (2901).
9. Connect both the switches to the router. Take care that while connecting the switches to the router,
GigabitEthernet ports are selected. The network design looks as shown below:
10. Now, we have to configure the router interfaces. Click on router to open the options. Click on
“Config”. Click on GigabitEthernet0/0 interface on the left-hand side. Give the IP address as
10.0.1.1 and change (click) the “Port Status” as “On”.
11. Repeat the above step for GigabitEthernet0/1 with IP address as 192.168.1.1.
12. For PCs A, B, and C change the “Default Gateway” in “IP Configuration” to 10.0.1.1.
13. For PCs D, E, and F change the “Default Gateway” in “IP Configuration” to 192.168.1.1. Final
network design will be as shown below:
14. Go to simulation mode and add a PDU from the source as PC A and the destination as PC F. Click
on play and watch the data flow.
RESULT:
Two LANs were connected using a router and the topology has been tested.