Square Root Table Using Applet Monday, May 5 2008 

SIMPLE APPLET

//SqrtList.java

import java.awt.*;

public class SqrtList extends java.applet.Applet {
    public void paint(Graphics g) {
	g.setFont(new Font("TimesRoman", Font.PLAIN, 12));
	for (float i = 1; i <= 10; i++)
	    g.drawString("sqrt(" + i + ") = " + Math.sqrt(i), 0, 
		(int)i * 16 - 3);
    }
}

This is the code that specifies the Java code to run:

<APPLET CODEBASE=”java” CODE=”SqrtList” WIDTH=160 HEIGHT=162> <EM>You need a Java-aware browser</EM> </APPLET>

The <APPLET> tag specifies the class to load (the CODE= field), URL information (the CODEBASE= field) and the size of the region the applet will own. Notice that Java doesn’t exactly integrate with the rest of the page. Within that region of the page Java is king: it decides background color and fonts and does all the mouse and keyboard handling.

Parameters to the applet are placed in <PARAM> tags between the <APPLET> and </APPLET> tags. Anything else between these tags is ignored. It’s common to include some information here for display by browsers that don’t know about Java, since they’ll ignore the <APPLET> and <PARAM> tags and display whatever else they find there.

OUTPUT

sqrt(1) = 1
sqrt(2) = 1.41421
sqrt(3) = 1.73205
sqrt(4) = 2
sqrt(5) = 2.23607
sqrt(6) = 2.44949
sqrt(7) = 2.64575
sqrt(8) = 2.82843
sqrt(9) = 3
sqrt(10) = 3.16228
Advertisements

TCP CLIENT/SERVER FOR ARITHMETIC OPERATION Sunday, May 4 2008 

TCP CLIENT

//arithtcpclient.java

import java.io.*;

import java.net.*;

public class arithtcpclient

{

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

{

System.out.println();

System.out.println(“ARITHMETIC CLIENT”);

System.out.println(“*****************”);

System.out.println(“Enter the host name to connect”);

String str;

DataInputStream inp=new DataInputStream(System.in);

str=inp.readLine();

Socket clientsoc = new Socket(str, 9);

System.out.println(“Enter the inputs”);

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

String userinput;

try

{

while (true)

{

do

{

userinput = stdin.readLine();

out.println(userinput);

}while(!userinput.equals(“.”));

System.out.println(“Sever Says : ” + in.readLine());

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

TCP SERVER

//arithtcpserver

import java.io.*;

import java.net.*;

public class arithtcpserver

{

public static void main(String arg[]) throws Exception

{

System.out.println();

System.out.println(“ARITHMETIC SERVER”);

System.out.println(“*****************”);

System.out.println(“Server is ready to accept inputs…”);

ServerSocket serversoc=new ServerSocket(9);

Socket clientsoc = serversoc.accept();

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

String inputline;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

try

{

while (true)

{

String s,op=””,st;

int i=0,c=0;

int[] a=new int[2];

while(true)

{

s=in.readLine();

if(s.equals(“+”) || s.equals(“-“) || s.equals(“*”) || s.equals(“/”))

op=s;

else if(s.equals(“.”))

break;

else

{

a[i]=Integer.parseInt(s);

i++;

}

}

if(op.equals(“+”))

c=a[0]+a[1];

else if(op.equals(“-“))

c=a[0]-a[1];

else if(op.equals(“*”))

c=a[0]*a[1];

else if(op.equals(“/”))

c=a[0]/a[1];

s=Integer.toString(c);

out.println(s);

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

Sample Output:

SERVER SIDE

*************

C:\jdk1.5\bin>javac arithtcpserver.java

C:\jdk1.5\bin>java arithtcpserver

ARITHMETIC SERVER

*********************

Server is ready to accept inputs…

CLIENT SIDE

************

C:\JDK1.5\bin>javac arithtcpclient.java

C:\JDK1.5\bin>java arithtcpclient

ARITHMETIC CLIENT

********************

Enter the host name to connect

p4-221

Enter the inputs

8

+

3

Sever Says : 11

9

11

Sever Says : -2

7

*

9

Sever Says : 63

12

/

3

Sever Says : 4

CLIENT/SERVER COMMUNICATION USING TCP SOCKETS Sunday, May 4 2008 

TCP CLIENT

//tcpclient.java

import java.io.*;

import java.net.*;

public class tcpclient

{

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

{

System.out.println(“TCP CLIENT”);

System.out.println(“Enter the host name to connect”);

DataInputStream inp=new DataInputStream(System.in);

String str=inp.readLine();

Socket clientsoc = new Socket(str, 9);

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

String userinput;

try

{

while (true)

{

System.out.println(“Sever Says : ” + in.readLine());

userinput = stdin.readLine();

out.println(userinput);

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

TCP SERVER

//tcpserver.java

import java.io.*;

import java.net.*;

public class tcpserver

{

public static void main(String a[]) throws Exception

{

System.out.println(“TCP SERVER”);

System.out.println(“Server is ready to connect…”);

ServerSocket serversoc=new ServerSocket(9);

Socket clientsoc = serversoc.accept();

PrintWriter out = new PrintWriter(clientsoc.getOutputStream(), true);

BufferedReader in = new BufferedReader(new

InputStreamReader(clientsoc.getInputStream()));

String inputline;

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

try

{

while (true)

{

inputline = stdin.readLine();

out.println(inputline);

System.out.println(“Client Says : “+in.readLine());

}

}

catch(Exception e)

{

System.exit(0);

}

}

}

Sample Output:

CLIENT-SERVER CHATTING USING TCP

*************************************

TCP SERVER

************

Server is ready to connect…

hello

Client Says : hello

How are you

Client Says : I’m doing programs

What programs

Client Says : networking

ok.Go ahead

Client Says : ok.Bye

Bye

TCP CLIENT

***********

Enter the host name to connect

p4-221

Sever Says : hello

hello

Sever Says : how are you

I’m doing programs

Sever Says : What programs

networking

Sever Says : ok.Go ahead

ok.Bye

Sever Says : Bye

Addition Server Thursday, May 1 2008 

//AddServer

import java.rmi.*;

public interface AddServer extends Remote

{

double add(double d1,double d2) throws RemoteException;

}

//Add Server Imp

import java.rmi.*;

import java.rmi.server.*;

public class AddServerImp extends UnicastRemoteObject implements AddServer

{

public AddServerImp() throws RemoteException

{

}

public double add(double d1,double d2) throws RemoteException

{

return d1+d2;

}

}

//Adds

import java.net.*;

import java.rmi.*;

public class Adds

{

public static void main(String args[])

{

try

{

AddServerImp addServerImp= new AddServerImp();

Naming.rebind(“Adds”,addServerImp);

}

catch(Exception e)

{

System.out.println(“Exception:”+e);

}

}

}

//AddClient

import java.rmi.*;

public class AddClient

{

public static void main(String args[])

{

try

{

String addServerURL=”rmi://localhost”+”/Adds”;

AddServer addserver=(AddServer)Naming.lookup(addServerURL);

System.out.println(“First number is:”+args[0]);

double d1=Double.valueOf(args[0]).doubleValue();

System.out.println(“Second number is:”+args[1]);

double d2=Double.valueOf(args[1]).doubleValue();

System.out.println(“The sum is:”+addserver.add(d1,d2));

}

catch(Exception e)

{

System.out.println(“Exception:”+e);

}

}

}