Lab Manual
Lab Manual
Lab Manual
--------------------------
2)b)FTPClientGUIModified java.-------------------------------------------------------------------------
3)b)Chat-gui-19.10.10----------------------------------------------------------------------------
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
System.out.println("connecting to server!");
System.out.println("Enter u r name");
String n=br.readLine();
OutputStream os=cs.getOutputStream();
os.write(n.getBytes());
InputStream is=cs.getInputStream();
is.read(data);
//PRINTING MESS AT CLIENT CONSLOE
mfs=mfs.trim();
System.out.println(mfs);
}
TCPServer.java
import java.io.*;
import java.net.*;
class TcpServer
while (true)
System.out.println("server is ready!");
Socket ls=ss.accept();
InputStream is=ls.getInputStream();
is.read(data);
mfc=mfc.trim();
String mfs="hello:"+mfc;
OutputStream os=ls.getOutputStream();
os.write(mfs.getBytes());
}
}
import java.net.*;
class DgClient
while (true)
ds.receive(p);
System.out.println(new String(p.getData(),0,p.getLength()));
{
ds=new DatagramSocket(cp);
client();
}
DatagrmApp.java
import java.net.*;
class DatagrmServerApp
int pos=0;
while (true)
int c=System.in.read();
switch (c)
case '\r':break;
pos=0;
default: buff[pos++]=(byte)c;
}
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.*;
/* ctrl space */
public JLabel l;
One()
b=new JButton("Upload");
lmsg1=new JLabel("");
b1=new JButton("Download");
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);
try {
/* String s=e.getActionCommand();
if(s.equals("Upload"))*/
if (b.getModel().isArmed())
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);
nt n=fis.read();
while (n!=-1)
out.print((char)n);n=fis.read();
//s.close();
repaint();
if (b1.getModel().isArmed())
String remoteadd=s.getRemoteSocketAddress().toString();
System.out.println(remoteadd);
int val;
val=j1.showOpenDialog(One.this);
String filename=j1.getSelectedFile().getName();
String filepath=j1.getSelectedFile().getPath();
System.out.println("File name:"+filename);
out.println("Download");
out.println(filepath);
int ch;
while ((ch=fromserver.read())!=-1)
fout.write((char) ch);
fout.close();//s.close();
lmsg2.setText(filename+"is downlaoded");
repaint();
System.out.println(ee);
}
}
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;
try {
while (true)
Socket sl=ss.accept();
System.out.println(" test1");
System.out.println(" test2");
String option=fromserver.readLine();
if (option.equalsIgnoreCase("upload"))
System.out.println("upload test");
String filefromclient=fromserver.readLine();
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();
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);
}
2)b)FTPClientGUIModified.java
import javax.swing.*;import java.awt.*;
import java.io.*;
One()
b=new JButton("Upload");
lmsg1=new JLabel("");
b1=new JButton("Download");
lmsg2=new JLabel("");
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);
String s=e.getActionCommand();
if(s.equals("Upload"))
int val;
val=j.showOpenDialog(One.this);
lmsg1.setText("is uploaded");
//s.close();
repaint();
if (b1.getModel().isArmed())
repaint();
}
}
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;
new CCLogin();
}
public CCLogin()
this.button.addActionListener(this);
localJPanel.add(this.heading); localJPanel.add(this.label);
localJPanel.add(this.tf);
localJPanel.add(this.button);
this.frame1.add(localJPanel);
localJPanel.setLayout(null);
this.frame1.setSize(300,300);
this.frame1.setVisible(true);
this.frame1.setDefaultCloseOperation(3);
try
str = this.tf.getText();
this.frame1.dispose();
c1.main(null);
catch(Exception localIOException)
}
ChatMultiServer.java
import java.net.*;
import java.io.*;
Thread t;
Socket s;
A(Socket x)
s=x;
t=new Thread(this);
t.start();
try
InputStream is=s.getInputStream();
is.read(data);
System.out.println(mfc);
String n=br.readLine();
OutputStream os=s.getOutputStream();
os.write(n.getBytes());
catch(Exception e)
e.printStackTrace();
class ChatMultiServer
System.out.println("ServerSocket is creating");
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
public Client1(String n)
name=n;
System.out.println("connecting to server");
while(true)
String n=br.readLine();
OutputStream os=s.getOutputStream();
os.write(n.getBytes());
InputStream is=s.getInputStream();
is.read(data);
mfc=mfc.trim();
System.out.println(mfc);
}
MulticastPeer.java
import java.net.*;
import java.io.*;
// args give message contents and destination multicast group (e.g. "228.5.6.7")
MulticastSocket s =null;
try
byte [] m = args[0].getBytes();
s.receive(messageIn);
catch (SocketException e)
catch (IOException e)
finally
}
3)b)Chat-gui-19.10.10
Client1.java
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
JButton button;
JTextArea t1;
JTextArea t2;
Socket s;
JScrollPane scrollPane1;
JScrollPane scrollPane2;
public Client1()
try
button=new JButton("SEND");
t1=new JTextArea(10,20);
t2=new JTextArea(10,20);
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");
s= new Socket("localhost",1010);
while(true)
InputStream is=s.getInputStream();
is.read(data);
String mfc=new String(data);
mfc=mfc.trim();
catch(Exception e1)
e1.printStackTrace();
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();
}
}
}
ChatMultiServer.java
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
Thread t;
Socket s;
JButton button;
JTextArea t1;
JTextArea t2;
JScrollPane scrollPane1;
JScrollPane scrollPane2;
A(Socket x)
s=x;
t=new Thread(this);
t.start();
try
button=new JButton("SEND");
t1=new JTextArea(10,20);
t2=new JTextArea(10,20);
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);
while(true)
InputStream is=s.getInputStream();
is.read(data);
catch (Exception e)
e.printStackTrace();
try
mess=t1.getText();
OutputStream os=s.getOutputStream();
os.write(mess.getBytes());
t1.selectAll();
t1.setText(" ");
catch (Exception e)
e.printStackTrace();
}
class ChatMultiServer
while(true)
Socket s=ss.accept();
new A(s);
}
4)a)Write a program to demonstrate RMI in java.
Hello.java
}
HelloImpl.java
import java.rmi.server.*;
private int i;
super();
i++;
return "Hello " +s+ " yours is " +i+ " request";
}
HelloClient.java
import java.rmi.*;
class HelloClient
Exception
Remote ref=Naming.lookup("hello");
Hello h = (Hello)ref;
System.out.println(result);
//rmi://135.135.5.146:1099/
HelloServer.java
import java.rmi.*;
import java.rmi.server.*;
class HelloServer {
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.*;
}
arithmeticImpl.java
import java.rmi.*;
import java.rmi.server.*;
return res;
return res;
{
double res = c * c1;
return res;
return res;
}
Arithmetic Client.java
//package rmipack;
import java.rmi.*;
class ArithmeticClient
1. rmi service
/*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]);
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.*;
java.rmi.registry.LocateRegistry.createRegistry(1099);
/*
*/
Naming.rebind("rmiintf",ob1);
}
}
#NameSpace
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.rmi.*;
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);
}
DNS d=null;
try
Remote ref=Naming.lookup("dns");
d=(DNS)ref;
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)
else
if(s.equals("Remove"))
String s2=d.remove(t1.getText());
t2.setText(s2);
if(s2==null)
else
if(s.equals("Refresh"))
t1.setText("");
t2.setText("");
if(s.equals("Close"))
System.exit(0);
}
DNS.java
}
DNSServer.java
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
String s;
FileInputStream fin=null;
FileOutputStream fout=null;
super();
try
fin=new FileInputStream("NameList.txt");
if(fin!=null){Clients.load(fin);
fin.close();
catch (Exception e)
e.printStackTrace();
if(Clients.get(name)!=null)
return false;
Clients.put(name,ip);
try
fout=new FileOutputStream("NameList.txt");
Clients.store(fout,"NameSpace");
fout.close();
ex.printStackTrace();
}
return true;
String ip=(String)Clients.get(host);
return ip;
String s1=(String)Clients.remove(s);
try
fout=new FileOutputStream("NameList.txt");
Clients.store(fout,"NameSpace");
fout.close();
ex.printStackTrace();
return s1;
}}
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
JButton b1,b2,b3,b4,b5;
JPanel p1,p2;
JLabel l1,l2;
JTextField t1,t2;
DataOutputStream output;
DataInputStream input;
newdnsclient()
try
input=new DataInputStream(s1.getInputStream());
output=new DataOutputStream(s1.getOutputStream());
catch(Exception e1)
e1.printStackTrace();
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);
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)
}
NameList2.txt
#NameSpace
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
class DNSServer
ServerSocket Server;
Socket connection;
DataOutputStream output;
DataInputStream input;
String s;
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)
else
if(s.equals("addhost"))
String s1=input.readUTF();
String s2=input.readUTF();
boolean b=addhost(s1,s2);
else
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)
fout=new FileOutputStream("NameList2.txt");
Clients.store(fout,"NameSpace");
fout.close(); }
if(s.equals("Close"))
System.exit(0);
catch(Exception e)
{e.printStackTrace();
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;
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.*;
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);
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.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.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);
String button=e.getActionCommand();
if(button.equals("SELECT"))
ta.setText((new db().select(t1.getText())));
t1.setText("");
else if(button.equals("INSERT"))
{
new db().indel(t1.getText());
else if(button.equals("DELETE"))
new db().indel(t1.getText());
t1.setText(""); }
else if(button.equals("EXIT"))
f.hide();
new QueryProcessing();
}
Db.java
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)
{
String s1="";
try
rs=stmt.executeQuery(s);
for(int i=1;i<=numberOfColumns;i++)
s1=s1+"\t"+rsMetaData.getColumnName(i);
s1=s1+"\n";
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();
JOptionPane.showMessageDialog(null,errorFields);
System.out.println("exception occured");
return(s1); }
try
stmt.executeUpdate(s);
JOptionPane.showMessageDialog(null,errorFields); }
catch(Exception e)
e.printStackTrace();
JOptionPane.showMessageDialog(null,errorFields);
System.out.println("exception occured");
new db(); } }
query.txt
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.*;
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="";
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);
try
{
String str=ae.getActionCommand();
if(str.equals("Execute"))
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
if(str.equals("Exit"))
System.exit(0);
stmt.close();
con.close();
catch(Exception e)
JOptionPane.showMessageDialog(null,errorFields);
//e.printStackTrace();
} }
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();
port=Integer.parseInt(args[0]);
cl.runServer(port);
}
Server.java
import java.io.*;
import java.net.*;
class Server
try
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
catch(Exception e1)
e1.printStackTrace();
}
7)Study of NFS.
1. Create a Folder nfs/abc.txt
Network (Enable)
Nfs (Enable)
2. Open Terminal
Open terminal
Type: df
cd abc
ls : abc.txt