Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

TECHNO ENGINEERING COLLEGE BANIPUR

Habra, West Bengal - 743233

COMPUTER SCIENCE & ENGINEERING

COMPUTER NETWORKS LAB


PCC CS692

NAME : SAMBADI SEN


ROLL : 24400119015
SEMESTER : 6TH

TEACHER’S SIGNATURE
COMPUTER NETWORKS ASSIGNMENT
1. Write a program in Java for distance vector algorithm to find suitable path for
transmission.

CODE:
import java.io.*;
public class shortpath
{
static int graph[][];
static int via[][];
static int rt[][];
static int v;
static int e;

public static void main(String args[]) throws IOException


{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Please enter the number of Vertices: ");


v = Integer.parseInt(br.readLine());

System.out.println("Please enter the number of Edges: ");


e = Integer.parseInt(br.readLine());

graph = new int[v][v];


via = new int[v][v];
rt = new int[v][v];
for(int i = 0; i < v; i++)
for(int j = 0; j < v; j++)
{
if(i == j)
graph[i][j] = 0;
else
graph[i][j] = 9999;
}

for(int i = 0; i < e; i++)


{
System.out.println("Please enter data for Edge " + (i + 1) + ":");
System.out.print("Source: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Destination: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;
}

dvr_calc_disp("The initial Routing Tables are: ");

System.out.print("Please enter the Source Node for the edge whose cost has
changed: ");
int s = Integer.parseInt(br.readLine());
s--;
System.out.print("Please enter the Destination Node for the edge whose cost has
changed: ");
int d = Integer.parseInt(br.readLine());
d--;
System.out.print("Please enter the new cost: ");
int c = Integer.parseInt(br.readLine());
graph[s][d] = c;
graph[d][s] = c;

dvr_calc_disp("The new Routing Tables are: ");


}

static void dvr_calc_disp(String message)


{
System.out.println();
init_tables();
update_tables();
System.out.println(message);
print_tables();
System.out.println();
}

static void update_table(int source)


{
for(int i = 0; i < v; i++)
{
if(graph[source][i] != 9999)
{
int dist = graph[source][i];
for(int j = 0; j < v; j++)
{
int inter_dist = rt[i][j];
if(via[i][j] == source)
inter_dist = 9999;
if(dist + inter_dist < rt[source][j])
{
rt[source][j] = dist + inter_dist;
via[source][j] = i;
}
}
}
}
}
static void update_tables()
{
int k = 0;
for(int i = 0; i < 4*v; i++)
{
update_table(k);
k++;
if(k == v)
k = 0;
}
}

static void init_tables()


{
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
if(i == j)
{
rt[i][j] = 0;
via[i][j] = i;
}
else
{
rt[i][j] = 9999;
via[i][j] = 100;
}
}
}
}

static void print_tables()


{
for(int i = 0; i < v; i++)
{
for(int j = 0; j < v; j++)
{
System.out.print("Dist: " + rt[i][j] + " ");
}
System.out.println();
}
}
}
OUTPUT:

2. Using TCP/IP sockets, write a client-server program to make client sending the
file name and the server to send back the contents of the requested file if
present.
3. Implement the above program using as message queues or FIFOs as IPC
channels.

CODE(2&3 COMBINED):
Client
import java.net.*;
import java.io.*;
public class ContentsClient
{
public static void main( String args[ ] ) throws Exception
{
Socket sock = new Socket(“127.0.0.1", 4000);
System.out.print("Enter the file name");
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
String fname = keyRead.readLine();

OutputStream ostream = sock.getOutputStream( );


PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.println(fname);

InputStream istream = sock.getInputStream();


BufferedReader socketRead = new BufferedReader(new InputStreamReader(istream));
String str;
while((str = socketRead.readLine()) != null)
{
System.out.println(str);
}
pwrite.close();
socketRead.close();
keyRead.close();
}
}

Server
import java.net.*;
import java.io.*;
public class ContentsServer
{
public static void main(String args[]) throws Exception
{

ServerSocket sersock = new ServerSocket(4000);


System.out.println("Server ready for connection");
Socket sock = sersock.accept();
System.out.println("Connection is successful and wating for client request");

InputStream istream = sock.getInputStream( );


BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));
String fname = fileRead.readLine( );
BufferedReader contentRead = new BufferedReader(new FileReader(fname) );

OutputStream ostream = sock.getOutputStream( );


PrintWriter pwrite = new PrintWriter(ostream, true);

String str;
while((str = contentRead.readLine()) != null)
{
pwrite.println(str);
}
sock.close();
sersock.close();
pwrite.close();
fileRead.close();
contentRead.close();
}
}

OUTPUT:

4. Write a program for simple RSA algorithm to encrypt and decrypt the data.

CODE:

import java.math.*;
import java.util.*;

class RSA {
public static void main(String args[])
{
int p, q, n, z, d = 0, e, i;
int msg = 12;
double c;
BigInteger msgback;

p = 3;
q = 11;
n = p * q;
z = (p - 1) * (q - 1);
System.out.println("the value of z = " + z);

for (e = 2; e < z; e++) {

if (gcd(e, z) == 1) {
break;
}
}
System.out.println("the value of e = " + e);
for (i = 0; i <= 9; i++) {
int x = 1 + (i * z);
if (x % e == 0) {
d = x / e;
break;
}
}
System.out.println("the value of d = " + d);
c = (Math.pow(msg, e)) % n;
System.out.println("Encrypted message is : " + c)

BigInteger N = BigInteger.valueOf(n);
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Decrypted message is : "+ msgback);
}

static int gcd(int e, int z)


{
if (e == 0)
return z;
else
return gcd(z % e, e);
}
}
OUTPUT:

5. Write a program for congestion control using Leaky bucket algorithm

CODE:

import java.io.*;
import java.util.*;

class Leakybucket {
public static void main (String[] args) {
int no_of_queries,storage,output_pkt_size;
int input_pkt_size,bucket_size,size_left;

storage=0;

no_of_queries=4;

bucket_size=10;

input_pkt_size=4;

output_pkt_size=1;
for(int i=0;i<no_of_queries;i++)
{
size_left=bucket_size-storage;

if(input_pkt_size<=(size_left))
{
storage+=input_pkt_size;
System.out.println("Buffer size= "+storage+
" out of bucket size= "+bucket_size);
}
else
{
System.out.println("Packet loss = "
+(input_pkt_size-(size_left)));

storage=bucket_size;

System.out.println("Buffer size= "+storage+


" out of bucket size= "+bucket_size);

}
storage-=output_pkt_size;
}
}
}

OUTPUT:

You might also like