CN Manual
CN Manual
proc finish {} {
global f nf ns
$ns flush-trace
close $f
close $nf
exec nam lab1.nam &
exit 0
}ns
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
$n0 label "TCP Source"
$n1 label "UDP Source"
$n2 label "Sink"
$ns color 1 red
$ns color 2 yellow
proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
exec nam lab2.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
proc sendPingPacket {} {
global ns ping0 ping1 ping4 ping5
set intervalTime 0.001
set now [$ns now]
$ns at [expr $now + $intervalTime] "$ping0 send"
$ns at [expr $now + $intervalTime] "$ping1 send"
$ns at [expr $now + $intervalTime] "$ping4 send"
$ns at [expr $now + $intervalTime] "$ping5 send"
proc finish {} {
global ns f nf outFile1 outFile2
$ns flush-trace
close $f
close $nf
exec nam lab3.nam &
exec xgraph Congestion1.xg -geometry 400x400 &
exec xgraph Congestion2.xg -geometry 400x400 &
exit 0
}
$ns color 1 red
$ns color 2 green
$ns make-lan "$n0 $n1 $n2 $n3 $n4 $n5 $n6 $n7" 10Mb 30ms LL
Queue/DropTail Mac/802_3
import java.io.*;
class Crc
{
public static void main(String args[]) throws
IOException {
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int[ ] data;
int[ ]div;
int[ ]divisor;
int[ ]rem;
int[ ] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in
divisor : ");
divisor_bits=Integer.parseInt(br.readLine())
; divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
*/ tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
rem[j] = crc[j];
}
if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}
5.Develop a program to implement a sliding window protocol in the data link layer.
import java.util.Scanner;
int ack = 0;
int i = 0;
scanner.close();
}
}
6.Develop a program to find the shortest path between vertices using the Bellman-Ford and
path vector routing algorithm
import java.util.Scanner;
D[source] = 0;
for (int node = 1; node <= num_ver - 1; node++)
{
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
D[dn] = D[sn] + A[sn][dn];
}
}
}
}
for (int sn = 1; sn <= num_ver; sn++)
{
for (int dn = 1; dn <= num_ver; dn++)
{
if (A[sn][dn] != MAX_VALUE)
{
if (D[dn] > D[sn]+ A[sn][dn])
System.out.println("The Graph contains negative
egde cycle");
}
}
}
for (int vertex = 1; vertex <= num_ver; vertex++)
{
System.out.println("distance of source " + source + " to "+ vertex
+"
is " + D[vertex]);
}
}
A[sn][dn] = scanner.nextInt();
if (sn == dn)
{
A[sn][dn] = 0;
continue;
}
if (A[sn][dn] == 0)
{
A[sn][dn] = MAX_VALUE;
}
}
}
System.out.println("Enter the source vertex");
source = scanner.nextInt();
BellmanFord b = new BellmanFord
(num_ver);
b.BellmanFordEvaluation(source, A);
scanner.close();
}
}
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.
TCP Server
import java.net.*;
import java.io.*;
class TCPserver{
public static void main(String args[]) throws Exception {
ServerSocket ss=new ServerSocket(4000);
System.out.println("Server ready for connection");
Socket s=ss.accept();
System.out.println("Connection is successful and waiting for chatting");
InputStream istream=s.getInputStream();
BufferedReader fileRead=new BufferedReader(new InputStreamReader(istream));
String fname=fileRead.readLine();
BufferedReader contentRead=new BufferedReader(new FileReader(fname));
OutputStream ostream=s.getOutputStream();
PrintWriter pwrite=new PrintWriter(ostream,true);
String str;
while((str=contentRead.readLine())!=null)
pwrite.println(str);
s.close();
ss.close();
pwrite.close();
fileRead.close();
contentRead.close();
}
}
Note: Create two different files Client.java and Server.java. Follow the steps given:
1. Open a terminal run the server program and provide the filename to send
2. Open one more terminal run the client program and provide the IP address of the
server. We can give localhost address “127.0.0.1” as it is running on same machine
or give the IP address of the machine.
3. Send any start bit to start sending file.
4. Refer https://www.tutorialspoint.com/java/java_networking.htmfor all the
parameters, methods description in socket communication.
At server side:
C:\Users\SP\Desktop\JavaP>javac TCPserver.java
C:\Users\SP\Desktop\JavaP>java TCPserver
Server ready for connection
Connection is successful and waiting for chatting
At client side:
C:\Users\SP\Desktop\JavaP>javac TCPclient.java
C:\Users\SP\Desktop\JavaP>java TCPclient
Enter the file name
TCP server.java
import java.net.*;
import java.io.*;
class TCPserver{
public static void main(String args[]) throws Exception {
ServerSocket ss=new ServerSocket(4000);
System.out.println("Server ready for connection");
Socket s=ss.accept();
System.out.println("Connection is successful and waiting for chatting");
InputStream istream=s.getInputStream();
BufferedReader fileRead=new BufferedReader(new InputStreamReader(istream));
String fname=fileRead.readLine();
BufferedReader contentRead=new BufferedReader(new FileReader(fname));
OutputStream ostream=s.getOutputStream();
PrintWriter pwrite=new PrintWriter(ostream,true);
String str;
while((str=contentRead.readLine())!=null)
pwrite.println(str);
s.close();
ss.close();
pwrite.close();
fileRead.close();
contentRead.close();
}
}
8. Develop a program on a datagram socket for client/server to display the messages
on client side, typed at the server side.
UDP Server
import java.io.*;
import java.net.*;
class UDPServer
{
public static void main(String args[])
{
try{ String message=args[0];
int serverPortNumber=Integer.parseInt(args[1]);
ServerSocket connectionSocket=new ServerSocket(serverPortNumber);
Socket dataSocket=connectionSocket.accept();
(dataSocket.getOutputStream());
socketOutput.println(message);
socketOutput.flush();
dataSocket.close();
connectionSocket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Steps to Run the Program
C:\Users\SP\Desktop\JavaP>javac UDPServer.java
C:\Users\SP\Desktop\JavaP>
UDP Client
import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[])
{
try
{
InetAddress acceptorHost=InetAddress.getByName(args[0]);
int ServerPortNum=Integer.parseInt(args[1]);
Socket clientSocket=new Socket(acceptorHost,ServerPortNum);
BufferedReader br=new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
System.out.println(br.readLine());
clientSocket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Steps to Run the Program
C:\Users\SP\Desktop\JavaP>javac UDPClient.java
9.Develop a program for a simple RSA algorithm to encrypt and decrypt the data.
RSA Key Generation
import java.util.*;
import java.math.BigInteger;
import java.lang.*;
class RSAkeygen
{
public static void main(String[] args)
{
Random rand1=new Random(System.currentTimeMillis());
Random rand2=new Random(System.currentTimeMillis()*10);
int pubkey=Integer.parseInt(args[0]);
BigInteger bigB_n=bigB_p.multiply(bigB_q);
BigInteger bigB_p_1_q_1=bigB_p_1.multiply(bigB_q_1);
while(true)
{
BigInteger BigB_GCD=bigB_p_1_q_1.gcd(new BigInteger(""+pubkey));
if(BigB_GCD.equals(BigInteger.ONE))
{
break;
}
pubkey++;
}
BigInteger bigB_pubkey=new BigInteger(""+pubkey);
BigInteger bigB_prvkey=bigB_pubkey.modInverse(bigB_p_1_q_1);
System.out.println("public key : "+bigB_pubkey+","+bigB_n);
System.out.println("private key : "+bigB_prvkey+","+bigB_n);
}
}
Steps to Compile & Run the Program
Output:
Key Generation
import java.math.BigInteger;
import java.util.*;
class RSAEncDec
{
public static void main(String[] args)
{
BigInteger bigB_pubkey = new BigInteger(args[0]);
BigInteger bigB_prvkey = new BigInteger(args[1]);
int asciiVal=Integer.parseInt(args[3]);
BigInteger bigB_plainVal=bigB_cipherVal.modPow(bigB_prvkey,bigB_n);
int plainVal=bigB_plainVal.intValue();
class Queue
{
int q[],f=0,r=0,size;
void insert(int n)
{
Scanner in = new Scanner(System.in);
q=new int[10];
for(int i=0;i<n;i++)
{
System.out.print("\nEnter " + i + " element: ");
int ele=in.nextInt();
if(r+1>10)
{
System.out.println("\nQueue is full \nLost Packet: "+ele);
break;
}
else
{
r++;
q[i]=ele;
}
}
}
void delete()
{
Scanner in = new Scanner(System.in);
Thread t=new Thread();
if(r==0)
System.out.print("\nQueue empty ");
else
{
for(int i=f;i<r;i++)
{
try
{
t.sleep(1000);
}
catch(Exception e){}
System.out.print("\nLeaked Packet: "+q[i]);
f++;
}
}
System.out.println();
}
}
class Leaky extends Thread
{
public static void main(String ar[]) throws Exception
{
Queue q=new Queue();
Scanner src=new Scanner(System.in);
System.out.println("\nEnter the packets to be sent:");
int size=src.nextInt();
q. insert(size);
q.delete();
}
}
Output: