Thank you!

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown ...

Minimal Design

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown ...

Download high quality wordpress themes at top-wordpress.net

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown ...

Easy to use theme admin panel

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown ...

Archive for April 2013

http header


import java.io.*;
import java.net.*;
import java.util.Scanner;

/** Fetches the HTML content of a web page (or HTTP header) as a String. */
public final class WebPageFetcher {

  /**
  * Demo harness.
  *
  * <ul>
  * <li>aArgs[0] : an HTTP URL
  * <li>aArgs[1] : (header | content)
  * </ul>
  */
  public static void main(String... aArgs) throws MalformedURLException {
    String url = aArgs[0];
    String option = aArgs[1];
    WebPageFetcher fetcher = new  WebPageFetcher(url);
    if ( HEADER.equalsIgnoreCase(option) ) {
      log( fetcher.getPageHeader() );
    }
    else if ( CONTENT.equalsIgnoreCase(option) ) {
      log( fetcher.getPageContent() );
    }
    else {
      log("Unknown option.");
    }
  }

  public WebPageFetcher( URL aURL ){
    if ( ! HTTP.equals(aURL.getProtocol())  ) {
      throw new IllegalArgumentException("URL is not for HTTP Protocol: " + aURL);
    }
    fURL = aURL;
  }

  public WebPageFetcher( String aUrlName ) throws MalformedURLException {
    this ( new URL(aUrlName) );
  }

  /** Fetch the HTML content of the page as simple text.   */
  public String getPageContent() {
    String result = null;
    URLConnection connection = null;
    try {
      connection =  fURL.openConnection();
      Scanner scanner = new Scanner(connection.getInputStream());
      scanner.useDelimiter(END_OF_INPUT);
      result = scanner.next();
    }
    catch ( IOException ex ) {
      log("Cannot open connection to " + fURL.toString());
    }
    return result;
  }

  /** Fetch HTML headers as simple text.  */
  public String getPageHeader(){
    StringBuilder result = new StringBuilder();

    URLConnection connection = null;
    try {
      connection = fURL.openConnection();
    }
    catch (IOException ex) {
      log("Cannot open connection to URL: " + fURL);
    }

    //not all headers come in key-value pairs - sometimes the key is
    //null or an empty String
    int headerIdx = 0;
    String headerKey = null;
    String headerValue = null;
    while ( (headerValue = connection.getHeaderField(headerIdx)) != null ) {
      headerKey = connection.getHeaderFieldKey(headerIdx);
      if ( headerKey != null && headerKey.length()>0 ) {
        result.append( headerKey );
        result.append(" : ");
      }
      result.append( headerValue );
      result.append(NEWLINE);
      headerIdx++;
    }
    return result.toString();
  }

  // PRIVATE //
  private URL fURL;
 
  private static final String HTTP = "http";
  private static final String HEADER = "header";
  private static final String CONTENT = "content";
  private static final String END_OF_INPUT = "\\Z";
  private static final String NEWLINE = System.getProperty("line.separator");

  private static void log(Object aObject){
    System.out.println(aObject);
  }
}

Ring




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Ring {
static int n,pc,in;
static String s;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter the Number of Clients");
n=Integer.parseInt(br.readLine());
System.out.println("Enter the Pri-Coordinator");
pc=Integer.parseInt(br.readLine());
System.out.println("Enter the Introducer");
in=Integer.parseInt(br.readLine());
s="";
for(int i=in;;)
{
System.out.print(i+"Send the Election messege to ");
s=s+i;
i=i+1;
if(i==pc)
i=i+1;
if(i>n)
i=1;
System.out.print(i+" The messege is "+s+"\n");
if(i==in)
break;
}
for(int i=n;i>=1;i--)
{
if(i!=pc && i>=i-1)
{
System.out.println("The new Co-ordinator is Selected."+i);
pc=i;
break;
}

}
for(int i=1;i<=n;i++)
{
if(i!=pc)
System.out.println("The new Co-ordinator "+pc+" is Send the ok Messege to ."+i);
}
}
}




Port scanning


import java.net.*;
import java.io.IOException;
import javax.swing.*;
public class Trial
{

public static void main(String args[])
{
  try
  {
   String str=JOptionPane.showInputDialog("Enter the host name");

   InetAddress ia=InetAddress.getByName(str);
  System.out.println("ip address is"+ia);
   for(int i=80;i<130;i++)
   {
    try
      {
       Socket s=new Socket(ia,i);
       System.out.println("port "+i+" is open");
       s.close();
       }
      catch(IOException e)
     {
     System.out.println("port "+i+"is off");
    }
   }
  }
catch(UnknownHostException e)
{ }
 }
}





Lamport


import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class common extends Panel implements Runnable
{
public Label l1,l2,l3,l4,l5;
public TextField t1,t2,t3;
public Button b1;
public int tick;
public java.awt.List list1;
public Thread th;
boolean flag;

public common(String str)
{
flag=true;
th=new Thread(this);
l1=new Label("time");
l2=new Label("message");
l3=new Label("to:");
l4=new Label("received");
l5=new Label(str);

t1=new TextField("0");
t2=new TextField();
t3=new TextField();

b1=new Button ("send");

list1=new java.awt.List();
this.setVisible(true);
this.setLayout(null);

l5.setBounds(100,15,100,30);
l1.setBounds(50,70,100,20);
l2.setBounds(50,120,100,20);
l3.setBounds(50,180,100,20);
l4.setBounds(50,240,100,20);

t1.setBounds(150,70,100,20);
t2.setBounds(150,120,100,20);
t3.setBounds(150,240,100,20);

list1.setBounds(150,180,100,40);

b1.setBounds(150,350,100,20);

t1.setEditable(false);

this.add(l5);
this.add(l1);
this.add(t1);
this.add(l2);
this.add(t2);
this.add(l3);
this.add(list1);
this.add(l4);
this.add(t3);
this.add(b1);
this.setSize(300,400);
this.setBackground(Color.GRAY);
}

public void run()
{
try{
while(flag)
{
t1.setText((Integer.parseInt(t1.getText()))+this.tick+"");
Thread.sleep(500);
}

}catch(Exception e) {;}

}

}
public class lamport implements ActionListener
{
Frame f;
common c1,c2,c3;
lamport()
{
f=new Frame(" lamport ");
c1=new common("Process: A");
c2=new common("Process: B");
c3=new common("Process: C");

f.setLayout(null);

c1.setBounds(30,100,300,400);
c2.setBounds(350,100,300,400);
c3.setBounds(670,100,300,400);

f.add(c1);
f.add(c2);
f.add(c3);

c1.b1.addActionListener(this);
c2.b1.addActionListener(this);
c3.b1.addActionListener(this);

c1.list1.add("Process B");
c1.list1.add("Process c");


c2.list1.add("Process A");
c2.list1.add("Process C");

c3.list1.add("Process A");
c3.list1.add("Process B");

c1.tick=6;
c2.tick=8;
c3.tick=10;

f.setVisible(true);
f.setSize(1000,600);
f.setBackground(Color.BLACK);

c1.th.start();
c2.th.start();
c3.th.start();

       }

     public static void main(String arg[])
{
lamport lp=new lamport();
}

public void actionPerformed(ActionEvent ae)
{
String temp1,temp2,temp3;
if(ae.getActionCommand().equals("send"))
{
if((""+ae.getSource()).substring(16,23).equals("button0"))
{
temp1=c1.t1.getText();
temp2=c1.t2.getText();

if(c1.list1.getSelectedItem().equals("Process B"))
{
temp3=c2.t1.getText();
c2.t3.setText(temp2+","+temp1);
}
      else if(c1.list1.getSelectedItem().equals("Process C"))
{
temp3=c3.t2.getText();
c3.t3.setText(temp2+","+temp1);
}
}

if((""+ae.getSource()).substring(16,23).equals("button1"))
{
temp1=c2.t1.getText();
temp2=c2.t2.getText();

if(c2.list1.getSelectedItem().equals("Process A"))
{
temp3=c1.t1.getText();

if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c1.t1.setText(temp1);
c1.t3.setText(temp2+","+temp1);
}

else if(c2.list1.getSelectedItem().equals("Process C"))
{

temp3=c3.t1.getText();

c3.t3.setText(temp2+","+temp1);
}

}

if((""+ae.getSource()).substring(16,23).equals("button2"))
{
temp1=c3.t1.getText();
temp2=c3.t2.getText();



if(c3.list1.getSelectedItem().equals("Process B"))
{
temp3=c2.t1.getText();

if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c2.t1.setText(temp1);
c2.t3.setText(temp2+","+temp1);
}

else if(c3.list1.getSelectedItem().equals("Process A"))
{

temp3=c1.t1.getText();

if(Integer.parseInt(temp1)>Integer.parseInt(temp3))
c1.t1.setText(temp1);
c1.t3.setText(temp2+","+temp1);
}
}
}
   }
}

Client



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

class client
{

public static void main(String args[])throws Exception
{
String str;
try
{
System.out.println("Client Side..........");
Socket socket=new Socket("localHost",5000);
BufferedReader in,inline;
PrintWriter out;
while(true)
{
inline=new BufferedReader(new InputStreamReader(System.in));
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out= new PrintWriter(socket.getOutputStream(),true);
System.out.println("Enter message:-");
str=inline.readLine();
out.println(str);
if(str.equals("quit"))
break;
else
{
str=in.readLine();
System.out.println(str);
}

}
}
catch(Exception e)
{
System.out.println("No EchoServer Found.");
}
}
}

--------------------------------------------------------------------------------------------------

Echo Server




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

class EchoServer implements Runnable
{

Socket ins;
int ci;
EchoServer(Socket ins,int i)
{
this.ins=ins;
this.ci=i;
}

public static void main(String args[])
{
try
{

ServerSocket socket=new ServerSocket(5000);
System.out.println("EchoServer Started......Waiting for Clients");
int i = 1;

Socket sock;
while(true)
{
sock=socket.accept();
Runnable r = new EchoServer(sock,i);
Thread t = new Thread(r);
t.start();
System.out.println("Client" + i+" is Connected.");
i++;

}
}
catch(Exception e)
{
//System.out.println("Cannot start EchoServer.");
}
}

public void run()
{
String str;
try
{
BufferedReader in,inline;
PrintWriter out;
inline=new BufferedReader(new InputStreamReader(System.in));
in=new BufferedReader(new InputStreamReader(ins.getInputStream()));
out= new PrintWriter(ins.getOutputStream(),true);
while(true)
{
str=in.readLine();
System.out.println("From Client"+ci+":"+str);

//str=inline.readLine();
out.println("Echo from Server:"+str);

if(str.equals("quit"))
break;
}
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println("Interrupted");
}
System.out.println("Client "+ci+" is Disconnected.");
}

}




Calculator


Calculator.java
 public interface Calculator extends java.rmi.Remote
{

public long add(long a,long b) throws java.rmi.RemoteException;
public long sub(long a,long b) throws java.rmi.RemoteException;
public long mul(long a,long b) throws java.rmi.RemoteException;
public long div(long a,long b) throws java.rmi.RemoteException;

}
----------------------------------------------------------------------------------------------------
CalulatorClient.java


import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient
{
 public static void main(String args[])
 {
  long num1=Integer.parseInt(args[0]);
  long num2=Integer.parseInt(args[1]);
  try
    {
     Calculator c=(Calculator)
     Naming.lookup("rmi://localhost/CalculatorService");
     System.out.println("The Addition of "+num1+"and"+num2+"is:"+c.add(num1,num2));
     System.out.println("The Subtraction of "+num1+"and"+num2+"is:"+c.sub(num1,num2));
     System.out.println("The Multiplication of "+num1+"and"+num2+"is:"+c.mul(num1,num2));
     System.out.println("The Division of "+num1+"and"+num2+"is:"+c.div(num1,num2));
    }
   catch(MalformedURLException m)
  {
   System.out.println("MaluformedURLException");
  }
  catch(RemoteException re)
  {
   System.out.println("RemoteException");
  }
  catch(NotBoundException nbe)
  {
   System.out.println("NotBoundExceptionException");
  }
  catch(java.lang.ArithmeticException ae)
  {
   System.out.println("java.lang.ArithmeticException");
   }

  }
}
---------------------------------------------------------------------------------------------------------
CalculatorImpl.java


public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator
{

 public CalculatorImpl() throws java.rmi.RemoteException
 {
  super();
 }
 public long add(long a,long b) throws java.rmi.RemoteException
 {
  return a+b;
 }
 public long sub(long a,long b) throws java.rmi.RemoteException
 {
  return a-b;
 }
 public long mul(long a,long b) throws java.rmi.RemoteException
 {
  return a*b;
 }
 public long div(long a,long b) throws java.rmi.RemoteException
 {
  return a/b;
 }


}



---------------------------------------------------------------------------------------------------
CalculatorServer.java

import java.rmi.Naming;
public class CalculatorServer
{
 public CalculatorServer()
 {
  try
   {
    Calculator c= new CalculatorImpl();
    Naming.rebind("rmi://localhost:1099/CalculatorService",c);  

   }
   catch(Exception e)
   {
   System.out.println("Trouble:"+e);
   }
 }
 public static void main(String args[])
 {
  new CalculatorServer();
 }
}





bully



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Bully {
static int n,pc,in;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter the Number of Clients");
n=Integer.parseInt(br.readLine());
System.out.println("Enter the Pri-Coordinator");
pc=Integer.parseInt(br.readLine());
System.out.println("Enter the Introducer");
in=Integer.parseInt(br.readLine());
for(int i=in;i<n;)
{
System.out.println(in+"Send the Election messege to "+ ++i);
}
for(int i=n;i>in;i--)
{
if(i!=pc)
System.out.println(i +"Send the Ok messege to "+ in);
}
for(int i=in+1;i<=n;i++)
{
if(i!=pc)
System.out.println(i +"Send the Election messege to "+ pc);
}
for(int i=n;i>=1;i--)
{
if(i!=pc && i>=i-1)
{
System.out.println("The new Co-ordinator is Selected."+i);
pc=i;
break;
}

}
for(int i=1;i<=n;i++)
{
if(i!=pc)
System.out.println("The new Co-ordinator "+pc+" is Send the ok Messege to ."+i);
}
}
}