[go: up one dir, main page]

0% found this document useful (0 votes)
26 views50 pages

Computer Networks Lab Manual

The document is a lab manual for computer networks, authored by Ishika Agarwal, covering various networking commands, error detection algorithms, and IP address conversions. It includes practical examples and code implementations for concepts like OSI model, VRC, LRC, checksum, CRC, and Hamming code. The manual serves as a comprehensive guide for understanding and applying networking principles in both Windows and Linux environments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views50 pages

Computer Networks Lab Manual

The document is a lab manual for computer networks, authored by Ishika Agarwal, covering various networking commands, error detection algorithms, and IP address conversions. It includes practical examples and code implementations for concepts like OSI model, VRC, LRC, checksum, CRC, and Hamming code. The manual serves as a comprehensive guide for understanding and applying networking principles in both Windows and Linux environments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

COMPUTER NETWORKS

LAB MANUAL
ISHIKA AGARWAL
22BCE3179

INDEX
1. Windows Networking Commands
2. Windows and Linux Commands
3. Algorithm of OSI Model
4. Vertical Redundancy Check (VRC)
5. Longitudinal Redundancy Check (LRC)
6. Checksum
7. Cyclic Redundancy Check (CRC)
8. IP Address Conversion- Decimal and Binary
9. Class Configuration
10. Hamming Code
11. Internet Checksum
12. Go-Back-N ARQ
13. Selective Repeat ARQ
14. Dijkstra’s Algorithm
15. Cisco Packet Tracer – Switches and End devices
16. Cisco Packet Tracer – Topology
17. Cisco Packet Tracer – Routers, Switches and End devices

1
Windows Networking Commands
ping command - When you ping a device you send that device a short message,
which it then sends back (the echo).

hostname command - displays the host name of your machine.

getmac command - shows the MAC address of your network interfaces.

2
ipconfig command - Finding Your IP Address and Default Gateway

3
arp command - used for showing the address resolution cache.

nslookup command - troubleshooting DNS related problems.

4
tracert command - displays a list of all the routers that a packet has to go
through to get from the computer where tracert is run to any other computer
on the internet.

pathping command - traces the route to the destination address, launches a 25


second test of each router, gathering statistics on the rate of data loss.

route command - displays the computers routing table.

5
systeminfo command - access the system’s hardware and software details

6
netstat -a command - displays a variety of statistics about a computers active
TCP/IP connections.

7
8
Windows and Linux Commands

Windows Linux Usage


Command Command
ipconfig ifconfig / ip Display and configure network interfaces.
ping ping Test the reachability of a host on an IP network.
tracert traceroute Print the route packets take to a network host.
nslookup nslookup Query Internet name servers interactively.
netstat netstat Display network connections, routing tables,
interface statistics, masquerade connections, and
multicast memberships.
arp -a arp Display and modify the IP-to-Physical address
translation tables used by ARP.
route print route Display and modify the IP routing table.
hostname hostname Display the system’s hostname.
netsh nmcli / ifup / Configure network interfaces and settings.
ifdown
getmac ip link Display the MAC address of network interfaces.
ipconfig systemd- Flush the DNS resolver cache.
/flushdns resolve --
flush-caches
net use mount Map network drives or mount file systems.
net user useradd / us Manage user accounts.
ermod / user
del
net groupadd / g Manage local groups.
localgroup roupmod / g
roupdel
tasklist ps Display a list of currently running processes.

9
taskkill kill Terminate a process by PID.
systeminfo uname - Display detailed system information.
a / lsb_relea
se -a
shutdown shutdown Shutdown or restart the system.

10
Algorithm of OSI Model

import java.util.*;
public class OSIModelSimulation {
public static void main(String[] args) {
System.out.print("Ishika Agarwal 22BCE3179\n ");
Scanner scanner = new Scanner(System.in);
System.out.print("TRANSMITTER\nEnter the string: ");
String message = scanner.nextLine();
String[] layers = {"AH", "PH", "SH", "TH", "NH", "DH"};
for (int i = 0; i < layers.length; i++) {
message = layers[i] + " " + message;
System.out.println(layers[i] + " LAYER: " + message);
}
System.out.println("MESSAGE ENTERED INTO PHYSICAL LAYER AND
TRANSMITTED.");
System.out.println("\nRECEIVER:\nMESSAGE ENTERED INTO PHYSICAL
LAYER");
for (int i = layers.length - 1; i >= 0; i--) {
System.out.println(layers[i] + " LAYER: " + message);
message = message.substring(layers[i].length() + 1); // Remove layer
header
}
System.out.println("Final message: " + message);
}
}

11
12
Vertical Redundancy Check

import java.util.*;
public class VRC {
// Function to calculate even parity for a binary string
public static char calculateParity(String data) {
int countOnes = 0;
for (int i = 0; i < data.length(); i++) {
if (data.charAt(i) == '1') {
countOnes++;
}
}
// If the count of 1's is even, return '0', else return '1' (for even parity)
return (countOnes % 2 == 0) ? '0' : '1';
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Ishika Agarwal 22BCE3179 ");
System.out.print("Enter transmitted binary data (without parity bit): ");
String transmittedData = scanner.nextLine();
System.out.print("Enter received binary data (without parity bit): ");
String receivedData = scanner.nextLine();
char transmittedParity = calculateParity(transmittedData);
System.out.println("Calculated parity bit for transmitted data: " +
transmittedParity);
char receivedParity = calculateParity(receivedData);

13
System.out.println("Calculated parity bit for received data: " +
receivedParity);
if (transmittedParity == receivedParity) {
System.out.println("No error detected in the received data.");
}
else {
System.out.println("Error detected in the received data.");
}
}
}

14
Longitudinal Redundancy Check

import java.util.*;
public class LongitudinalRedundancyCheck {
// Function to calculate LRC for the given data block
public static String calculateLRC(String[] dataBlock) {
int blockLength = dataBlock[0].length();
char[] lrc = new char[blockLength];
for (int i = 0; i < blockLength; i++) {
lrc[i] = '0';
}
for (String data : dataBlock) {
for (int i = 0; i < blockLength; i++) {
lrc[i] = (lrc[i] == data.charAt(i)) ? '0' : '1';
}
}
return new String(lrc);
}
public static void main(String[] args) {
System.out.println("Ishika AGarwal 22BCE3179");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of data blocks: ");
int n = scanner.nextInt();
scanner.nextLine();
String[] dataBlock = new String[n];
System.out.println("Enter the data blocks (equal length): ");

15
for (int i = 0; i < n; i++) {
dataBlock[i] = scanner.nextLine();
}
String lrc = calculateLRC(dataBlock);
System.out.println("Calculated LRC: " + lrc);
System.out.println("Enter received data blocks (including LRC at the end):
");
String[] receivedBlock = new String[n + 1];
for (int i = 0; i <= n; i++) {
receivedBlock[i] = scanner.nextLine();
}
String receivedLRC = calculateLRC(receivedBlock);
// Check for errors
if (receivedLRC.equals("00000000")) {
System.out.println("No error detected in the received data.");
} else {
System.out.println("Error detected in the received data.");
}
}
}

16
17
Checksum

import java.util.*;
class checkSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Ishika Agarwal 22BCE3179");
System.out.println("Enter the number of binary words: ");
int numWords = sc.nextInt();
sc.nextLine();
String[] words = new String[numWords];
System.out.println("Enter the binary words separated by space: ");
String input = sc.nextLine();
words = input.split(" ");
if (words.length != numWords) {
System.out.println("Error: Number of words does not match the
input.");
return;
}
int sum = 0;
for (String word : words) {
int decimalValue = Integer.parseInt(word, 2);
sum += decimalValue;
}
String binarySum = Integer.toBinaryString(sum);
// Calculate checksum
String checksum = "";
18
for (char ch : binarySum.toCharArray()) {
checksum += (ch == '0') ? '1' : '0';
}
// Add checksum to the sum
int checksumDecimal = Integer.parseInt(checksum, 2);
int total = sum + checksumDecimal;
String binaryTotal = Integer.toBinaryString(total);
String complement = "";
for (char ch : binaryTotal.toCharArray()) {
complement += (ch == '0') ? '1' : '0';
}
int finalValue = Integer.parseInt(complement, 2);
System.out.println("\nBinary Words:");
for (String word : words) {
System.out.println(word);
}
System.out.println("\nSum: " + binarySum);
System.out.println("Checksum: " + checksum);
System.out.println("Value received after transmission: " + complement);
if (finalValue == 0) {
System.out.println("Message Received Successfully\n");
} else {
System.out.println("Message Received with Errors\n");
}
}
}

19
20
Cyclic Redundancy Check

import java.util.*;
class CRC {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Ishika Agarwal 22BCE3179");
System.out.println("Enter divisor (binary) : ");
String divisor = scanner.nextLine();
System.out.println("Enter Data to be sent (binary): ");
String data = scanner.nextLine();
String appendedData = data;
while (appendedData.length() < (data.length() + divisor.length() - 1)) {
appendedData += "0";
}
String transmittedCode = data + divide(appendedData, divisor);
System.out.println("The transmitted Code Word (data + checksum) is: " +
transmittedCode);
System.out.println("Please enter the received Code Word (with checksum):
");
String receivedData = scanner.nextLine();
String remainder = divide(receivedData, divisor);
if (Integer.parseInt(remainder) == 0)
System.out.println("The received code word contains no errors.");
else
System.out.println("The received code word contains errors.");
}

21
// Division function for CRC (XOR division)
static String divide(String dividend, String divisor) {
int pointer = divisor.length();
String result = dividend.substring(0, pointer);
String remainder = "";
while (pointer <= dividend.length()) {
remainder = "";
for (int i = 0; i < divisor.length(); i++) {
if (result.charAt(i) == divisor.charAt(i))
remainder += "0";
else
remainder += "1";
}
if (pointer == dividend.length())
break;
result = remainder.substring(1) + dividend.charAt(pointer);
pointer++;
}
return remainder.substring(1);
}
}

22
23
IP address – Decimal and Binary Conversion

import java.util.*;
public class IPConversion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Ishika Agarwal 22BCE3179");
System.out.println("Enter '1' to convert from binary to decimal.");
System.out.println("Enter '2' to convert from decimal to binary.");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
// Convert from binary to decimal
System.out.print("Enter the IP address in binary format: ");
String binaryIP = scanner.nextLine();
String decimalIP = binaryToDecimal(binaryIP);
System.out.println("Decimal format: " + decimalIP);
break;
case 2:
// Convert from decimal to binary
System.out.print("Enter the IP address in decimal format: ");
String decimalIPString = scanner.nextLine();
String binaryIPString = decimalToBinary(decimalIPString);
System.out.println("Binary format: " + binaryIPString);
break;

24
default:
System.out.println("Invalid choice.");
break;
}
}
// Convert binary IP to decimal IP
private static String binaryToDecimal(String binaryIP) {
String[] binaryParts = binaryIP.split("\\.");
StringBuilder result = new StringBuilder();
for (int i = 0; i < binaryParts.length; i++) {
String byteString = binaryParts[i];
int decimalValue = Integer.parseInt(byteString, 2);
if (i > 0) {
result.append(".");
}
result.append(decimalValue);
}
return result.toString();
}
// Convert decimal IP to binary IP
private static String decimalToBinary(String decimalIP) {
String[] decimalParts = decimalIP.split("\\.");
String result = "";
for (int i = 0; i < decimalParts.length; i++) {
int decimalValue = Integer.parseInt(decimalParts[i]);
String binaryString = String.format("%8s",
Integer.toBinaryString(decimalValue)).replace(' ', '0');
25
if (i > 0) {
result += ".";
}
result += binaryString;
}
return result;
}
}

26
Class Configuration

import java.util.*;
public class ClassConfig {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Ishika Agarwal 22BCE3179");
System.out.println("Enter '1' to input IP address in decimal
format.");
System.out.println("Enter '2' to input IP address in binary
format.");
int choice = scanner.nextInt();
scanner.nextLine();
String ipAddress;
switch (choice) {
case 1:
System.out.print("Enter the IP address in decimal format :
");
ipAddress = scanner.nextLine();
break;
case 2:
System.out.print("Enter the IP address in binary format : ");
String binaryIP = scanner.nextLine();
ipAddress = binaryToDecimal(binaryIP);
break;
default:
System.out.println("Invalid choice.");

27
scanner.close();
return;
}
String ipClass = determineIPClass(ipAddress);
System.out.println("IP Address Class: " + ipClass);
}
private static String binaryToDecimal(String binaryIP) {
String[] binaryParts = binaryIP.split("\\.");
String result = "";
for (int i = 0; i < binaryParts.length; i++) {
String byteString = binaryParts[i];
int decimalValue = Integer.parseInt(byteString, 2);
if (i > 0) {
result += ".";
}
result += decimalValue;
}
return result;
}
private static String determineIPClass(String ipAddress) {
String[] octets = ipAddress.split("\\.");
if (octets.length != 4) {
return "Invalid IP Address";
}
int firstOctet = Integer.parseInt(octets[0]);
if (firstOctet >=0 && firstOctet <= 127)

28
return "Class A";
else if (firstOctet >= 128 && firstOctet <= 191)
return "Class B";
else if (firstOctet >= 192 && firstOctet <= 223)
return "Class C";
else if (firstOctet >= 224 && firstOctet <= 239)
return "Class D";
else if (firstOctet >= 240 && firstOctet <= 255)
return "Class E";
else
return "Invalid IP Address";
}
}

29
Hamming Code
import java.util.*;
class HammingCode {
static void print(int ar[]) {
for (int i = 1; i < ar.length; i++) {
System.out.print(ar[i]);
}
System.out.println();
}
static int[] calculation(int[] ar, int r) {
for (int i = 0; i < r; i++) {
int x = (int) Math.pow(2, i);
for (int j = 1; j < ar.length; j++) {
if (((j >> i) & 1) == 1) {
if (x != j)
ar[x] = ar[x] ^ ar[j];
}
}
System.out.println("r" + x + " = " + ar[x]);
}
return ar;
}
static int[] generateCode(String str, int M, int r) {
int[] ar = new int[r + M + 1];
int j = 0;
for (int i = 1; i < ar.length; i++) {

30
if ((Math.ceil(Math.log(i) / Math.log(2)) - Math.floor(Math.log(i) /
Math.log(2))) == 0) {
ar[i] = 0; // redundant bits
} else {
ar[i] = (int) (str.charAt(j) - '0');
j++;
}
}
return ar;
}
static int detectError(int[] ar, int r) {
int errorPos = 0;
for (int i = 0; i < r; i++) {
int x = (int) Math.pow(2, i);
int parity = 0;
for (int j = 1; j < ar.length; j++) {
if (((j >> i) & 1) == 1) {
parity = parity ^ ar[j];
}
}
if (parity != 0) {
errorPos += x;
}
}
return errorPos;
}
public static void main(String[] args) {
31
System.out.println("Ishika Agarwal 22BCE3179 ");
Scanner sc = new Scanner(System.in);
System.out.print("Enter the data word: ");
String str = sc.nextLine();
int M = str.length();
int r = 1;
while (Math.pow(2, r) < (M + r + 1)) {
r++;
}
int[] ar = generateCode(str, M, r);
System.out.println("Generated Hamming Code:");
ar = calculation(ar, r);
print(ar);
System.out.print("Enter the received Hamming code: ");
String receivedStr = sc.nextLine();
int[] receivedAr = new int[r + M + 1];
for (int i = 1; i < receivedAr.length; i++) {
receivedAr[i] = (int) (receivedStr.charAt(i - 1) - '0');
}
int errorPos = detectError(receivedAr, r);
if (errorPos == 0) {
System.out.println("No error detected.");
}
else {
System.out.println("Error detected at position: " + errorPos);
System.out.println("Corrected Hamming Code:");

32
receivedAr[errorPos] = receivedAr[errorPos] ^ 1; // Correct the error
print(receivedAr);
}
}
}

33
Internet CheckSum

import java.util.*;
public class ChecksumMethod {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Ishika Agarwal 22BCE3179");
System.out.println("Enter the string input:");
String input = scan.next();
int checksum = generateChecksum(input);
System.out.println("The checksum generated: " + Integer.toHexString
(checksum));
System.out.println("Enter the data to be sent:");
input = scan.next();
System.out.println("Enter the checksum to be sent:");
checksum = Integer.parseInt(scan.next(), 16);
receive(input, checksum);
}
static int generateChecksum(String s) {
int checksum = 0;
for (int i = 0; i < s.length() - 2; i += 2) {
int x = (int) (s.charAt(i));
String hexValue = Integer.toHexString(x);
x = (int) (s.charAt(i + 1));
hexValue += Integer.toHexString(x);
System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : " + hexValue);
x = Integer.parseInt(hexValue, 16);
34
checksum += x;
}
int x;
if (s.length() % 2 == 0) {
x = (int) (s.charAt(s.length() - 2));
String hexValue = Integer.toHexString(x);
x = (int) (s.charAt(s.length() - 1));
hexValue += Integer.toHexString(x);
System.out.println(s.charAt(s.length() - 2) + "" + s.charAt(s.length() - 1) +
" : " + hexValue);
x = Integer.parseInt(hexValue, 16);
}
else {
x = (int) (s.charAt(s.length() - 1));
String hexValue = "00" + Integer.toHexString(x);
System.out.println(s.charAt(s.length() - 1) + " : " + hexValue);
x = Integer.parseInt(hexValue, 16);
}
checksum += x;
String hexValue = Integer.toHexString(checksum);
if (hexValue.length() > 4) {
int carry = Integer.parseInt(("" + hexValue.charAt(0)), 16);
hexValue = hexValue.substring(1);
checksum = Integer.parseInt(hexValue, 16) + carry;
}
checksum = compl(checksum);
return checksum;
35
}
static void receive(String s, int checksum) {
int generatedChecksum = generateChecksum(s);
generatedChecksum = compl(generatedChecksum);
int FinalSum = compl(generatedChecksum + checksum);
System.out.println("FinalSum = " + Integer.toHexString(FinalSum));
if (FinalSum == 0) {
System.out.println("Data is received without error.");
} else {
System.out.println("There is an error in the received data.");
}
}
static int compl(int checksum) {
return Integer.parseInt("FFFF", 16) - checksum;
}
}

36
37
Go-Back-N ARQ

import java.util.*;
class GoBackN {
private int windowSize;
private int total;
private int[] packets;
private int next;
private int ack;

public GoBackN(int windowSize, int total) {


this.windowSize = windowSize;
this.total = total;
this.packets = new int[total];
for (int i = 0; i < total; i++) {
packets[i] = i;
}
this.next = 0;
this.ack = 0;
}

public void sendPackets() {


while (ack < total) {
System.out.println("Sending packets: ");
int packetsSent = 0;
for (int i = 0; i < windowSize && next < total; i++) {

38
System.out.print(packets[next] + " ");
next++;
packetsSent++;
}
System.out.println();
if (packetsSent == 0) {
break;
}
receiveACKs(packetsSent);
}
}

private void receiveACKs(int packetsSent) {


int lastAck = ack + packetsSent;
if (lastAck > total) {
lastAck = total;
}
System.out.println();
System.out.println("Receiving ACKs for packets up to: " + lastAck);
ack = lastAck;
}

public static void GoBackN(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Ishika Agarwal 22BCE3179 ");
System.out.print("Enter the window size: ");

39
int windowSize = sc.nextInt();
System.out.print("Enter the total number of packets: ");
int total = sc.nextInt();
GoBackN gbn = new GoBackN(windowSize, total);
gbn.sendPackets();
sc.close();
}
}

40
Selective Repeat ARQ
import java.util.*;
class SelectiveRepeatARQ {
private int windowSize;
private int total;
private int[] packets;
private boolean[] rec;

public SelectiveRepeatARQ(int windowSize, int total) {


this.windowSize = windowSize;
this.total = total;
this.packets = new int[total];
this.rec = new boolean[total];
for (int i = 0; i < total; i++) {
packets[i] = i; // Initialize packets with sequence numbers
rec[i] = false; // Initialize received status
}
}
public void sendPackets() {
int base = 0;
while (base < total) {
System.out.println();
System.out.println("Sending packets: ");
for (int i = base; i < base + windowSize && i < total; i++) {
System.out.print(packets[i] + " ");
}

41
System.out.println();
receiveACKs(base);
base += windowSize;
}
}

private void receiveACKs(int base) {


for (int i = 0; i < windowSize && (base + i) < total; i++) {
int ack = base + i;
System.out.println(); System.out.println("Receiving ACK for packet: " +
ack);
rec[ack] = true;
}
System.out.println();
System.out.println("Currently received packets: ");
for (int i = 0; i < total; i++) {
if (rec[i]) {
System.out.print(i + " ");
}
}
System.out.println();
}

public static void SelectiveRepeatARQ(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Ishika Agarwal 22BCE3179 ");
System.out.print("Enter the window size: ");
42
int windowSize = sc.nextInt();
System.out.print("Enter the total number of packets: ");
int total = sc.nextInt();
SelectiveRepeatARQ srARQ = new SelectiveRepeatARQ(windowSize, total);
srARQ.sendPackets();
}
}

43
Dijkstra’s Algorithm
import java.util.Scanner;
public class Dijkstra {
private static final int MAX_VERTICES = 100;
private static final int INF = Integer.MAX_VALUE;

public static int minDistance(int[] dist, boolean[] sptSet, int V) {


int min = INF, min_index = -1;

for (int v = 0; v < V; v++) {


if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}
return min_index;
}
public static void dijkstra(int[][] graph, int src, int V) {
int[] dist = new int[V];
boolean[] sptSet = new boolean[V];

for (int i = 0; i < V; i++) {


dist[i] = INF;
sptSet[i] = false;
}

44
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet, V);
sptSet[u] = true;

for (int v = 0; v < V; v++) {


if (!sptSet[v] && graph[u][v] != 0 && dist[u] != INF && dist[u] +
graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
System.out.println("Vertex\tDistance from Source Node " + src);
for (int i = 0; i < V; i++) {
System.out.println(i + "\t\t" + dist[i]);
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int[][] graph = new int[MAX_VERTICES][MAX_VERTICES];
System.out.println("Ishika Agarwal 22BCE3179");
System.out.print("Enter the number of nodes: ");
int V = sc.nextInt();
System.out.print("Enter the number of edges: ");
int E = sc.nextInt();

45
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
graph[i][j] = 0;
}
}

System.out.println("Enter the edges (source node (u), destination node (v),


weight (w)):");
for (int i = 0; i < E; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
graph[u][v] = w;
graph[v][u] = w;
}

System.out.print("Enter the source node: ");


int src = sc.nextInt();
dijkstra(graph, src, V);
sc.close();
}
}

46
Cisco Packet Tracer
Switches and End devices

47
Topology

48
49
Routers, Switches and End devices

50

You might also like