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

CN Codes

computer network lab course
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

CN Codes

computer network lab course
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Prog1(CRC1.

java)
import java.util.Scanner;

public class CRC1 {


public static String msg,gen;
public static int[] data,divisor;

private static void init() {


data = new int[msg.length()+gen.length()-1];
divisor = new int[gen.length()];

for(int i=0; i<msg.length(); i++)


data[i] = msg.charAt(i)-'0';

for(int i=0; i<gen.length(); i++)


divisor[i] = gen.charAt(i)-'0';
}

private static void calcCRC() {


for(int i=0; i<msg.length(); i++) {
if(data[i] == 1) {
for(int j=0; j<divisor.length; j++) {
data[i+j] ^= divisor[j];
}
}
}
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter message bits: ");
msg = sc.nextLine();
System.out.print("Enter generator: ");
gen = sc.nextLine();
init();
calcCRC();
System.out.print("The checksum code is: ");
for(int i=msg.length(); i<data.length; i++) {
System.out.print(data[i]);
}
System.out.println();
System.out.print("Enter checksum code: ");
msg = sc.nextLine();
System.out.print("Enter generator: ");
gen = sc.nextLine();
init();
calcCRC();
boolean valid = true;
for(int i=0; i<data.length; i++) {
if(data[i] == 1) {
valid = false;
break;
}
}
System.out.println(valid?"Data stream is valid":"Data stream is invalid. CRC
error occurred.");
}
}
Prog2(Sliding.java)
import java.util.Scanner;
public class Sliding {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cF = 0;
int ack = -1;
System.out.print("Enter the window size: ");
int wS = sc.nextInt();

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


int tF = sc.nextInt();

while (cF < tF) {


for (int i = 0; i < Math.min(wS,tF);i++) {
System.out.println("Sending frame: " + (cF + i));
}
System.out.print("Enter the ack for the frame received (-1 for no ack): ");
ack = sc.nextInt();

if (ack == -1) {
System.out.println("No ack received. Resending window from frame: " +
cF);
} else {
System.out.println("Acknowledgment received for frame: " + ack);
cF = ack + 1;
}
}

System.out.println("All frames sent successfully!");


sc.close();
}
}
Prog3(BellmanFord.java)
import java.util.Arrays;
import java.util.Scanner;
class BellmanFord {
static class Edge {
int src, dest, weight;
Edge(int s, int d, int w) {
src = s;
dest = d;
weight = w;
}
}
int V, E;
Edge[] edge;
BellmanFord(int v, int e) {
V = v;
E = e;
edge = new Edge[e];
}
void bellmanFord(int src) {
int[] dist = new int[V];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[src] = 0;
for (int i = 1; i < V; ++i) {
for (int j = 0; j < E; ++j) {
int u = edge[j].src;
int v = edge[j].dest;
int weight = edge[j].weight;
if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
}
}
}
for (int j = 0; j < E; ++j) {
int u = edge[j].src;
int v = edge[j].dest;
int weight = edge[j].weight;
if (dist[u] != Integer.MAX_VALUE && dist[u] + weight < dist[v]) {
System.out.println("Graph contains negative weight cycle");
return;
}
}
System.out.println("Vertex Distance from Source");
for (int i = 0; i < V; ++i) {
System.out.println(i + "\t\t" + dist[i]);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of vertices and edges:");
int V = scanner.nextInt();
int E = scanner.nextInt();
BellmanFord graph = new BellmanFord(V, E);
System.out.println("Enter the source, destination and weight for each edge:");
for (int i = 0; i < E; ++i) {
int src = scanner.nextInt();
int dest = scanner.nextInt();
int weight = scanner.nextInt();
graph.edge[i] = new Edge(src, dest, weight);
}
System.out.println("Enter the source vertex:");
int src = scanner.nextInt();

graph.bellmanFord(src);
scanner.close();
}
}
Prog4(FileServer.java)
import java.io.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
try {
serverSocket = new ServerSocket(5050);
System.out.println("Server is running and waiting for connections...");
socket = serverSocket.accept();
System.out.println("Client connected!");
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String fileName = in.readLine();
System.out.println("Requested file: " + fileName);
File file = new File(fileName);
if (file.exists() && !file.isDirectory()) {
BufferedReader fileReader = new BufferedReader(new FileReader(file));
String line;
while ((line = fileReader.readLine()) != null) {
out.println(line);
}
fileReader.close();
} else {
out.println("File not found!");
}
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Prog4(FileClient.java)
import java.io.*;
import java.net.*;
public class FileClient {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("localhost", 5050);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the file name to request from the server: ");
String fileName = userInput.readLine();
out.println(fileName);
String response;
System.out.println("File contents:");
while ((response = in.readLine()) != null) {
System.out.println(response);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Prog5(DatagramServer.java)
import java.net.*;
import java.util.Scanner;
public class DatagramServer {
public static void main(String[] args) {
DatagramSocket socket = null;
Scanner scanner = new Scanner(System.in);
try {
socket = new DatagramSocket(9876);
System.out.println("Server is running...");
while (true) {
System.out.print("Enter message to send: ");
String message = scanner.nextLine();
byte[] buffer = message.getBytes();
InetAddress clientAddress = InetAddress.getByName("localhost");
int clientPort = 9877;
DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
clientAddress, clientPort);
socket.send(packet);
System.out.println("Message sent to client.");
if (message.equalsIgnoreCase("exit")) {
System.out.println("Server shutting down.");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
scanner.close();
}
}
}
Prog5(DatagramClient.java)
import java.net.*;
public class DatagramClient {
public static void main(String[] args) {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(9877);
System.out.println("Client is ready to receive messages...");
while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("Received from server: " + message);
if (message.equalsIgnoreCase("exit")) {
System.out.println("Client shutting down.");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
}
}
import java.math.BigInteger;
import java.security.SecureRandom;
Prog6.java import java.util.Scanner;
public class RSA{
private BigInteger n, d, e;
private int bitlen = 1024;
public RSA() {
SecureRandom r = new SecureRandom();
BigInteger p = new BigInteger(bitlen/2, 100, r);
BigInteger q = new BigInteger(bitlen/2, 100, r);
n=p.multiply(q);
BigInteger phi =
(p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
e=new BigInteger("65537");
d = e.modInverse(phi);
}
public byte[] encrypt(byte[] message) {
return (new BigInteger(message)).modPow(e, n).toByteArray();
}
public byte[] decrypt(byte[] encrypted) {
return (new BigInteger(encrypted)).modPow(d, n).toByteArray();
}
public static void main(String[] args) {
RSA rsa = new RSA();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a message to encrypt: ");
String text = scanner.nextLine();
byte[] encrypted=rsa.encrypt(text.getBytes());
byte[] decrypted = rsa.decrypt(encrypted);
System.out.println("Encrypted: " + new BigInteger(encrypted).toString(16));
System.out.println("Decrypted: " + new String(decrypted));
scanner.close();
}
}
Prog7.java
import java.util.Scanner;

public class LeakyBucket {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter bucket capacity: ");
int bC = sc.nextInt();
System.out.print("Enter output rate: ");
int oR = sc.nextInt();
int cW=0; //cW->current Water
while (true) {
System.out.print("Enter packet size (or -1 to quit): ");
int pS = sc.nextInt(); //pS->PacketSize
if (pS == -1)
break;
if (cW + pS > bC) {
System.out.println("Packet of size "+pS+" dropped. Bucket overflow.");
} else {
cW += pS;
System.out.println("Packet of size "+pS+" added to the bucket.");
}
if (cW <= 0) {
System.out.println("No packets to process.");
}else {
int leaked = Math.min(cW, oR);
cW -= leaked;
System.out.println("Leaked "+leaked+" units from the bucket.
Remaining water: " + cW);
}
}
sc.close();
}
}
Prog8(program1.tcl) $cbr0 set interval_ 0.005

set ns [new Simulator] Extra spaces are added for readability, but when
executing the code, use only a single
set nf [open program1.nam w]
space between elements to avoid errors. Copy
$ns namtrace-all $nf pasting still works :)

set tf [open program1.tr w] set null0 [new Agent/Null]

$ns trace-all $tf $ns attach-agent $n3 $null0

proc finish {} { $ns connect $udp0 $null0

global ns nf tf $ns connect $udp1 $null0

$ns flush-trace $ns at 0.1 "$cbr0 start"

close $tf $ns at 0.2 "$cbr1 start"

exec nam program1.nam & $ns at 1.0 "finish"

exit 0 $ns run

} Prog8(program1.awk)

for {set i 0} {$i<4} {incr i} { BEGIN { c=0;}{

set n$i [$ns node] if ($1=="d"){

} c++;

$ns duplex-link $n0 $n2 200Mb 10ms DropTail printf("%s\t %s\n",$5,$11);

$ns duplex-link $n1 $n2 100Mb 5ms DropTail }

$ns duplex-link $n2 $n3 1Mb 1000ms DropTail }

$ns queue-limit $n0 $n2 10 END{

$ns queue-limit $n1 $n2 10 printf("The number of packets dropped =%d\n",c);

for {set i 0} {$i<3} {incr i} { }

set udp$i [new Agent/UDP]

$ns attach-agent [set n$i] [set udp$i]

set cbr$i [new Application/Traffic/CBR]

[set cbr$i] attach-agent [set udp$i]

$cbr0 set packetSize_ 500


Prog9(program2.tcl) $ns attach-agent $n5 $p5

set ns [new Simulator] $p1 set packetSize_ 50000

set nf [open program2.nam w] $p1 set interval_ 0.0001

$ns namtrace-all $nf $p3 set packetSize_ 30000

set tf [open program2.tr w] $p3 set interval_ 0.00001

$ns trace-all $tf $ns queue-limit $n0 $n4 5

proc finish {} { $ns queue-limit $n2 $n4 3

global ns nf tf $ns queue-limit $n4 $n5 2

$ns flush-trace Agent/Ping instproc recv {from rtt} {

close $nf puts "node [$self set node_ id] received answer
from $from with round trip time $rtt ms"
close $tf
}
exec nam program2.nam &
$ns connect $p1 $p5
exit 0
$ns connect $p3 $p4
}
$ns connect $p2 $p5
for {set i 0} {$i<6} {incr i} {
for {set t 0} {$t<=2.9} {set t [expr $t + 0.1]} {
set n$i [$ns node]
$ns at $t "$p1 send"
}
}
$n4 shape box
for {set t 0} {$t<=2.9} {set t [expr $t + 0.1]} {
$ns duplex-link $n0 $n4 1005Mb 1ms DropTail
$ns at $t "$p3 send"
$ns duplex-link $n1 $n4 50Mb 1ms DropTail
}
$ns duplex-link $n2 $n4 2000Mb 1ms DropTail
$ns at 3.0 "finish"
$ns duplex-link $n3 $n4 200Mb 1ms DropTail
$ns run
$ns duplex-link $n4 $n5 1Mb 1ms DropTail Prog8(program2.awk)
BEGIN{drop=0;}{
for {set i 0} {$i<4} {incr i} { if($1=="d"){

set p[expr $i+1] [new Agent/Ping] drop++;


}
$ns attach-agent [set n$i] [set p[expr $i+1]]
}END{
} printf("total number of %s packets dropped due to
congestion= %d \n",$5,drop);
set p5 [new Agent/Ping]
}

You might also like