0% found this document useful (0 votes)
6 views28 pages

Record T-1to10

The document outlines various networking tasks including basic network commands like ping, tracert, and ipconfig, along with error detection using CRC-CCITT, bit stuffing, character stuffing, Dijkstra’s algorithm, distance vector algorithm, and congestion control using the leaky bucket algorithm. It provides Java programs for each task demonstrating their implementation. Additionally, it includes TCP socket programming for server-client communication.

Uploaded by

uwmabtw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views28 pages

Record T-1to10

The document outlines various networking tasks including basic network commands like ping, tracert, and ipconfig, along with error detection using CRC-CCITT, bit stuffing, character stuffing, Dijkstra’s algorithm, distance vector algorithm, and congestion control using the leaky bucket algorithm. It provides Java programs for each task demonstrating their implementation. Additionally, it includes TCP socket programming for server-client communication.

Uploaded by

uwmabtw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

22A81A0652 CN-LAB

Task-1

Study of basic network commands and network configuration


commands
1.ping 2.Tracert 3.IP config
4.Hostname 5.nslookup 6.netstat
1. ipconfig

Displays the current configuration of the IP stack on a networked computer.

Provides details such as IP address, subnet mask, and default gateway for all adapters.

ipconfig /all

Shows a detailed view of all network interfaces.

Includes additional information like DNS servers, MAC addresses, and DHCP settings.
22A81A0652 CN-LAB

2. ping

Tests connectivity to a specific IP address or domain name.

Measures round-trip time and packet loss, helping diagnose network issues.

3. tracert

Traces the route packets take to reach a destination. Identifies

each hop and measures the delay between them.

4. hostname

 Displays the name of the computer or device on the network.

 Useful for identifying the device in a network environment.

5. nslookup

 Resolves domain names to IP addresses and vice versa.

 Useful for verifying DNS configurations and troubleshooting name resolution problems.
22A81A0652 CN-LAB

6. netstat

 Displays active network connections and listening ports.

 Provides details on protocol use, local and foreign addresses, and connection states.
22A81A0652 CN-LAB

Task-2

Construct detecting error using CRC-CCITT


Detecting Errors Using CRC-CCITT The Cyclic Redundancy Check (CRC) with the CCITT
standard was used to detect transmission errors in data packets. The steps involved include:

 Generating a Checksum:

o The data to be transmitted is divided by a predetermined polynomial


(CRC- CCITT).

o The remainder becomes the checksum, appended to the data.

 Error Detection at the Receiver:

o The receiver performs the same division using the received data.

o If the remainder is zero, the transmission is error-free; otherwise, errors


are detected.

Program:
import java.io.*;
class CRC{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Generator:");
String gen = br.readLine();
System.out.println("Enter Data:");
String data = br.readLine();
String code = data;
while(code.length() < (data.length() + gen.length() - 1))
code = code + "0";
code = data + div(code,gen);
System.out.println("Generator Code: " + code);
System.out.println("enter received Code:");
String rec = br.readLine();
if(Integer.parseInt(div(rec,gen)) == 0)
System.out.println("no errors");
else
System.out.println("errors");
}
static String div(String num1,String num2)
{ int pointer = num2.length();
String result = num1.substring(0, pointer);
String remainder = "";
for(int i = 0; i < num2.length(); i++)
{
if(result.charAt(i) == num2.charAt(i))
remainder += "0";
22A81A0652 CN-LAB

else
remainder += "1";
}
while(pointer < num1.length())
{ if(remainder.charAt(0) == '0'){
remainder = remainder.substring(1, remainder.length());
remainder = remainder + String.valueOf(num1.charAt(pointer));
pointer++;
}
result = remainder;
remainder = "";
for(int i = 0; i < num2.length(); i++)
{ if(result.charAt(i) == num2.charAt(i))
remainder += "0";
else
remainder += "1";
}
}
return remainder.substring(1,remainder.length());
}
}
}

Output:
22A81A0652 CN-LAB

Task-3

Implementation of Bit Stuffing

Bit Stuffing:

Bit stuffing is a technique employed to maintain synchronization in data transmission. Key


points include:

Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class BitStuffing {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter input data:");
String inputData = br.readLine();
String stuffedData = stuffing(inputData);
System.out.println("Stuffed data: " + stuffedData);
System.out.println("Enter receiver data:");
String receivedData = br.readLine();
String destuffedData = destuffing(receivedData);
System.out.println("De-stuffed data: " + destuffedData);
}
public static String stuffing(String data)
{ StringBuilder res = new
StringBuilder(); int count = 0;
int n = data.length();
for (int i = 0; i < n; i++) {
char currentChar = data.charAt(i);
if (currentChar == '1') {
count++;
res.append('1');
if (count == 5)
{
res.append('0');
count = 0;
}
} else {
count = 0;
res.append('0');
}
}
return res.toString();
}
public static String destuffing(String data) {
StringBuilder res = new StringBuilder();
int count = 0;
int n = data.length();
22A81A0652 CN-LAB

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


22A81A0652 CN-LAB

char currentChar = data.charAt(i);


if (currentChar == '1') {
count++;
res.append('1');
} else {
if (count == 5)
{ count = 0;
continue;
}
res.append('0');
count = 0;
}
}
return res.toString();
}
}

Output:
22A81A0652 CN-LAB

Task-4

Implementation of Character Stuffing


Character Stuffing:

Character stuffing is a data transmission technique where special characters (like escape
sequences) are added to a message to differentiate control information from actual data. It
prevents misinterpretation of reserved characters by inserting an escape character before them.
The receiver removes the extra characters to retrieve the original message

Program:
import java.util.Scanner;
public class CharacterStuffing {
public static void main(String[] args)
{ Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the string: ");
String input = scanner.nextLine();
System.out.print("Enter the flag character: ");
char flag = scanner.next().charAt(0);
System.out.print("Enter the escape character: ");
char escape = scanner.next().charAt(0);
System.out.println("Original String: " + input);
String stuffedString = characterStuffing(input, flag, escape);
System.out.println("Stuffed String: " + stuffedString);
String destuffedString = characterDestuffing(stuffedString, flag, escape);
System.out.println("Destuffed String: " + destuffedString);
}
public static String characterStuffing(String input, char flag, char escape)
{ StringBuilder result = new StringBuilder();
result.append(flag);
for (int i = 0; i < input.length(); i++)
{ char currentChar = input.charAt(i);
if (currentChar == flag || currentChar == escape) {
result.append(escape);
}
result.append(currentChar);
}
result.append(flag);
return result.toString();
}
public static String characterDestuffing(String stuffedInput, char flag, char escape)
{ StringBuilder result = new StringBuilder();
boolean escapeFlag = false;
for (int i = 1; i < stuffedInput.length() - 1; i++) {
char currentChar = stuffedInput.charAt(i);
if (escapeFlag) {

result.append(currentChar);
escapeFlag = false;
22A81A0652 CN-LAB

} else if (currentChar == escape) {


22A81A0652 CN-LAB

escapeFlag = true;
} else {
result.append(currentChar);
}
}
return result.toString();
}
}

Output:
22A81A0652 CN-LAB

Task-6

Implementation of Dijkstra’s algorithm

Dijkstra’s algorithm:

Dijkstra's algorithm is a shortest path algorithm used to find the minimum distance from a source
node to all other nodes in a weighted graph. It uses a priority queue (or a min-heap) to explore the
nearest unvisited node first, updating distances dynamically.

Program:
import java.util.*;
class Dijkstra {
private int getMinDistanceNode(int[] dist, boolean[] visited, int nodes)
{ int min = Integer.MAX_VALUE;
int minNode = -1;
for (int i = 0; i < nodes; i++) {
if (!visited[i] && dist[i] < min)
{ min = dist[i];
minNode = i;
}
}
return minNode;
}
public void dijkstra(int[][] graph, int startNode, int nodes) {
int[] dist = new int[nodes];
boolean[] visited = new boolean[nodes];
Arrays.fill(dist,
Integer.MAX_VALUE); dist[startNode]
= 0;
for (int i = 0; i < nodes - 1; i++) {
int u = getMinDistanceNode(dist, visited, nodes);
visited[u] = true;
for (int v = 0; v < nodes; v++) {
if (!visited[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] +
graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
printSolution(dist, nodes);
}
private void printSolution(int dist[], int nodes)
{ System.out.println("Node \t Distance from Source");
for (int i = 0; i < nodes; i++) {
System.out.println(i + " \t " + dist[i]);
}
}
public static void main(String[] args)
{ Scanner sc = new Scanner(System.in);
22A81A0652 CN-LAB

System.out.println("Enter the number of nodes in the graph:");


int nodes = sc.nextInt();
int[][] graph = new int[nodes][nodes];
System.out.println("Enter the graph's adjacency matrix (use 0 for no path):");
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) { graph[i]
[j] = sc.nextInt();
} }
System.out.println("Enter the starting node (0 to " + (nodes - 1) + "):");
int startNode = sc.nextInt();
if (startNode < 0 || startNode >= nodes) {
System.out.println("Invalid start node! Please choose a node between 0 and " + (nodes - 1));
sc.close();
return;
}
Dijkstra dijkstra = new Dijkstra();
dijkstra.dijkstra(graph, startNode, nodes);
sc.close();
}
}
Output:
22A81A0652 CN-LAB

Task-7

Implementation of Distance vector algorithm

Distance vector algorithm:

The Distance Vector Algorithm is a routing algorithm used in computer networks to determine the
shortest path between nodes. Each router shares its distance table with its neighbors, updating routes
based on received information.

Program:
import java.util.Scanner;
public class DistanceVectorsimple
{ public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of nodes: ");
int nodes = sc.nextInt();
int[][] dist = new int[nodes][nodes];
System.out.println("Enter the distance matrix (Enter -1 for no direct path):");
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++)
{ dist[i][j] = sc.nextInt();
if (dist[i][j] == -1 && i != j) { dist[i]
[j] = Integer.MAX_VALUE;
}
}
}
findShortestPaths(dist, nodes);
sc.close();
}
public static void findShortestPaths(int[][] dist, int nodes)
{ for (int k = 0; k < nodes; k++) {
for (int i = 0; i < nodes; i++)
{ for (int j = 0; j < nodes; j++)
{
if (dist[i][k] != Integer.MAX_VALUE && dist[k][j] != Integer.MAX_VALUE)
{ if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
System.out.println("Shortest distance between each pair of nodes:");
for (int i = 0; i < nodes; i++) {
for (int j = 0; j < nodes; j++) {
if (dist[i][j] == Integer.MAX_VALUE)
{ System.out.print("INF ");
22A81A0652 CN-LAB

} else {
System.out.print(dist[i][j] + " ");
22A81A0652 CN-LAB

}
}
System.out.println();
}
}
}
Output:
22A81A0652 CN-LAB

Task-8

Implementation of Congestion Control Using

Leaky Bucket Algorithm

Leaky Bucket algorithm:

The leaky bucket is an algorithm based on an analogy of how a bucket with a constant leak will
overflow if either the average rate at which water is poured in exceeds the rate at which the bucket
leaks or if more water than the capacity of the bucket is poured in all at once.

Program:

import java.io.*;
import java.util.*;
class Leakybucket {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of queries: ");
int no_of_queries = scanner.nextInt();
System.out.print("Enter the bucket size: ");
int bucket_size = scanner.nextInt();
System.out.print("Enter the input packet size: ");
int input_pkt_size = scanner.nextInt();
System.out.print("Enter the output packet size: ");
int output_pkt_size = scanner.nextInt();
int storage = 0;
int size_left;
for (int i = 0; i < no_of_queries; i++)
{ size_left = bucket_size - storage;
if (input_pkt_size <= size_left)
{ storage += input_pkt_size; }
else {
System.out.println("Packet loss = " + input_pkt_size); }
System.out.println("Buffer size= " + storage + " out of bucket size= " + bucket_size);
storage -= output_pkt_size;
}
scanner.close();
}
}
22A81A0652 CN-LAB

Output:
22A81A0652 CN-LAB

Task-9

Implementation Using Socket TCP both server

And Client Programs

TCP socket programming enables communication between a server and a client using the
Transmission Control Protocol. The server creates a socket, binds it to an address and port,
listens for incoming connections, and accepts clients. The client creates a socket, connects to the
server, and exchanges data using send/receive operations.

Programs:

TCP Server :
import java.io.*;
import java.net.*;
public class TCPServer {
public static void main(String[] args)
{ int port = 6500;
try (ServerSocket serverSocket = new ServerSocket(port))
{ System.out.println("Server is waiting for a connection on port " + port);
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected!");
BufferedReader input = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);
String message;
while ((message = input.readLine()) != null)
{ System.out.println("Received from client: " + message);
output.println("Server received: " + message);
}
} catch (IOException e) {
System.err.println("Server error: " + e.getMessage());
}
}
}

TCP Client :
import java.io.*;
import java.net.*;
public class TCPClient {
public static void main(String[] args)
{ String serverAddress = "localhost";
int port = 6500;
try (Socket socket = new Socket(serverAddress, port))
{ BufferedReader input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
22A81A0652 CN-LAB

BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));


System.out.println("Connected to the server. Type your messages.");
String userInput;
while ((userInput = consoleInput.readLine()) != null) {
output.println(userInput);
System.out.println("Server response: " + input.readLine()); }
} catch (IOException e) {
System.err.println("Client error: " + e.getMessage());
}
}
}

OUTPUT:
22A81A0652 CN-LAB

Task-10

Implementation Using Socket UDP both server

And Client Programs


UDP socket programming enables connectionless communication between a server and a client
using the User Datagram Protocol. The server creates a socket, binds it to an address and port,
and waits to receive datagrams. The client creates a socket and sends datagrams to the server,
which responds accordingly.

Programs:

UDP Server :

import java.net.DatagramPacket;

import java.net.DatagramSocket;

public class UDPServer {

public static void main(String[] args) {

try {

DatagramSocket serverSocket = new DatagramSocket(9876);

byte[] receiveBuffer = new byte[1024];

System.out.println("UDP Server is running...");

while (true) {

DatagramPacket receivePacket = new DatagramPacket(receiveBuffer,


receiveBuffer.length);

serverSocket.receive(receivePacket);

String receivedMessage = new String(receivePacket.getData(), 0,


receivePacket.getLength());

System.out.println("Client: " + receivedMessage);

if (receivedMessage.equalsIgnoreCase("exit")) {

System.out.println("Client disconnected. Server shutting down...");

break;

String response = "Server received: " + receivedMessage;


22A81A0652 CN-LAB

byte[] sendBuffer = response.getBytes();

DatagramPacket sendPacket = new DatagramPacket(

sendBuffer, sendBuffer.length,

receivePacket.getAddress(),

receivePacket.getPort()

);

serverSocket.send(sendPacket);

serverSocket.close();

} catch (Exception e) {

e.printStackTrace();

UDP Client :

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.util.Scanner;

public class UDPClient {

public static void main(String[] args)

{ try {

DatagramSocket clientSocket = new DatagramSocket();

InetAddress serverAddress = InetAddress.getByName("localhost");

Scanner scanner = new Scanner(System.in);

System.out.println("UDP Client started. Type 'exit' to quit.");


22A81A0652 CN-LAB

while (true) {

System.out.print("Enter message: ");

String userInput = scanner.nextLine();

byte[] sendBuffer = userInput.getBytes();

DatagramPacket sendPacket = new

DatagramPacket( sendBuffer, sendBuffer.length,

serverAddress, 9876

);

clientSocket.send(sendPacket);

if (userInput.equalsIgnoreCase("exit")) {

System.out.println("Closing client...");

break;

byte[] receiveBuffer = new byte[1024];

DatagramPacket receivePacket = new DatagramPacket(receiveBuffer,


receiveBuffer.length);

clientSocket.receive(receivePacket);

String response = new String(receivePacket.getData(), 0, receivePacket.getLength());

System.out.println("Server: " + response);

clientSocket.close();

scanner.close();

} catch (Exception e) {

e.printStackTrace();

}
22A81A0652 CN-LAB

OUTPUT:
22A81A0652 CN-LAB

Task-5

Implementation of Stop and Wait Protocol


Stop and Wait Protocol :

The Stop-and-Wait protocol ensures reliable data transmission by sending one frame at a time
and waiting for an acknowledgment before sending the next. The sender transmits a frame and
pauses until it receives an acknowledgment (ACK) from the receiver. If an ACK is not received
within a timeout period, the sender retransmits the frame. This process continues until all data is
successfully transmitted.

Program :

import java.util.Scanner;

class StopAndWaitProtocol {

private int frame = 0;

private boolean ackReceived = true;

private int totalFrames;

private Scanner scanner = new Scanner(System.in);

public StopAndWaitProtocol(int totalFrames) {

this.totalFrames = totalFrames;

class Sender extends Thread

{ public void run() {

while (frame < totalFrames) {

synchronized (StopAndWaitProtocol.this)

{ while (!ackReceived) {

try {

StopAndWaitProtocol.this.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}
22A81A0652 CN-LAB

if (frame < totalFrames) {

System.out.println("Sender: Sending Frame " + frame);

ackReceived = false;

StopAndWaitProtocol.this.notify();

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

class Receiver extends Thread {

public void run() {

while (frame < totalFrames) {

synchronized (StopAndWaitProtocol.this)

{ while (ackReceived) {

try {

StopAndWaitProtocol.this.wait();

} catch (InterruptedException e) {

e.printStackTrace();

System.out.print("Receiver: Did you receive Frame " + frame + "? (Yes/No): ");
22A81A0652 CN-LAB

String response = scanner.next();

if (response.equalsIgnoreCase("Yes")) {

System.out.println("Receiver: Acknowledgment Sent for Frame " + frame);

ackReceived = true;

frame++;

StopAndWaitProtocol.this.notify();

} else {

System.out.println("Receiver: Frame " + frame + " lost, asking sender to


retransmit...");

ackReceived = false;

StopAndWaitProtocol.this.notify();

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

System.out.println("Receiver: All frames successfully received. Transmission complete.");

scanner.close();

public static void main(String[] args)

{ Scanner scanner = new

Scanner(System.in);

System.out.print("Enter the number of frames to send: ");

int totalFrames = scanner.nextInt();


22A81A0652 CN-LAB

StopAndWaitProtocol protocol = new StopAndWaitProtocol(totalFrames);

Sender sender = protocol.new Sender();

Receiver receiver = protocol.new Receiver();

sender.start();

receiver.start();

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy