Lab Manual

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 86

1)a)Write a program to implement TCP Connection in java.

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

1)b)Write a program to implement UDP Connection in java.-------------------------

2)a)Write a program to implement FTP in java.-------------------------------------------

2)b)FTPClientGUIModified java.-------------------------------------------------------------------------

3)a)Write a program to implement chat server and multicast peer in java-------

3)b)Chat-gui-19.10.10----------------------------------------------------------------------------

4)a)Write a program to demonstrate RMI in java.---------------------------------------

4)b)Write a program to perform arithmetic operations using RMI in java.-------

5)a)Write a program to implement DNS protocol using RMI in java.--------------

5)b)Write a program to implement mini DNS protocol using RPC in java.-----------

6)a)Write a program to implement Query Processing in java.----------------------------

6)b)Write a program it implement 2PC in java.-------------------------------------------

7)Study of NFS.-------------------------------------------------------------------------------------
1)a)Write a program to implement TCP Connection in java.
TCPClient.java

import java.net.*;

import java.io.*;

class TcpClient

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

Socket cs=new Socket("localhost",1600);

System.out.println("connecting to server!");

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

System.out.println("Enter u r name");

String n=br.readLine();

//SENDING DATA TO SERVER

OutputStream os=cs.getOutputStream();

os.write(n.getBytes());

//READING DATA FROM SERVER

InputStream is=cs.getInputStream();

byte data[]=new byte[50];

is.read(data);
//PRINTING MESS AT CLIENT CONSLOE

String mfs=new String(data);

mfs=mfs.trim();

System.out.println(mfs);

}
TCPServer.java

import java.io.*;

import java.net.*;

class TcpServer

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

ServerSocket ss=new ServerSocket(1600);

while (true)

System.out.println("server is ready!");

Socket ls=ss.accept();

//READING DATA FROM CLIENT

InputStream is=ls.getInputStream();

byte data[]=new byte[50];

is.read(data);

String mfc=new String(data);

mfc=mfc.trim();

String mfs="hello:"+mfc;

//SENDING MESS TO CLIENT

OutputStream os=ls.getOutputStream();

os.write(mfs.getBytes());

}
}

1)b)Write a program to implement UDP Connection in java.


DgClient.java

import java.net.*;

class DgClient

public static int sp=997;

public static int cp=998;

public static int bs=1024;

public static DatagramSocket ds;

public static byte buff[]=new byte[bs];

public static void client() throws Exception

while (true)

DatagramPacket p=new DatagramPacket(buff,buff.length);

ds.receive(p);

System.out.println(new String(p.getData(),0,p.getLength()));

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

{
ds=new DatagramSocket(cp);

client();

}
DatagrmApp.java

import java.net.*;

class DatagrmServerApp

public static int sp=997;

public static int cp=998;

public static int bs=1024;

public static DatagramSocket ds;

public static byte buff[]=new byte[bs];

public static void server() throws Exception

int pos=0;

while (true)

int c=System.in.read();

switch (c)

case -1:System.out.println("server quits!");return;

case '\r':break;

case '\n': ds.send(new


DatagramPacket(buff,pos,InetAddress.getLocalHost(),cp));

pos=0;

default: buff[pos++]=(byte)c;
}

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

ds=new DatagramSocket(sp);

server();

}
2)a)Write a program to implement FTP in java.
FTPClient.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import java.io.*;

class One extends JFrame implements ActionListener

/* ctrl space */

public JButton b,b1;

public JLabel l;

public JLabel l1,lmsg1,lmsg2;

One()

b=new JButton("Upload");

l=new JLabel("Uplaod a file : ");

lmsg1=new JLabel("");

b1=new JButton("Download");

l1=new JLabel("Downlaod a file");

lmsg2=new JLabel("");
setLayout(new GridLayout(2,3,10,10));

add(l);add(b);add(lmsg1);add(l1);add(b1);add(lmsg2);

b.addActionListener(this);

b1.addActionListener(this);

setVisible(true);

setSize(600,500);

public void actionPerformed(ActionEvent e)

// TODO Auto-generated method stub

try {

/* String s=e.getActionCommand();

if(s.equals("Upload"))*/

if (b.getModel().isArmed())

Socket s=new Socket("localhost",1010);

System.out.println("Client connected to server");

JFileChooser j=new JFileChooser();

int val;

val=j.showOpenDialog(One.this);

String filename=j.getSelectedFile().getName();

String path=j.getSelectedFile().getPath();
PrintStream out=new PrintStream(s.getOutputStream());

out.println("Upload");

out.println(filename);

FileInputStream fis=new FileInputStream(path);

nt n=fis.read();

while (n!=-1)

out.print((char)n);n=fis.read();

fis.close(); out.close();lmsg1.setText(filename+"is uploaded");

//s.close();

repaint();

if (b1.getModel().isArmed())

Socket s=new Socket("localhost",1010);

System.out.println("Client connected to server");

String remoteadd=s.getRemoteSocketAddress().toString();

System.out.println(remoteadd);

JFileChooser j1=new JFileChooser(remoteadd);

int val;

val=j1.showOpenDialog(One.this);

String filename=j1.getSelectedFile().getName();
String filepath=j1.getSelectedFile().getPath();

System.out.println("File name:"+filename);

PrintStream out=new PrintStream(s.getOutputStream());

out.println("Download");

out.println(filepath);

FileOutputStream fout=new FileOutputStream(filename);

DataInputStream fromserver=new DataInputStream(s.getInputStream());

int ch;

while ((ch=fromserver.read())!=-1)

fout.write((char) ch);

fout.close();//s.close();

lmsg2.setText(filename+"is downlaoded");

repaint();

catch (Exception ee)

// TODO: handle exception

System.out.println(ee);

}
}

public class FTPClient

public static void main(String[] args)

new One();

}
FTPServer.java

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.PrintStream;

import java.net.ServerSocket;

import java.net.Socket;

public class FTPServer {

public static void main(String[] args)

try {

while (true)

ServerSocket ss=new ServerSocket(1010);

Socket sl=ss.accept();

System.out.println("Server scoket is created....");

System.out.println(" test1");

DataInputStream fromserver=new DataInputStream(sl.getInputStream());

System.out.println(" test2");

String option=fromserver.readLine();
if (option.equalsIgnoreCase("upload"))

System.out.println("upload test");

String filefromclient=fromserver.readLine();

File clientfile=new File(filefromclient);

FileOutputStream fout=new FileOutputStream(clientfile);

int ch;

while ((ch=fromserver.read())!=-1)

fout.write((char)ch);

fout.close();

if (option.equalsIgnoreCase("download"))

System.out.println("download test");

String filefromclient=fromserver.readLine();

File clientfile=new File(filefromclient);

FileInputStream fis=new FileInputStream(clientfile);

PrintStream out=new PrintStream(sl.getOutputStream());

int n=fis.read();

while (n!=-1)
{

out.print((char)n);

n=fis.read();

fis.close();

out.close();

} //while

catch (Exception e)

System.out.println(e);

// TODO: handle exception

}
2)b)FTPClientGUIModified.java
import javax.swing.*;import java.awt.*;

import java.awt.event.*;import java.net.*;

import java.io.*;

class One extends JFrame implements ActionListener

public JButton b,b1; public JLabel l;public JLabel l1,lmsg1,lmsg2;

One()

b=new JButton("Upload");

l=new JLabel("Upload a file : ");

lmsg1=new JLabel("");

b1=new JButton("Download");

l1=new JLabel("Download a file");

lmsg2=new JLabel("");

b.setBounds(100, 20, 100, 25);

b1.setBounds(100, 75, 100, 25);

l.setBounds(5, 20, 100, 25);

l1.setBounds(5, 75, 100, 25);

setLayout(null);
add(l);add(b);add(lmsg1);add(l1);add(b1);add(lmsg2);

b.addActionListener(this);b1.addActionListener(this);

setVisible(true);

setSize(300,150);

//setResizable(false); setDefaultCloseOperation(3);

public void actionPerformed(ActionEvent e)

String s=e.getActionCommand();

if(s.equals("Upload"))

JFileChooser j=new JFileChooser();

int val;

val=j.showOpenDialog(One.this);

lmsg1.setText("is uploaded");

//s.close();

repaint();

if (b1.getModel().isArmed())

lmsg2.setText( "is downlaoded");

repaint();

}
}

public class FTPClientGUIModified

public static void main(String[] args)

new One();

}
3)a)Write a program to implement chat server and multicast peer in java
CCLogin.java

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import java.awt.GridLayout;

public class CCLogin implements ActionListener

JFrame frame1; JTextField tf,tf1; JButton button;

JLabel heading; JLabel label,label1;

public static void main(String[] paramArrayOfString)

new CCLogin();

}
public CCLogin()

this.frame1 = new JFrame("Login Page");

this.tf = new JTextField(10);

this.button = new JButton("Login");

this.heading = new JLabel("Chat Server");

this.heading.setFont(new Font("Impact", 1, 40));

this.label = new JLabel("Enter you Login Name");

this.label.setFont(new Font("Serif", 0, 24));

JPanel localJPanel = new JPanel();

this.button.addActionListener(this);

localJPanel.add(this.heading); localJPanel.add(this.label);

localJPanel.add(this.tf);

localJPanel.add(this.button);

this.heading.setBounds(30, 20, 280, 50);

this.label.setBounds(20, 100, 250, 60);

this.tf.setBounds(50, 150, 150, 30);

this.button.setBounds(70, 190, 90, 30);

this.frame1.add(localJPanel);

localJPanel.setLayout(null);

this.frame1.setSize(300,300);

this.frame1.setVisible(true);
this.frame1.setDefaultCloseOperation(3);

public void actionPerformed(ActionEvent paramActionEvent)

String str = "";

try

str = this.tf.getText();

this.frame1.dispose();

Client1 c1= new Client1(str);

c1.main(null);

catch(Exception localIOException)

}
ChatMultiServer.java

import java.net.*;

import java.io.*;

class A implements Runnable

Thread t;

Socket s;

A(Socket x)

s=x;

t=new Thread(this);

t.start();

public void run()

try

/* Reading data from client */

InputStream is=s.getInputStream();

byte data[]=new byte[50];

is.read(data);

String mfc=new String(data);


mfc=mfc.trim();

System.out.println(mfc);

/* Sending message to the server */

//System.out.println("Hi"+name+"u can start chating");

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

String n=br.readLine();

OutputStream os=s.getOutputStream();

os.write(n.getBytes());

catch(Exception e)

e.printStackTrace();

class ChatMultiServer

static int c=0;

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

System.out.println("ServerSocket is creating");

ServerSocket ss=new ServerSocket(1010);

System.out.println("ServerSocket is created");
System.out.println("waiting for the client from the client");

while(true)

Socket s=ss.accept();

new A(s);

}
Client1.java

import java.net.*;

import java.io.*;

class Client1

static String name="";

public Client1(String n)

name=n;

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

System.out.println("connecting to server");

System.out.println("client1 connected to server");

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

/* Sending message to the server */

System.out.println("Hi\t"+name+" u can start chating");

while(true)

Socket s=new Socket("localhost",1010);

String n=br.readLine();
OutputStream os=s.getOutputStream();

os.write(n.getBytes());

/* Reading data from client */

InputStream is=s.getInputStream();

byte data[]=new byte[50];

is.read(data);

String mfc=new String(data);

mfc=mfc.trim();

System.out.println(mfc);

}
MulticastPeer.java

import java.net.*;

import java.io.*;

public class MulticastPeer

public static void main(String args[])

// args give message contents and destination multicast group (e.g. "228.5.6.7")

MulticastSocket s =null;

try

InetAddress group = InetAddress.getByName(args[1]);

s = new MulticastSocket(6789); s.joinGroup(group);

byte [] m = args[0].getBytes();

DatagramPacket messageOut = new DatagramPacket(m, m.length, group, 6789);

s.send(messageOut); byte[] buffer = new byte[1000];

for(int i=0; i< 3;i++) {

// get messages from others in group

DatagramPacket messageIn = new DatagramPacket(buffer, buffer.length);

s.receive(messageIn);

System.out.println("Received:" + new String(messageIn.getData())); }


s.leaveGroup(group);

catch (SocketException e)

System.out.println("Socket: " + e.getMessage());

catch (IOException e)

System.out.println("IO: " + e.getMessage());

finally

if(s != null) s.close();

}
3)b)Chat-gui-19.10.10
Client1.java

import java.net.*;

import java.io.*;

import javax.swing.*;

import java.awt.event.*;

class Client1 extends JFrame implements ActionListener

JButton button;

JTextArea t1;

JTextArea t2;

String mess=" ";

Socket s;

JScrollPane scrollPane1;

JScrollPane scrollPane2;

public Client1()

try

button=new JButton("SEND");

t1=new JTextArea(10,20);
t2=new JTextArea(10,20);

scrollPane1 = new JScrollPane(t2);

scrollPane2= new JScrollPane(t1);

scrollPane2.setBounds(20,250,600,50);

button.setBounds(20,300,100,50);

scrollPane1.setBounds(20,20,600,200);

setLayout(null);

add(scrollPane2);

add(button);

add(scrollPane1);

t2.setEditable(false);

button.addActionListener(this);

setSize(700,400);

setVisible(true);

setLayout(null);

System.out.println("connecting to server");

System.out.println("client connected to server");

System.out.println("Hi you can start chatting");

s= new Socket("localhost",1010);

while(true)

InputStream is=s.getInputStream();

byte data[]=new byte[50];

is.read(data);
String mfc=new String(data);

mfc=mfc.trim();

t2.append("My Pal :"+mfc+"\n");

catch(Exception e1)

e1.printStackTrace();

public void actionPerformed(ActionEvent e)

try

String n=t1.getText();

OutputStream os=s.getOutputStream();

os.write(n.getBytes());

t2.append("Me :"+n+"\n");

t1.setText(" ");

catch(Exception e2)

e2.printStackTrace();

}
}

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

Client1 c = new Client1();

}
ChatMultiServer.java

import java.net.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import java.awt.event.*;

class A extends JFrame implements Runnable , ActionListener

Thread t;

Socket s;

JButton button;

JTextArea t1;

JTextArea t2;

String mess=" ";

JScrollPane scrollPane1;

JScrollPane scrollPane2;

A(Socket x)

s=x;

t=new Thread(this);

t.start();

public void run()


{

try

button=new JButton("SEND");

t1=new JTextArea(10,20);

t2=new JTextArea(10,20);

scrollPane1 = new JScrollPane(t2);

scrollPane2=new JScrollPane(t1);

scrollPane1.setBounds(20,20,600,200);

scrollPane2.setBounds(20,250,600,50);

button.setBounds(20,300,100,50);

setLayout(null);

add(scrollPane1);add(scrollPane2);add(button);

t2.setEditable(false);

button.addActionListener(this);

setSize(700,400);

setVisible(true);

System.out.println("Hi you can start chatting");

while(true)

InputStream is=s.getInputStream();

byte data[]=new byte[50];

is.read(data);

String mfc=new String(data);


mfc=mfc.trim();

t2.append("My Friend: "+mfc+"\n");

catch (Exception e)

e.printStackTrace();

public void actionPerformed(ActionEvent e1)

try

mess=t1.getText();

OutputStream os=s.getOutputStream();

os.write(mess.getBytes());

String text = t1.getText();

t2.append("Me : "+text + "\n");

t1.selectAll();

t1.setText(" ");

catch (Exception e)

e.printStackTrace();
}

class ChatMultiServer

static int c=0;

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

System.out.println("server socket is creating");

ServerSocket ss=new ServerSocket(1010);

System.out.println("server socket is created");

System.out.println("waiting for client");

while(true)

Socket s=ss.accept();

new A(s);

}
4)a)Write a program to demonstrate RMI in java.

Hello.java

public interface Hello extends java.rmi.Remote

String sayHello(String s) throws java.rmi.RemoteException;

}
HelloImpl.java

import java.rmi.server.*;

public class HelloImpl extends UnicastRemoteObject implements Hello

private int i;

public HelloImpl() throws Exception

super();

public String sayHello(String s)

i++;

return "Hello " +s+ " yours is " +i+ " request";

}
HelloClient.java

import java.rmi.*;

class HelloClient

public static void main(String[] args) throws

Exception

Remote ref=Naming.lookup("hello");

Hello h = (Hello)ref;

String result = h.sayHello(args[0]);

System.out.println(result);

//rmi://135.135.5.146:1099/
HelloServer.java

import java.rmi.*;

import java.rmi.server.*;

class HelloServer {

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

HelloImpl remoteObj = new HelloImpl();

System.out.println("HelloImpl Created");

/* UnicastRemoteObject.exportObject(remoteObj);

System.out.println("HelloImpl Exported"); */

java.rmi.registry.LocateRegistry.createRegistry(1099);

Naming.rebind("hello", remoteObj);

System.out.println("HelloImpl Binded");

}
4)b)Write a program to perform arithmetic operations using RMI in java.
arithmeticIntf.java

import java.rmi.*;

interface ArithmeticIntf extends Remote

public double add(double a,double a1) throws RemoteException;

public double sub(double b,double b1) throws RemoteException;

public double mul(double c,double c1) throws RemoteException;

public double div(double d,double d1) throws RemoteException;

}
arithmeticImpl.java

import java.rmi.*;

import java.rmi.server.*;

public class ArithmeticImpl extends UnicastRemoteObject implements ArithmeticIntf

public ArithmeticImpl() throws RemoteException {}

public double add(double a,double a1) throws RemoteException

double res = a + a1;

return res;

public double sub(double b,double b1) throws RemoteException

double res = b - b1;

return res;

public double mul(double c,double c1) throws RemoteException

{
double res = c * c1;

return res;

public double div(double d,double d1) throws RemoteException

double res = d / d1;

return res;

}
Arithmetic Client.java

//package rmipack;

import java.rmi.*;

class ArithmeticClient

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

/* we are designing an URL within which we can keep three options.

1. rmi service

2. ip address of the system in which your rmiserver is existing.

3. remote interface name */

String url = "rmi://"+args[0]+"/rmiintf";

/*lookup is a method which accepts the url created in order to search the rmi server and

returns a reference which will be type casted as of remote interface type created by you
and keep the reference within a reference variable of rmiintf. With the help of this
reference we have to call the methods present within the rmi server.

*/

ArithmeticIntf ri = (ArithmeticIntf)Naming.lookup(url);
double d = Double.parseDouble(args[1]);

double d1 = Double.parseDouble(args[2]);

double res = ri.add(d,d1);

double res1 = ri.sub(d,d1);

double res2 = ri.mul(d,d1);

double res3 = ri.div(d,d1);

System.out.println("Addition is : "+res);

System.out.println("Subtraction is : "+res1);

System.out.println("Multiplication is : "+res2);

System.out.println("Division is : "+res3);

}
ArithmeticServer.java

//This program written to specify a name to the server so that the client can access it

import java.rmi.*;

import java.rmi.registry.*;

import java.rmi.server.*;

public class ArithmeticServer

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

ArithmeticImpl ob1 = new ArithmeticImpl();

java.rmi.registry.LocateRegistry.createRegistry(1099);

/*

rebind is a method in the Naming class which accepts

two parameters. One is the remote interface name created

by you. Second one is the object of the Server class

in which all the methods has been implemented.

This parameter is accepted to provide a name to the

server so that the client can recognize it.

*/

Naming.rebind("rmiintf",ob1);

}
}

5)a)Write a program to implement DNS protocol usig RMI in java.


NameList.text

#NameSpace

#Wed Nov 10 11:14:03 IST 2010


dnsclient.java

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.net.*;

import java.awt.*;

import java.rmi.*;

class dnsclient extends JFrame implements ActionListener

JButton b1,b2,b3,b4,b5;

JPanel p1,p2;

JLabel l1,l2;

JTextField t1,t2;

DataOutputStream output;

DataInputStream input;

dnsclient()

b1=new JButton("addhost");

b2=new JButton("lookup");

b3=new JButton("Remove");

b4=new JButton("Refresh");

b5=new JButton("Close");
p1=new JPanel();

p2=new JPanel();

l1=new JLabel("Host");

l2=new JLabel("IP");

t1=new JTextField("",20);

t2=new JTextField("",20);

p1.setLayout(new FlowLayout());

p1.add(l1);

p1.add(t1);

p1.add(l2);

p1.add(t2);

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(b4);

p2.add(b5);

add(p1,"North");

add(p2,"South");

setSize(300,300);

setVisible(true);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);
}

public void actionPerformed(ActionEvent e)

DNS d=null;

try

Remote ref=Naming.lookup("dns");

d=(DNS)ref;

catch (Exception e1)

e1.printStackTrace();

String s=e.getActionCommand();

try

if(s.equals("addhost"))

Boolean b=d.addhost(t1.getText(),t2.getText());

if(b==true)

t2.setText("Reg");

else
{

t2.setText("Not reg");

if(s.equals("lookup"))

String s1=d.lookup(t1.getText());

t2.setText(s1);

if(s1==null)

t2.setText("host name not found");

else

t2.setText("the ip address is "+s1);

if(s.equals("Remove"))

String s2=d.remove(t1.getText());

t2.setText(s2);

if(s2==null)

t2.setText("host name not found");

else

t2.setText("the ip address"+s2+"is removed");

if(s.equals("Refresh"))

t1.setText("");
t2.setText("");

if(s.equals("Close"))

System.exit(0);

catch (Exception e1)

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

dnsclient g=new dnsclient();

}
DNS.java

public interface DNS extends java.rmi.Remote

String lookup(String s) throws java.rmi.RemoteException;

boolean addhost(String s1,String s2) throws java.rmi.RemoteException;

String remove(String s) throws java.rmi.RemoteException;

}
DNSServer.java

import java.util.*;

import java.io.*;

import java.net.*;

import java.awt.*;

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

class DNSServer extends UnicastRemoteObject implements DNS

String s;

Properties Clients=new Properties();

FileInputStream fin=null;

FileOutputStream fout=null;

DNSServer() throws RemoteException

super();

try

fin=new FileInputStream("NameList.txt");
if(fin!=null){Clients.load(fin);

fin.close();

catch (Exception e)

e.printStackTrace();

public boolean addhost(String name,String ip)

if(Clients.get(name)!=null)

return false;

Clients.put(name,ip);

try

fout=new FileOutputStream("NameList.txt");

Clients.store(fout,"NameSpace");

fout.close();

catch (IOException ex)

ex.printStackTrace();

}
return true;

public String lookup(String host)

String ip=(String)Clients.get(host);

return ip;

public String remove(String s)

String s1=(String)Clients.remove(s);

try

fout=new FileOutputStream("NameList.txt");

Clients.store(fout,"NameSpace");

fout.close();

catch (IOException ex)

ex.printStackTrace();

return s1;

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

DNSServer s=new DNSServer();


Naming.rebind("dns",s);

}}

5)b)Write a program to implement mini DNS protocol using RPC in java.


newdnsclient.java

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

import java.io.*;

import java.net.*;

class newdnsclient extends JFrame implements ActionListener

JButton b1,b2,b3,b4,b5;

JPanel p1,p2;

JLabel l1,l2;

JTextField t1,t2;

DataOutputStream output;

DataInputStream input;

newdnsclient()

/* Start of Socket Creation Code */

try

Socket s1=new Socket(InetAddress.getLocalHost(),8037);

input=new DataInputStream(s1.getInputStream());
output=new DataOutputStream(s1.getOutputStream());

catch(Exception e1)

e1.printStackTrace();

/* End of Socket Creation Code */

b1=new JButton("addhost");

b2=new JButton("lookup");

b3=new JButton("Remove");

b4=new JButton("Refresh");

b5=new JButton("Close");

p1=new JPanel();

p2=new JPanel();

l1=new JLabel("Host");

l2=new JLabel("IP");

t1=new JTextField("",20);

t2=new JTextField("",20);

p1.setLayout(new FlowLayout());

p1.add(l1);

p1.add(t1);

p1.add(l2);

p1.add(t2);
p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(b4);

p2.add(b5);

add(p1,"North");

add(p2,"South");

setSize(300,300);

setVisible(true);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

public void actionPerformed(ActionEvent e)

String s=e.getActionCommand();

try

if(s.equals("addhost"))

{
output.writeUTF("addhost");

output.writeUTF(t1.getText());

output.writeUTF(t2.getText());

s=input.readUTF();t2.setText(s);

if(s.equals("lookup"))

output.writeUTF("lookup");

output.writeUTF(t1.getText());

s=input.readUTF();t2.setText(s);

if(s.equals("Remove"))

output.writeUTF("Remove");

output.writeUTF(t1.getText());

s=input.readUTF();

t2.setText(s);

if(s.equals("Refresh"))

t1.setText("");

t2.setText("");

}
if(s.equals("Close"))

output.writeUTF("Close");

System.exit(0);

catch(Exception e1)

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

newdnsclient g=new newdnsclient();

}
NameList2.txt

#NameSpace

#Wed Nov 10 11:07:30 IST 2010


DNSServer.java

import java.util.*;

import java.io.*;

import java.net.*;

import java.awt.*;

class DNSServer

ServerSocket Server;

Socket connection;

DataOutputStream output;

DataInputStream input;

String s;

Properties Clients=new Properties();

FileInputStream fin=null;

FileOutputStream fout=null;

DNSServer()

try

fin=new FileInputStream("NameList2.txt");

if(fin!=null)
{

Clients.load(fin);

fin.close();

Server=new ServerSocket(8037);

connection=Server.accept();

output=new DataOutputStream(connection.getOutputStream());

input=new DataInputStream(connection.getInputStream());

catch(Exception e)

e.printStackTrace();

void runServer()

while(true)

try

s=input.readUTF();

if(s.equals("lookup"))

{
String s2=input.readUTF();

System.out.println(s2);

String s1=lookup(s2);

if(s1==null)

output.writeUTF("host name not found");

else

output.writeUTF("the ip address of"+s2+"is"+s1);}

if(s.equals("addhost"))

String s1=input.readUTF();

String s2=input.readUTF();

boolean b=addhost(s1,s2);

if(b==true)output.writeUTF("host name registered");

else

output.writeUTF("host name alreadUTFy existing");}

if(s.equals("Remove"))

String s2=input.readUTF();

System.out.println(s2);

String s1=(String)Clients.remove(s2);

System.out.println(s1);

if(s1==null)

output.writeUTF("host name not found");


else

output.writeUTF("the ip address is "+s1+" Removed");

fout=new FileOutputStream("NameList2.txt");

Clients.store(fout,"NameSpace");

fout.close(); }

if(s.equals("Close"))

System.exit(0);

catch(Exception e)

{e.printStackTrace();

boolean addhost(String name,String ip)

if(Clients.get(name)!=null)

return false;

Clients.put(name,ip);

try

{
fout =new FileOutputStream("NameList2.txt");

Clients.store(fout,"NameSpace");

fout.close();

catch(IOException ex)

ex.printStackTrace();

return true;

String lookup(String host)

String ip=(String)Clients.get(host);return ip;

public static void main(String args[])

DNSServer s=new DNSServer();

s.runServer();

}
6)a)Write a program to implement Query Processing in java.
QueryProcessing.java

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class QueryProcessing extends JFrame implements ActionListener

JFrame f;

JButton b1,b2,b3,b4;

JTextField t1;

JLabel l,l1,l2,l3,l4;

JTextArea ta;

int temp=0;

String e1;

QueryProcessing()

f= new JFrame("QUERY");

f.setDefaultCloseOperation(EXIT_ON_CLOSE);

f.setLayout(null);

b1=new JButton("SELECT");

b1.setBounds(150,200,100,25);
f.getContentPane().add(b1);

b1.addActionListener(this);

b2=new JButton("INSERT");

b2.setBounds(150,300,100,25);

f.getContentPane().add(b2); b2.addActionListener(this);

b3=new JButton("DELETE"); b3.setBounds(150,400,100,25);

f.getContentPane().add(b3);

b3.addActionListener(this);

b4=new JButton("EXIT");

b4.setBounds(150,500,100,25);

f.getContentPane().add(b4);

b4.addActionListener(this);

t1=new JTextField(50);

t1.setBounds(150,100,300,45);

f.getContentPane().add(t1);

l=new JLabel("Enter a Query");

l.setFont(new Font("Georgia",Font.ITALIC,20));

l.setBounds(10,100,150,25);

f.getContentPane().add(l);

l1=new JLabel("Query");

l1.setFont(new Font("Georgia",Font.ITALIC,24));

l1.setBounds(60,280,100,25); f.getContentPane().add(l1);

l2=new JLabel("Operations");
l2.setFont(new Font("Georgia",Font.ITALIC,24));

l2.setBounds(30,330,150,25);

f.getContentPane().add(l2);

l4=new JLabel("QUERY PROCESSING");

l4.setFont(new Font("Georgia",Font.ITALIC,28));

l4.setBounds(400,20,400,25);

f.getContentPane().add(l4);

ta=new JTextArea();

ta.setBounds(300,200,500,300);

f.getContentPane().add(ta);

f.setVisible(true); f.setSize(850,600);

public void actionPerformed(ActionEvent e)

String button=e.getActionCommand();

if(button.equals("SELECT"))

ta.setText((new db().select(t1.getText())));

System.out.println("Select button is clicked"+new db().select(t1.getText()));

t1.setText("");

else if(button.equals("INSERT"))
{

new db().indel(t1.getText());

System.out.println("Insert button is clicked"); t1.setText(""); }

else if(button.equals("DELETE"))

new db().indel(t1.getText());

System.out.println("Delete button is clicked");

t1.setText(""); }

else if(button.equals("EXIT"))

f.hide();

public static void main(String args[])

new QueryProcessing();

}
Db.java

// Query Processing Example

import java.io.*;

import java.sql.*;

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

class db

Connection connection;

Statement stmt;

ResultSet rs;

public db()

try

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

connection =DriverManager.getConnection("jdbc:odbc:qp");

stmt=connection.createStatement();

catch(Exception ere)
{

public String select(String s)

String s1="";

try

rs=stmt.executeQuery(s);

ResultSetMetaData rsMetaData = rs.getMetaData();

int numberOfColumns = rsMetaData.getColumnCount();

for(int i=1;i<=numberOfColumns;i++)

s1=s1+"\t"+rsMetaData.getColumnName(i);

s1=s1+"\n";

System.out.println("datas from database"+numberOfColumns);

while(rs.next())

for(int i=1;i<=numberOfColumns;i++)

s1=s1+"\t"+rs.getString(i);}s1=s1+"\n";

System.out.println(s1);

}
catch(Exception e)

e.printStackTrace();

JLabel errorFields = new JLabel("<HTML><FONT COLOR = BLUE>Query is not


processed.</FONT></HTML>");

JOptionPane.showMessageDialog(null,errorFields);

System.out.println("exception occured");

return(s1); }

public void indel(String s)

try

stmt.executeUpdate(s);

JLabel errorFields = new JLabel("<HTML><FONT COLOR = BLUE>Query is processed.</FONT></HTML>")

JOptionPane.showMessageDialog(null,errorFields); }

catch(Exception e)

e.printStackTrace();

JLabel errorFields = new JLabel("<HTML><FONT COLOR = BLUE>Query is not


processed</FONT></HTML>");

JOptionPane.showMessageDialog(null,errorFields);

System.out.println("exception occured");

public static void main(String args[])


{

new db(); } }

6)b)Write a program it implement 2PC in java.


//db1.ldb and db1.mdb are there.

query.txt

update account set bal = 25000 where accno =1


Client.java

import java.io.*;

import java.net.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.JList;

import javax.swing.event.*;

import java.util.*;

class Client extends Frame implements ActionListener

Button b1,b2,b3,b4,b5;

Panel p1,p2;

TextField t1;

Label l1;

ServerSocket ss;

Socket s;

DataOutputStream output;
DataInputStream input;

Connection con;

Statement stmt;

String prepareStatus="";

static int port;

Client()

b1=new Button("Prepared");

b2=new Button("NotPrepared");

b3=new Button("Commit");

b4=new Button("Execute");

b5=new Button("Exit");

t1=new TextField("",35);

l1=new Label("SQL");

p1=new Panel();

p2=new Panel();

p1.setLayout(new FlowLayout());

p1.add(l1);

p1.add(t1);

p2.add(b1);

p2.add(b2);

p2.add(b3);

p2.add(b4);

p2.add(b5);
add(p1);

add(p2,"South");

setSize(300,300);

setVisible(true);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

try

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con= DriverManager.getConnection("jdbc:odbc:2pc");

stmt = con.createStatement();

con.setAutoCommit(false);

catch (Exception e1)

public void actionPerformed(ActionEvent ae)

try
{

String str=ae.getActionCommand();

if(str.equals("Execute"))

String query = t1.getText();

System.out.println(stmt.executeUpdate(query));

t1.setText("Executed");

System.out.println(prepareStatus);

if(str.equals("Prepared"))

output.writeUTF("Prepared");//******

prepareStatus=input.readUTF();//*****

t1.setText(prepareStatus);

System.out.println(prepareStatus);

if(str.equals("NotPrepared"))

output.writeUTF("NotPrepared");//****

System.out.println(prepareStatus);

}
if(str.equals("Commit"))

if(prepareStatus.equals("Commit"))

con.commit();

t1.setText("commit Complete");

else

t1.setText("commit not Complete");

if(str.equals("Exit"))

System.exit(0);

stmt.close();

con.close();

catch(Exception e)

JLabel errorFields = new JLabel("<HTML><FONT COLOR = BLUE>TABLE


LOCKED.</FONT></HTML>");

JOptionPane.showMessageDialog(null,errorFields);
//e.printStackTrace();

} }

public void runServer(int port)

try

ss=new ServerSocket(port);

s=ss.accept();

output=new DataOutputStream(s.getOutputStream());

input=new DataInputStream(s.getInputStream());

System.out.println("ready:");

catch(Exception e)

e.printStackTrace();

public static void main(String args[])

Client cl=new Client();

port=Integer.parseInt(args[0]);

cl.runServer(port);

}
Server.java

import java.io.*;

import java.net.*;

class Server

public static void main(String args[])

try

Socket s1=new Socket(InetAddress.getLocalHost(),6001);

DataInputStream input1=new DataInputStream(s1.getInputStream());

DataOutputStream output1=new DataOutputStream(s1.getOutputStream());

Socket s2=new Socket(InetAddress.getLocalHost(),6002);

DataInputStream input2=new DataInputStream(s2.getInputStream());

DataOutputStream output2=new DataOutputStream(s2.getOutputStream());

while (true)

String rs1=input1.readUTF();

String rs2=input2.readUTF();

//System.out.println(rs1);
if((rs1.equals("Prepared")) &&(rs2.equals("Prepared")))

output1.writeUTF("Commit");

output2.writeUTF("Commit");

else

output1.writeUTF("wait for others to prepare.");

output2.writeUTF("wait for others to prepare.");

catch(Exception e1)

e1.printStackTrace();

}
7)Study of NFS.
1. Create a Folder nfs/abc.txt

2. Know the ipaddress

Applications->System Settings->Network—edit ( ipaddress, subnetmask)

(or) In terminal type ifconfig

3. Enable the desired services

1. System Services->Server Settings->Services

 Network (Enable)

 Nfs (Enable)

 Iptables (Disable) (we do not firewalls)

2. System Settings ->Security Level (Firewall options-disable, Selinux-disable)

Creation of NFS Server

1. System Settings->Server Settings->NFS

+ Add (All are making security levels low)

2. Open Terminal

Type: service nfs restart

Creation of NFS Client

Open terminal

Type: df

Type: mount –t nfs 135.135.5.120:/usr/nfs /root/abc

cd abc

ls : abc.txt

Unmount: umount –t nfs 135.135.5.120:/usr/nfs


Note: service network restart (if n/w is disabled use this )

You might also like