0% found this document useful (0 votes)
6 views

Computer Network Manual (1)

The document outlines various programming tasks related to networking and data transmission, including implementing point-to-point networks, Ethernet LANs, error detection using CRC, sliding window protocols, and shortest path algorithms. It also includes client-server communication using TCP/IP sockets, RSA encryption, and congestion control using the leaky bucket algorithm. Each task is accompanied by sample code and expected outputs.

Uploaded by

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

Computer Network Manual (1)

The document outlines various programming tasks related to networking and data transmission, including implementing point-to-point networks, Ethernet LANs, error detection using CRC, sliding window protocols, and shortest path algorithms. It also includes client-server communication using TCP/IP sockets, RSA encryption, and congestion control using the leaky bucket algorithm. Each task is accompanied by sample code and expected outputs.

Uploaded by

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

1 Implement three nodes point – to – point network with duplex links between

them. Set the queue size, vary the bandwidth, and find the number of packets
dropped.

2 Implement transmission of ping messages/trace route over a network


topology consisting of 6 nodes and find the number of packets dropped due to
congestion.

3 Implement an Ethernet LAN using n nodes and set multiple traffic nodes and
plot congestion window for different source / destination.

4 Develop a program for error detecting code using CRC-CCITT (16- bits).

5 Develop a program to implement a sliding window protocol in the data link


layer.

6 Develop a program to find the shortest path between vertices using the
Bellman-Ford and path vector routing algorithm.

7 Using TCP/IP sockets, write a client – server program to make the client send
the file name and to make the server send back the contents of the requested
file if present.

8 Develop a program on a datagram socket for client/server to display the


messages on client side, typed at the server side.

9 Develop a program for a simple RSA algorithm to encrypt and decrypt the
data.

10 Develop a program for congestion control using a leaky bucket algorithm


1 Implement three nodes point – to – point network with duplex links between
them. Set the queue size, vary the bandwidth, and find the number of packets
dropped.

class Node {
int queueSize, dropped = 0;

Node(int size) { queueSize = size; }

void send(Packet[] packets) {


dropped += Math.max(0, packets.length - queueSize);
}
}

class Packet {}

public class Network {


public static void main(String[] args) {
Node A = new Node(10), B = new Node(5), C = new Node(8);
Packet[] packets = new Packet[20];

A.send(packets);
B.send(packets);
C.send(packets);

System.out.println("A dropped: " + A.dropped);


System.out.println("B dropped: " + B.dropped);
System.out.println("C dropped: " + C.dropped);
}
}

Output
A dropped: 10
B dropped: 15
C dropped: 12
2 Implement transmission of ping messages/trace route over a network
topology consisting of 6 nodes and find the number of packets dropped due to
congestion.

class Node {
int queueSize, dropped = 0;
Node(int size) { queueSize = size; }
void send(Packet p) {
if (queueSize > 0) queueSize--;
else dropped++;
}
}
class Packet {}
public class Network {
public static void main(String[] args) {
Node[] nodes = {new Node(5), new Node(4), new Node(6), new Node(3),
new Node(7), new Node(2)};
Packet[] packets = new Packet[20];
for (Packet p : packets)
for (Node node : nodes)
node.send(p);
for (int i = 0; i < nodes.length; i++)
System.out.println("Node " + (i+1) + " dropped: " + nodes[i].dropped);
}
}
Output
Node 1 dropped: 15
Node 2 dropped: 16
Node 3 dropped: 14
Node 4 dropped: 17
Node 5 dropped: 13
Node 6 dropped: 18

3 Implement an Ethernet LAN using n nodes and set multiple traffic nodes and
plot congestion window for different source / destination.
import java.util.Random;

class Node {
int id, queueSize, dropped = 0;

Node(int id, int size) {


this.id = id;
this.queueSize = size;
}

void send(Node dest, int packets) {


int congestionWindow = Math.min(queueSize, packets);
queueSize -= congestionWindow;
dropped += packets - congestionWindow;
System.out.println("Src: " + id + " -> Dest: " + dest.id + ", Congestion
Window: " + congestionWindow);
}
}

public class EthernetLan {


public static void main(String[] args) {
int n = 5; // Number of nodes
Node[] nodes = new Node[n];
Random rand = new Random();

// Initialize nodes with random queue sizes


for (int i = 0; i < n; i++)
nodes[i] = new Node(i + 1, rand.nextInt(10) + 5);

// Simulate traffic between nodes


for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j)
nodes[i].send(nodes[j], rand.nextInt(20) + 5);
}
}

// Print dropped packets for each node


for (Node node : nodes)
System.out.println("Node " + node.id + " dropped packets: " +
node.dropped);
}
}

Output
Src: 5 -> Dest: 2, Congestion Window: 0
Src: 5 -> Dest: 3, Congestion Window: 0
Src: 5 -> Dest: 4, Congestion Window: 0
Node 1 dropped packets: 80
Node 2 dropped packets: 57
Node 3 dropped packets: 59
Node 4 dropped packets: 46
Node 5 dropped packets: 49
4. Develop a program for error detecting code using CRC-CCITT (16- bits).

public class CRC16CCITT {


private static final int POLY = 0x1021;

public static int calculateCRC(byte[] data) {


int crc = 0xFFFF;
for (byte b : data) {
crc ^= (b << 8);
for (int i = 0; i < 8; i++) {
if ((crc & 0x8000) != 0) {
crc = (crc << 1) ^ POLY;
} else {
crc = crc << 1;
}
}
}
return crc & 0xFFFF;
}

public static void main(String[] args) {


byte[] data = "123456789".getBytes();
System.out.printf("CRC-CCITT (16-bit): 0x%04X%n", calculateCRC(data));
}
}

Output
CRC-CCITT (16-bit): 0x29B1

5. Develop a program to implement a sliding window protocol in the data link


layer.

public class Slidingwindow {


static final int WINDOW_SIZE = 4;

public static void main(String[] args) {


int[] frames = {1, 2, 3, 4, 5, 6, 7, 8};
int start = 0, end = Math.min(WINDOW_SIZE, frames.length);

while (start < frames.length) {


System.out.print("Sending frames: ");
for (int i = start; i < end; i++) {
System.out.print(frames[i] + " ");
}
System.out.println();

// Simulate receiving acknowledgment for the current window


start += WINDOW_SIZE;
end = Math.min(start + WINDOW_SIZE, frames.length);
}
}
}
Output
Sending frames: 1 2 3 4
Sending frames: 5 6 7 8
6. Develop a program to find the shortest path between vertices using the
Bellman-Ford and path vector routing algorithm.

import java.util.Arrays;

public class BellmanFord {


static void bellmanFord(int V, int[][] edges, int src) {
int[] dist = new int[V];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[src] = 0;

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


for (int[] edge : edges) {
if (dist[edge[0]] != Integer.MAX_VALUE && dist[edge[0]] +
edge[2] < dist[edge[1]]) {
dist[edge[1]] = dist[edge[0]] + edge[2];
}
}
}

for (int[] edge : edges) {


if (dist[edge[0]] != Integer.MAX_VALUE && dist[edge[0]] + edge[2] <
dist[edge[1]]) {
System.out.println("Negative weight cycle detected");
return;
}
}

for (int i = 0; i < V; i++) System.out.println("Vertex " + i + ": " +


dist[i]);
}

public static void main(String[] args) {


int[][] edges = {
{0, 1, -1}, {0, 2, 4}, {1, 2, 3}, {1, 3, 2}, {1, 4, 2},
{3, 2, 5}, {3, 1, 1}, {4, 3, -3}
};
bellmanFord(5, edges, 0);
}
}

Output
Vertex 0: 0
Vertex 1: -1
Vertex 2: 2
Vertex 3: -2
Vertex 4: 1

7.Using TCP/IP sockets, write a client – server program to make the client send
the file name and to make the server send back the contents of the requested
file if present.
import java.io.*;
import java.net.*;

public class Server1 {


public static void main(String[] args) throws IOException {
try (ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true))
{

String fileName = in.readLine();


File file = new File(fileName);
if (file.exists()) {
try (BufferedReader fileReader = new BufferedReader(new
FileReader(file))) {
fileReader.lines().forEach(out::println);
}
} else {
out.println("File not found.");
}
}
}
}

import java.io.*;
import java.net.*;

public class Client1 {


public static void main(String[] args) throws IOException {
try (Socket socket = new Socket("localhost", 8080);
BufferedReader consoleReader = new BufferedReader(new
InputStreamReader(System.in));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()))) {

System.out.print("Enter the file name: ");


out.println(consoleReader.readLine());

String response;
while ((response = in.readLine()) != null) {
System.out.println(response);
}
}
}
}

9 Develop a program for a simple RSA algorithm to encrypt and decrypt the
data.
import java.math.BigInteger;
import java.util.Scanner;

public class SimpleRsa {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Public and private keys


BigInteger p = new BigInteger("61");
BigInteger q = new BigInteger("53");
BigInteger n = p.multiply(q);
BigInteger phi =
p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
BigInteger e = new BigInteger("17");
BigInteger d = e.modInverse(phi);
// Input message
System.out.print("Enter a message to encrypt (number): ");
BigInteger message = new BigInteger(scanner.nextLine());

// Encryption: c = m^e mod n


BigInteger encrypted = message.modPow(e, n);
System.out.println("Encrypted message: " + encrypted);

// Decryption: m = c^d mod n


BigInteger decrypted = encrypted.modPow(d, n);
System.out.println("Decrypted message: " + decrypted);

scanner.close();
}
}

Output
Enter a message to encrypt (number): 42
Encrypted message: 2557
Decrypted message: 42

10. Develop a program for congestion control using a leaky bucket algorithm

import java.util.Scanner;

public class LeakyBucket {


public static void main(String[] args) {
int bucketSize = 10, outputRate = 4, stored = 0;

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the number of packets arriving: ");
int n = scanner.nextInt();
int[] packets = new int[n];

System.out.println("Enter the size of each packet:");


for (int i = 0; i < n; i++) {
packets[i] = scanner.nextInt();
}

for (int packet : packets) {


if (packet <= (bucketSize - stored)) {
stored += packet;
} else {
System.out.println("Packet of size " + packet + " is dropped.");
}
System.out.println("Bucket stores " + stored + " units.");
stored = Math.max(0, stored - outputRate);
}
scanner.close();
}
}
Output
Enter the number of packets arriving: 5
Enter the size of each packet:
3
6
2
8
5
Bucket stores 3 units.
Bucket stores 6 units.
Bucket stores 4 units.
Bucket stores 8 units.
Bucket stores 9 units.

You might also like