Record T-1to10
Record T-1to10
Task-1
Provides details such as IP address, subnet mask, and default gateway for all adapters.
ipconfig /all
Includes additional information like DNS servers, MAC addresses, and DHCP settings.
22A81A0652 CN-LAB
2. ping
Measures round-trip time and packet loss, helping diagnose network issues.
3. tracert
4. hostname
5. nslookup
Useful for verifying DNS configurations and troubleshooting name resolution problems.
22A81A0652 CN-LAB
6. netstat
Provides details on protocol use, local and foreign addresses, and connection states.
22A81A0652 CN-LAB
Task-2
Generating a Checksum:
o The receiver performs the same division using the received data.
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
Bit Stuffing:
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
Output:
22A81A0652 CN-LAB
Task-4
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
escapeFlag = true;
} else {
result.append(currentChar);
}
}
return result.toString();
}
}
Output:
22A81A0652 CN-LAB
Task-6
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
Task-7
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
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
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
OUTPUT:
22A81A0652 CN-LAB
Task-10
Programs:
UDP Server :
import java.net.DatagramPacket;
import java.net.DatagramSocket;
try {
while (true) {
serverSocket.receive(receivePacket);
if (receivedMessage.equalsIgnoreCase("exit")) {
break;
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;
{ try {
while (true) {
serverAddress, 9876
);
clientSocket.send(sendPacket);
if (userInput.equalsIgnoreCase("exit")) {
System.out.println("Closing client...");
break;
clientSocket.receive(receivePacket);
clientSocket.close();
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
22A81A0652 CN-LAB
OUTPUT:
22A81A0652 CN-LAB
Task-5
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 {
this.totalFrames = totalFrames;
synchronized (StopAndWaitProtocol.this)
{ while (!ackReceived) {
try {
StopAndWaitProtocol.this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
22A81A0652 CN-LAB
ackReceived = false;
StopAndWaitProtocol.this.notify();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
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
if (response.equalsIgnoreCase("Yes")) {
ackReceived = true;
frame++;
StopAndWaitProtocol.this.notify();
} else {
ackReceived = false;
StopAndWaitProtocol.this.notify();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
scanner.close();
Scanner(System.in);
sender.start();
receiver.start();
OUTPUT: