You are on page 1of 27

Program 1

Aim: Program to display name and IP address of a system


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

public class ip_localmachine


{
public static void main(String args[]) throws
Exception
{
InetAddress ipadd=InetAddress.getLocalHost();
System.out.println("Host and Address :"+ipadd);
System.out.println("Host name:"+ipadd.getHostName());
String n=ipadd.toString();
System.out.println("IP address :"+n.substring(n.indexOf("/")+1));
}
}

OUTPUT

Program 2
AIM: Program to obtain the information about the (A) HOST (B) PORT (C)
PROTOCOLS
//PROGRAM TO OBTAIN THE INFORMATION ABOUT THE (A) HOST (B) PORT (C) PROTOCOLS.
import java.lang.* ;
import java.io.*;
import java.net.*;
class ud1
{
public static void main(String args []) throws
MalformedURLException
{ URL url = new URL("http://www.yahoo.com");
try
{

System.out.println("host name is " + url.getHost());


System.out.println("port no. is " + url.getPort());
System.out.println("protocol used is " + url.getProtocol());

}
catch (Exception e)
{
}
}

OUTPUT

System.out.println("error"+e);

Program 3
AIM: Simulate the functioning of lamport logical clock
//WRITE A PROGRAM IN JAVA IN JAVA FOR LAMPORT LOGICAL CLOCK.
import java.util.*;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class Lamport{
int e[][]=new int[10][10];
int en[][]=new int[10][10];
int ev[]=new int[10];
int i,p,j,k;
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
int xpoints[] =new int[5];
int ypoints[] =new int[5];
class Draw extends JFrame{
private final int ARR_SIZE = 4;
void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
Graphics2D g = (Graphics2D) g1.create();
double dx = x2 - x1, dy = y2 - y1;
double angle = Math.atan2(dy, dx);
int len = (int) Math.sqrt(dx*dx + dy*dy);
AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
at.concatenate(AffineTransform.getRotateInstance(angle));
g.transform(at);

// Draw horizontal arrow starting in (0, 0)


g.drawLine(0, 0, len, 0);
g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4); }
public void paintComponent(Graphics g) {
for (int x = 15; x < 200; x += 16)
drawArrow(g, x, x, x, 150);
drawArrow(g, 30, 300, 300, 190);
}
public void paint(Graphics g){
int h1,h11,h12;
Graphics2D go=(Graphics2D)g;
go.setPaint(Color.black);
for(i=1;i<=p;i++)
{
go.drawLine(50,100*i,450,100*i);
}

for(i=1;i<=p;i++) {
for(j=1;j<=ev[i];j++)
{
k=i*10+j;
go.setPaint(Color.blue);
go.fillOval(50*j,100*i-3,5,5);
go.drawString("e"+i+j+"("+en[i][j]+")",50*j,100*i-5);
h1=hm.get(k);
if(h1!=0) {

h11=h1/10;
h12=h1%10;
go.setPaint(Color.red);
drawArrow(go,50*h12+2,100*h11,50*j+2,100*i);
}}}}}
public void calc(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of process:");
p=sc.nextInt();
System.out.println("Enter the no of events per process:");
for(i=1;i<=p;i++)
{
ev[i]=sc.nextInt();
}
System.out.println("Enter the relationship:");
for(i=1;i<=p;i++)
{
System.out.println("For process:"+i);
for(j=1;j<=ev[i];j++)
{
System.out.println("For event:"+(j));
int input=sc.nextInt();
k=i*10+j;
hm.put(k,input);
if(j==1)
en[i][j]=1;
}}

for(i=1;i<=p;i++)
{
for(j=2;j<=ev[i];j++)
{
k=i*10+j;
if(hm.get(k)==0)
{
en[i][j]=en[i][j-1]+1;
}
else
{
int a=hm.get(k);
int p1=a/10;
int e1=a%10;
if(en[p1][e1]>en[i][j-1])
en[i][j]=en[p1][e1]+1;
else
en[i][j]=en[i][j-1]+1;
}}}
for(i=1;i<=p;i++)
{
for(j=1;j<=ev[i];j++)
{
System.out.println(en[i][j]);
}}
JFrame jf=new Draw();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setSize(500,500);
jf.setVisible(true);
}
public static void main(String[] args){
Lamport lam=new Lamport();
lam.calc();
}}

OUTPUT

Program 4
Aim: Program to read the source code of a web page
//WRITE A PROGRAM READ A SOURCE CODE FOR WEB PAGE.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class URLExp {
public static void main(String[] args) {
try {
URL google = new URL("http://www.google.co.in");
URLConnection url = google.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(url.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}}}

Program 5
AIM:WRITE A PROGRAM IN JAVA TO PRINT THE DATE AND TIME OF A SYSTEM.
import java.util.Date;
public class DateDemo {
public static void main(String args[]) {
// Instantiate a Date object
Date date = new Date();
// display time and date using toString()
System.out.println(date.toString());
}
}

OUTPUT

Program 6
AIM: WRITE A PROGRAM FOR Finding DEADLOCK BETWEEN PROCESS.
import java.util.*;
public class SimpleDeadLock extends Thread {
public static Object l1 = new Object();
public static Object l2 = new Object();
private int index;
public static void main(String[] a) {
Thread t1 = new Thread1();
Thread t2 = new Thread2();
t1.start();
t2.start();
}
private static class Thread1 extends Thread {
public void run() {
synchronized (l1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (l2) {
System.out.println("Thread 2: Holding lock 1 & 2...");
}
}
}
}
private static class Thread2 extends Thread {
public void run() {

synchronized (l2) {
System.out.println("Thread 2: Holding lock 2...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for lock 1...");
synchronized (l1) {
System.out.println("Thread 2: Holding lock 2 & 1...");
}
}
}
}
}

OUTPUT

Program 7
AIM: Program to

implement RMI mechanism

//Adder
import java.rmi.*;
public interface Adder extends Remote{
public int add(int x,int y)throws RemoteException;
}
//AdderRemote
import java.rmi.*;
import java.rmi.server.*;
public class AdderRemote extends UnicastRemoteObject implements Adder{
AdderRemote()throws RemoteException{
super();
}
public int add(int x,int y){return x+y;}
}
//MyServer
import java.rmi.*;
import java.rmi.registry.*;
public class MyServer{
public static void main(String args[]){
try{
Adder stub=new AdderRemote();
Naming.rebind("rmi://localhost:5000/sonoo",stub);
}catch(Exception e){System.out.println(e);}
}}
//MyClient
import java.rmi.*;
public class MyClient{
public static void main(String args[]){
try{
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34,4));
}catch(Exception e){}
}}

Program 8
AIM: simulate the functioning of vector clock

//vector clock
import java.util.Iterator;
import java.util.Vector;
public class VectorClock
{
private Vector<Integer> clocks= null;
private int localID = 0;
public VectorClock(int clientID){
clocks= new Vector<Integer>();
for (int i=0; i < clientID; i++)
clocks.add(i, 0);
localID = clientID-1;
}
public Vector<Integer> getVector()
{
return clocks;
}
public void increment(){
clocks.set(localID, clocks.get(localID) + 1);
}
public void update(VectorClock c){
Iterator<Integer> j = c.getVector().iterator();
if (clocks.size() < c.getVector().size())
clocks.setSize(c.getVector().size());

for (int i = 0;j.hasNext(); i++)


{
int next = j.next();
if ( i != localID)
if (clocks.get(i) == null || clocks.get(i) < next)
clocks.set(i, next);
}
}
public void extend(){
clocks.add(0);
}
public void extend(int e){
clocks.add(e);
}
public boolean lessThan(VectorClock c){
Iterator<Integer> i = clocks.iterator();
Iterator<Integer> j = c.getVector().iterator();
while (i.hasNext() && j.hasNext()){
if ( i.next() > j.next())
return false;
}
return true;
}
public boolean equals(
VectorClock c){
Iterator<Integer> i = clocks.iterator();
Iterator<Integer> j = c.getVector().iterator();
if (clocks.size() != c.getVector().size())
return false;

while (i.hasNext() && j.hasNext()){


if ( i.next() != j.next())
return false;
}
return true;
}
public String toString(){
return clocks.toString();
}
public static void main(String s[])
{
Vector v=new Vector();
v.add(new VectorClock(1));
v.add(new VectorClock(12));
v.add(new VectorClock(9));
v.add(new VectorClock(10));
v.add(new VectorClock(8));
v.add(new VectorClock(11));
Iterator itr=v.iterator();
while(itr.hasNext())
{
VectorClock vc=(VectorClock)itr.next();
vc.increment();
vc.extend(2);
System.out.println(vc.getVector()); ;
}}}

OUTPUT

Program 9
AIM: Program for LRU Page replacement
//LRU Page replacement
import java.io.*;
class LRU
{
public static int sort(int c[])
{
int max=-1;
int temp=-1;
for(int k=0;k<3;k++)
{
if(c[k]>max)
{
max=c[k];
temp=k;
}
}
return temp;
}
public static void main(String args[])throws IOException
{
int z,m=0,hit=0,faults=0;
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println(enter the size of the array);
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];

int flag[]=new int[n];


System.out.println(enter the elements);
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
flag[i]=0;
}
int b[]=new int[3];
int c[]=new int[3];
for(int i=0;i<3;i++)
{
b[i]=-1;
c[i]=0;
}
for(int i=0;i<n;i++)
{
z=a[i];
for(int j=0;j<3;j++)
{
if(z==b[j])
{
flag[i]=1;
hit=hit+1;
break;
}
}
if (flag[i]==0 && b[2]==-1)
{
for(int j=0;j<3;j++)

{
if(b[j]==-1)
{
b[j]=z;
faults=faults+1;
flag[i]=1;
break;
}
}
}
if(flag[i]==0)
{
m=sort(c);
b[m]=z;
faults=faults+1;
flag[i]=1;
for(int k=0;k<3;k++)
c[k]=c[k]+1;
c[m]=0;
}
}
System.out.println(no of hits+hit);
System.out.println(no of faults+faults);
System.out.println(hit ratio+(hit*100)/(hit+faults));

}
}

OUTPUT

Program 10
AIM : Client-server chat using TCP/IP
//server
import java.io.*;
import java.net.*;
public class GossipServer
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000);
System.out.println("Server

ready for chatting");

Socket sock = sersock.accept( );


// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead


InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new
InputStreamReader(istream));

String receiveMessage, sendMessage;


while(true)
{
if((receiveMessage = receiveRead.readLine()) != null)
{

object)

System.out.println(receiveMessage);
}
sendMessage = keyRead.readLine();
pwrite.println(sendMessage);
pwrite.flush();
}
}
}
//Client
import java.io.*;
import java.net.*;
public class GossipClient
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("127.0.0.1", 3000);
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead

object)

InputStream istream = sock.getInputStream();


BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

System.out.println("Start the chitchat, type and press Enter key");

String receiveMessage, sendMessage;

while(true)
{
sendMessage = keyRead.readLine();

// keyboard reading

pwrite.println(sendMessage);

// sending to server

pwrite.flush();

// flush the data

if((receiveMessage = receiveRead.readLine()) != null) //receive from server


{
System.out.println(receiveMessage); // displaying at DOS prompt
}
}
}
}

OUTPUT
SERVER

CLIENT

You might also like