Monday, December 29, 2008

Word Application in Java(Windows/MAC/LINUX)

Professional.java
compile and run from the Command Prompt
Note: Some files are missing which can be forwarded upon request.
rajneshwar@gmail.com
-----------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.border.*;
import java.io.*;
import java.awt.datatransfer.*;

public class Professional extends JFrame implements ActionListener
{
String software = "Professional Word 2007";
String username = "Rajneshwar Prasad";
private JDesktopPane jdp = new JDesktopPane();
Container c = getContentPane();
JInternalFrame login;
JTextArea textArea;

Color color1 = new Color(170,196,181);
Color color2 = new Color(0,0,0);
Color color3 = new Color(23,14,199);

JPanel loginPanel = new JPanel();
JLabel userLabel = new JLabel("Username");
JLabel passLabel = new JLabel("Password");
JTextField userText = new JTextField(10);
JPasswordField passText = new JPasswordField(10);
JButton submit = new JButton("Submit");
JButton cancel = new JButton("Cancel");

JPanel tool = new JPanel();
Icon newIcon = new ImageIcon("images//new.gif");
JButton newButton = new JButton(newIcon);
Icon openIcon = new ImageIcon("images//new.gif");
JButton openButton = new JButton(openIcon);
Icon saveIcon = new ImageIcon("images//new.gif");
JButton saveButton = new JButton(saveIcon);

JMenuBar jmb = new JMenuBar();
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu view = new JMenu("View");
JMenu insert = new JMenu("Insert");
JMenu format = new JMenu("Format");
JMenu tools = new JMenu("Tools");
JMenu table = new JMenu("Table");
JMenu window = new JMenu("Window");
JMenu help = new JMenu("Help");

JMenuItem fnew = new JMenuItem("New...");JMenuItem fopen = new JMenuItem("Open...");
JMenuItem fclose = new JMenuItem("Close");JMenuItem fsave = new JMenuItem("Save");
JMenuItem fsaveas = new JMenuItem("Save As...");JMenuItem fsaveaswebpage = new JMenuItem("Save As Web Page");
JMenuItem fsearch = new JMenuItem("Search...");JMenuItem fversions = new JMenuItem("Versions...");
JMenuItem fwebpagepreview = new JMenuItem("Web Page Preview");JMenuItem fpagesetup = new JMenuItem("Page Setup...");
JMenuItem fprintpreview = new JMenuItem("Print Preview");JMenuItem fprint = new JMenuItem("Print...");
JMenuItem fproperties = new JMenuItem("Properties");JMenuItem fexit = new JMenuItem("Exit");
JMenu fsendto = new JMenu("Send To");
JMenuItem fsmailrecipient = new JMenuItem("Mail Recipient");
JMenuItem fsmailrecipientreview = new JMenuItem("Mail Recipient (for Review)...");
JMenuItem fsmailrecipientattach = new JMenuItem("Mail Recipient (as Attachment)...");
JMenuItem fsroutingrecipient = new JMenuItem("Routing Recipient...");
JMenuItem fsexchangefolder = new JMenuItem("Exchange Folder...");
JMenuItem fsonlinemeeting = new JMenuItem("Online Meeting Participant");
JMenuItem fsfaxrecipient = new JMenuItem("Fax Recipient...");
JMenuItem fsmspowerpoint = new JMenuItem("Microsoft PowerPoint");

JMenuItem eundo = new JMenuItem("Undo");JMenuItem eredo = new JMenuItem("Redo");
JMenuItem ecut = new JMenuItem("Cut");JMenuItem ecopy = new JMenuItem("Copy");
JMenuItem eofficeclipboard = new JMenuItem("Office Clip Board...");JMenuItem epaste = new JMenuItem("Paste");
JMenuItem epastespecial = new JMenuItem("Paste Special...");JMenuItem epasteashyperlink = new JMenuItem("Paste as Hyperlink");
JMenuItem eselectall = new JMenuItem("Select All");JMenuItem efind = new JMenuItem("Find...");
JMenuItem ereplace = new JMenuItem("Replace...");JMenuItem egoto = new JMenuItem("Go To...");
JMenuItem elinks = new JMenuItem("Links..."); JMenuItem eobject = new JMenuItem("Object");
JMenu eclear = new JMenu("Clear");
JMenuItem ecformats = new JMenuItem("Formats");
JMenuItem eccontents = new JMenuItem("Contents Del");

JMenuItem vnormal = new JMenuItem("Normal");JMenuItem vweblayout = new JMenuItem("Web Layout");
JMenuItem vprintlayout = new JMenuItem("Print Layout");JMenuItem voutline = new JMenuItem("Outline");
JMenuItem vtaskpane = new JMenuItem("Task Pane");JMenuItem vruler = new JMenuItem("Ruler");
JMenuItem vdocumentmap = new JMenuItem("Document Map");JMenuItem vheadernfooter = new JMenuItem("Header and Footer");
JMenuItem vfootnotes = new JMenuItem("Footnotes");JMenuItem vmarkup = new JMenuItem("Markup");
JMenuItem vfullscreen = new JMenuItem("Full Screen");JMenuItem vzoom = new JMenuItem("Zoom...");
JMenu vtoolbars = new JMenu("Toolbars");
JCheckBoxMenuItem vtstandard = new JCheckBoxMenuItem("Standard");JCheckBoxMenuItem vtformatting = new JCheckBoxMenuItem("Formating");
JCheckBoxMenuItem vtautotext = new JCheckBoxMenuItem("AutoText");JCheckBoxMenuItem vtcontrol = new JCheckBoxMenuItem("Control Toolbox");
JCheckBoxMenuItem vtdatabase = new JCheckBoxMenuItem("Database");JCheckBoxMenuItem vtdrawing = new JCheckBoxMenuItem("Drawing");
JCheckBoxMenuItem vtforms = new JCheckBoxMenuItem("Forms");JCheckBoxMenuItem vtframes = new JCheckBoxMenuItem("Frames");
JCheckBoxMenuItem vtmailmerge = new JCheckBoxMenuItem("Mail Merge");JCheckBoxMenuItem vtoutline = new JCheckBoxMenuItem("Outlining");
JCheckBoxMenuItem vtpicture = new JCheckBoxMenuItem("Picture");JCheckBoxMenuItem vtreview = new JCheckBoxMenuItem("Reviewing");
JCheckBoxMenuItem vttables = new JCheckBoxMenuItem("Tables and Borders");JCheckBoxMenuItem vtvb = new JCheckBoxMenuItem("Visual Basic");
JCheckBoxMenuItem vtweb = new JCheckBoxMenuItem("Web");JCheckBoxMenuItem vtwebtools = new JCheckBoxMenuItem("Web Tools");
JCheckBoxMenuItem vtwordcount = new JCheckBoxMenuItem("Word Count");JCheckBoxMenuItem vtwordart = new JCheckBoxMenuItem("Word Art");
JMenuItem vtcustomize = new JMenuItem("Customize...");

JMenuItem ibreak = new JMenuItem("Break...");JMenuItem ipagenum = new JMenuItem("Page Numbers...");
JMenuItem idate = new JMenuItem("Date and Time...");JMenuItem ifield = new JMenuItem("Field...");
JMenuItem isymbol = new JMenuItem("Symbol...");JMenuItem icomment = new JMenuItem("Comment");
JMenuItem iwebcomp = new JMenuItem("Web Component...");JMenuItem idiagram = new JMenuItem("Diagram...");
JMenuItem itextbox = new JMenuItem("Text Box");JMenuItem ifile = new JMenuItem("File...");
JMenuItem iobject = new JMenuItem("Object...");JMenuItem ibookmark = new JMenuItem("Bookmark");
JMenuItem ihyperlink = new JMenuItem("Hyperlink...");
JMenu iautotext = new JMenu("AutoText");JMenu iref = new JMenu("Reference");
JMenu ipicture = new JMenu("Picture");

JMenuItem ffont = new JMenuItem("Font...");JMenuItem fparagraph = new JMenuItem("Paragraph...");
JMenuItem fbullets = new JMenuItem("Bullets and Numbering...");JMenuItem fborders = new JMenuItem("Borders and Shading...");
JMenuItem fcolumns = new JMenuItem("Columns...");JMenuItem ftabs = new JMenuItem("Tabs...");
JMenuItem fdropcap = new JMenuItem("Drop Cap...");JMenuItem ftextdir = new JMenuItem("Text Direction...");
JMenuItem fchangecase = new JMenuItem("Change Case...");JMenuItem ftheme = new JMenuItem("Theme...");
JMenuItem fautoformat = new JMenuItem("AutoFormat");JMenuItem fstylenformat = new JMenuItem("Styles and Formatting...");
JMenuItem frevealformat = new JMenuItem("Reveal Formatting");JMenuItem fobject = new JMenuItem("Object...");
JMenu fbackground = new JMenu("Background");JMenu fframes = new JMenu("Frames");

JMenuItem tspelling = new JMenuItem("Spelling and Grammar...");
JMenuItem tfixbroken = new JMenuItem("Fix Broken Text...");
JMenuItem twordcount = new JMenuItem("Word Count...");
JMenuItem tautosum = new JMenuItem("AutoSummrize...");
JMenuItem tspeech = new JMenuItem("Speech");
JMenuItem ttrackchanges = new JMenuItem("Track Changes");
JMenuItem tcomparenmerge = new JMenuItem("Compare and Merge Documents...");
JMenuItem tprotect = new JMenuItem("Protect Document...");
JMenuItem ttoolsweb = new JMenuItem("Tools on the Web...");
JMenuItem ttemplates = new JMenuItem("Templates and Add-Ins");
JMenuItem tautocorrect = new JMenuItem("AutoCorrect Options...");
JMenuItem tcustomize = new JMenuItem("Customize...");
JMenuItem toptions = new JMenuItem("Options...");
JMenu tlanguage = new JMenu("Language");
JMenu tonlinecol = new JMenu("Online Collaboration");
JMenu tletters = new JMenu("Letters and Mailings");
JMenu tmacro = new JMenu("Macro");

JMenuItem tbdraw = new JMenuItem("Draw Table");
JMenuItem tbmerge = new JMenuItem("Merge Cells");
JMenuItem tbsplitcell = new JMenuItem("Split Cells...");
JMenuItem tbsplittable = new JMenuItem("Split Table");
JMenuItem tbautoformat = new JMenuItem("Table AutoFormat...");
JMenuItem tbheading = new JMenuItem("Heading Rows Repeat");
JMenuItem tbsort = new JMenuItem("Sort...");
JMenuItem tbformula = new JMenuItem("Formula...");
JMenuItem tbhidegrid = new JMenuItem("Hide Gridlines...");
JMenuItem tbproperties = new JMenuItem("Table Properties");
JMenu tbinsert = new JMenu("Insert");
JMenu tbdelete = new JMenu("Delete");
JMenu tbselect = new JMenu("Select");
JMenu tbautofit = new JMenu("AutoFit");
JMenu tbconvert = new JMenu("Convert");

JMenuItem wnew = new JMenuItem("New Window");
JMenuItem warrange = new JMenuItem("Arrange All");
JMenuItem wsplit = new JMenuItem("Split");

JMenuItem hmsword = new JMenuItem("Microsoft Word Help");
JMenuItem hoffassist = new JMenuItem("Show the Office Assistant");
JMenuItem hwhats = new JMenuItem("What's This?");
JMenuItem hoffice = new JMenuItem("Office on the Web");
JMenuItem hactivate = new JMenuItem("Activate Product...");
JMenuItem hwperfect = new JMenuItem("WordPerfect Help...");
JMenuItem hdetect = new JMenuItem("Detect and Repair...");
JMenuItem habout = new JMenuItem("About Microsoft Word");

Clipboard clipboard = getToolkit().getSystemClipboard();

NewWindow nw = new NewWindow();
int win = 0;

public Professional()throws Exception{
setTitle(software+" -- Rajneshwar Prasad");
Splash sp = new Splash(1000);
sp.showSplash();

jdp.setDragMode(5);
jdp.setAutoscrolls(true);
jdp.setToolTipText(""+software+"");

jdp.setBackground(color1);
jdp.setOpaque(true);

login = new JInternalFrame("Please Log-in",true,true,true,true);
UIManager.put("ToolTip.foreground",new ColorUIResource(Color.blue));
UIManager.put("ToolTip.background",new ColorUIResource(Color.white));

login.setToolTipText("
Contact Administrator
if unable to Log-in
");

submit.addActionListener(this);
cancel.addActionListener(this);
loginPanel.add(userLabel);loginPanel.add(userText);
//loginPanel.add(passLabel);loginPanel.add(passText);
loginPanel.add(submit);loginPanel.add(cancel);
login.getContentPane().add(loginPanel,BorderLayout.CENTER);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
login.setLocation(screen.width/2-100,screen.height/2-150);
login.setSize(200,130);
login.setResizable(false);
login.setMaximizable(false);
login.setClosable(false);
//Rectangle rect = new Rectangle(screen.width/2-100,screen.height/2-150,150,200);
//login.setBounds(rect);
login.setSelected(true);
login.toFront();
jdp.add(login);
login.setVisible(true);

c.add(jdp);
}
public void actionPerformed(ActionEvent ae){
if (ae.getSource()==submit){
username = userText.getText();
if(!(username.equals("")))
setTitle(software+" -- "+username);
login.setVisible(false);

jmb.add(file);jmb.add(edit);jmb.add(view);
jmb.add(insert);jmb.add(format);jmb.add(tools);
jmb.add(table);jmb.add(window);jmb.add(help);

fileMenu();editMenu();viewMenu();insertMenu();formatMenu();
toolsMenu(); tableMenu(); windowMenu(); helpMenu();

setJMenuBar(jmb);

JToolBar jtb = new JToolBar(software);
newButton.addActionListener(this);openButton.addActionListener(this);saveButton.addActionListener(this);
newButton.setToolTipText("New");
jtb.add(newButton);
openButton.setToolTipText("Open");
jtb.add(openButton);
saveButton.setToolTipText("Save");
Border m_lowered = new BevelBorder(BevelBorder.RAISED);
//saveButton.setBorder(m_lowered);
jtb.add(saveButton);
c.add(jtb,BorderLayout.NORTH);
win++;
nw.createWindow(jdp,win,textArea);
}
if(ae.getSource()==fnew||ae.getSource()==newButton){
win++;
nw.createWindow(jdp,win,textArea);
}
if(ae.getSource()==fopen||ae.getSource()==openButton){
JFileChooser jfc = new JFileChooser();
int ret = jfc.showOpenDialog(this);
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (ret == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
System.out.println("Opening: " + file.getName() + "." );
} else {
System.out.println("Open command cancelled by user." );
}
}
if(ae.getSource()==fclose){
nw.closeWindow();
}
if(ae.getSource()==fsave||ae.getSource()==fsaveas||ae.getSource()==saveButton){
JFileChooser jfc = new JFileChooser();
int ret = jfc.showSaveDialog(this);
}
if(ae.getSource()==fexit){System.exit(0);}

if(ae.getSource()==ecopy){
StringSelection data = new StringSelection(textArea.getText());
clipboard.setContents(data, data);
}
if(ae.getSource()==epaste){
Transferable clipData = clipboard.getContents(this);
String s;
try {
s = (String) (clipData.getTransferData(DataFlavor.stringFlavor));
}catch (Exception ex) {
s = ex.toString();
}
textArea.setText(s);
}
}
public static void main(String[] args)throws Exception
{
Professional p = new Professional();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
p.setSize(screen.width,screen.height-50);
p.show();
p.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
p.setIconImage(Toolkit.getDefaultToolkit().getImage("images//icon.gif"));
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
public void fileMenu(){
fnew.addActionListener(this);fopen.addActionListener(this);
fclose.addActionListener(this);fsave.addActionListener(this);
fsaveas.addActionListener(this);fsaveaswebpage.addActionListener(this);
fsearch.addActionListener(this);fwebpagepreview.addActionListener(this);
fversions.addActionListener(this);fpagesetup.addActionListener(this);
fprintpreview.addActionListener(this);fprint.addActionListener(this);
fproperties.addActionListener(this);fexit.addActionListener(this);

fnew.setIcon(newIcon);
fopen.setIcon(newIcon);
fsave.setIcon(newIcon);

file.add(fnew);file.add(fopen);file.add(fclose);file.addSeparator();
file.add(fsave);file.add(fsaveas);file.add(fsaveaswebpage);file.add(fsearch);file.addSeparator();
file.add(fversions);file.addSeparator();
file.add(fwebpagepreview);file.addSeparator();
file.add(fpagesetup);file.add(fprintpreview);file.add(fprint);file.addSeparator();
file.add(fsendto);file.add(fproperties);file.addSeparator();
file.add(fexit);

fsmailrecipient.addActionListener(this); fsmailrecipientreview.addActionListener(this);
fsmailrecipientattach.addActionListener(this);fsroutingrecipient.addActionListener(this);
fsexchangefolder.addActionListener(this);fsonlinemeeting.addActionListener(this);
fsfaxrecipient.addActionListener(this);fsmspowerpoint.addActionListener(this);

fsendto.add(fsmailrecipient); fsendto.add(fsmailrecipientreview);
fsendto.add(fsmailrecipientattach); fsendto.add(fsroutingrecipient);
fsendto.add(fsexchangefolder); fsendto.add(fsonlinemeeting);
fsendto.add(fsfaxrecipient); fsendto.addSeparator();
fsendto.add(fsmspowerpoint);

}
public void editMenu(){
eundo.addActionListener(this);eredo.addActionListener(this);
ecut.addActionListener(this);ecopy.addActionListener(this);
eofficeclipboard.addActionListener(this);epaste.addActionListener(this);
epasteashyperlink.addActionListener(this);eselectall.addActionListener(this);
efind.addActionListener(this);ereplace.addActionListener(this);
egoto.addActionListener(this);elinks.addActionListener(this);
eobject.addActionListener(this);

edit.add(eundo);edit.add(eredo);edit.addSeparator();
edit.add(ecut);edit.add(ecopy);edit.add(eofficeclipboard);edit.add(epaste);edit.add(epastespecial);edit.add(epasteashyperlink);edit.addSeparator();
edit.add(eclear);edit.add(eselectall);edit.addSeparator();
edit.add(efind);edit.add(ereplace);edit.add(egoto);edit.addSeparator();
edit.add(elinks);edit.add(eobject);

ecformats.addActionListener(this); eccontents.addActionListener(this);
eclear.add(ecformats); eclear.add(eccontents);
}
public void viewMenu(){
view.add(vnormal);view.add(vweblayout);view.add(vprintlayout);view.addSeparator();
view.add(vtoolbars);
view.add(voutline); view.add(vtaskpane);view.add(vruler);view.add(vdocumentmap);view.addSeparator(); view.add(vheadernfooter);
view.add(vfootnotes);view.add(vmarkup);view.addSeparator();
view.add(vfullscreen); view.add(vzoom);

vtoolbars.add(vtstandard); vtoolbars.add(vtformatting);vtoolbars.add(vtstandard);
vtoolbars.add(vtformatting);vtoolbars.add(vtautotext); vtoolbars.add(vtcontrol);
vtoolbars.add(vtdatabase); vtoolbars.add(vtdrawing); vtoolbars.add(vtforms);
vtoolbars.add(vtframes); vtoolbars.add(vtmailmerge); vtoolbars.add(vtoutline);
vtoolbars.add(vtpicture); vtoolbars.add(vtreview); vtoolbars.add(vttables);
vtoolbars.add(vtvb); vtoolbars.add(vtweb); vtoolbars.add(vtwebtools);
vtoolbars.add(vtwordcount); vtoolbars.add(vtwordart);
vtoolbars.addSeparator();
vtoolbars.add(vtcustomize);
}
public void insertMenu(){
insert.add(ibreak); insert.add(ipagenum);
insert.add(idate); insert.add(ifield); insert.add(iautotext);
insert.add(isymbol); insert.add(icomment); insert.addSeparator();
insert.add(iref); insert.add(iwebcomp); insert.addSeparator();
insert.add(ipicture); insert.add(idiagram); insert.add(itextbox);
insert.add(ifile); insert.add(iobject); insert.add(ibookmark);
insert.add(ihyperlink);
}
public void formatMenu(){
format.add(ffont); format.add(fparagraph);
format.add(fbullets);format.add(fborders);
format.add(fcolumns);format.add(ftabs);
format.add(fdropcap);format.add(ftextdir);
format.add(fchangecase);format.addSeparator();
format.add(ftheme);
format.add(fautoformat);format.add(fstylenformat);
format.add(frevealformat);format.add(fobject);

JMenu fbackground = new JMenu("Background");
JMenu fframes = new JMenu("Frames");
}
public void toolsMenu(){
tools.add(tspelling); tools.add(tlanguage); tools.add(tfixbroken);
tools.add(twordcount); tools.add(tautosum);
tools.add(tspeech); tools.addSeparator(); tools.add(ttrackchanges);
tools.add(tcomparenmerge); tools.add(tprotect); tools.add(tonlinecol); tools.addSeparator();
tools.add(tletters); tools.addSeparator(); tools.add(ttoolsweb); tools.add(tmacro);
tools.add(ttemplates); tools.add(tautocorrect); tools.add(tcustomize);
tools.add(toptions);
}
public void tableMenu(){
table.add(tbdraw); table.addSeparator(); table.add(tbinsert); table.add(tbdelete);
table.add(tbselect); table.add(tbmerge);
table.add(tbsplitcell); table.add(tbsplittable); table.addSeparator();
table.add(tbautoformat); table.add(tbautofit); table.add(tbheading); table.addSeparator();
table.add(tbconvert); table.add(tbsort); table.add(tbformula);
table.add(tbhidegrid); table.addSeparator(); table.add(tbproperties);
}
public void windowMenu(){
window.add(wnew); window.add(warrange); window.add(wsplit);
window.addSeparator();
}
public void helpMenu(){
help.add(hmsword); help.add(hoffassist);
help.addSeparator(); help.add(hwhats);
help.add(hoffice); help.add(hactivate);
help.add(hwperfect); help.addSeparator();
help.add(hdetect); help.addSeparator();
help.add(habout);
}
}

DOS Chat -- Java Networking II

Save the programs in a folder.

compile both programs

copy the class files to different computers if necessary

execute the Server first (java Server)

execute the Client by passing a parameter as the ipaddress of the computer that has the Server running on it .(eg. java Client 10.0.1.13)
The Chat continues until either side sends "bye" in the message.
Client.java
----------------------------------------------------------------
import java.net.*;
import java.io.*;

public class Client
{
public static void main(String args[]) throws IOException
{
int PORT = 8134;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";

InetAddress host = InetAddress.getLocalHost();
String diffHost = args[0];
Socket sock = new Socket(diffHost,PORT);
System.out.println("Chat Client Started");
do{
System.out.println("Enter your message here: ");
DataInputStream dis = new DataInputStream(System.in);
message = dis.readLine();
outStream = sock.getOutputStream();
outDataStream = new DataOutputStream (outStream);
outDataStream.writeUTF(message);

inStream = sock.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("Server Sent: "+message);
}while(!message.equals("bye"));
}
}
----------------------------------------------------------------
Server.java
----------------------------------------------------------------
import java.io.*;
import java.net.*;

public class Server
{
public static void main(String[] args) throws IOException
{
int PORT = 8134;
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message="";
String received="";

System.out.println("Chat Server Started");

ServerSocket sock = new ServerSocket(PORT);
Socket conn = sock.accept();
do{
inStream = conn.getInputStream ();
inDataStream = new DataInputStream ( inStream );
message = inDataStream.readUTF();
System.out.println("Client sent: "+message);

DataInputStream dis = new DataInputStream(System.in);
message = dis.readLine();
outStream = conn.getOutputStream();
outDataStream = new DataOutputStream (outStream);
System.out.println("Enter your message here: ");
outDataStream.writeUTF(message);
}while(!message.equals("bye"));
conn.close();
}
}
----------------------------------------------------------------

DOS Chat -- Java Networking

Client.java
-----------------------------------------------------------------
/***********************************
Author: Rajneshwar Prasad
***********************************/

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

class Client
{ public static void main(String a[]) throws IOException
{ // Specify your server with a url or an IP address.
// if you try this on your system, insert your system name
// such as "pc107" in the server string instead of loopback.
String loopback = "127.0.0.1"; // "localhost" will also work.
String server = loopback;
// Open our connection to the server, at port 4444
Socket sock = new Socket(server,4444);

// Get I/O streams from the socket
BufferedReader dis = new BufferedReader(new InputStreamReader( sock.getInputStream() ));
PrintStream dat = new PrintStream(sock.getOutputStream() );

// Now we can just read from and write to the streams.
// Start with reading for this server.
String fromServer = dis.readLine();
System.out.println("Got this from server: " + fromServer);

String myReply = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
myReply = in.readLine();
dat.println(myReply);

fromServer = dis.readLine();
System.out.println("More from Server: "+ fromServer);

// All done. Close the connection.
sock.close();
}
}

-----------------------------------------------------------------
Server.java
-----------------------------------------------------------------
/***********************************
Author: Rajneshwar Prasad
***********************************/
import java.io.*;
import java.net.*;

public class Server
{ public static void main(String a[]) throws IOException
{ int port = 4444;
Socket client = null;
// Next we create the server socket that we will listen to.
ServerSocket servsock = new ServerSocket(port);

String query = "If you met me would you shake my hand, or sniff it?";

while (true)
{ // Wait for the next client connection
client = servsock.accept();
// Create the input and output streams for our communication.
PrintStream out = new PrintStream( client.getOutputStream() );
BufferedReader in = new BufferedReader(new InputStreamReader( client.getInputStream()));
// Now you can just write to and read from these streams.

// Send our query
out.println(query); out.flush();

// get the reply
String reply = in.readLine();
if (reply.indexOf("sniff") > -1)
{ System.out.println("On the Internet I know this is a DOG!");
out.println("You're a dog.");
}
else
{ System.out.println("Probably a person or an AI experiment");
out.println("You're a person or something.");
}
out.flush();

// All done. Close this connection
client.close();
}
}
}
-----------------------------------------------------------------

MicroVillage Application in RMI

What MicroVillage Does?



Rajneshwar Prasad




Untitled Document

Aim

To overcome the problems that are faced by the public when buying tickets i.e. by introducing multi-customer service at multiple counters.

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

MicroVillage

What MicroVillage do?

There is a server and several clients.


Server acts as the counter at Village.
Client comes and chooses the server name(computer name).





By clicking the button "Check for Movies", the client is connected to the counter (server).

The server then accepts the call and returns to the client the movies available on that day. (The

Listbox is populated with the movies).


The client then chooses the movie and the server then updates the "Time" textfield.
Client after choosing the movie enters the age and clicks on the button "BUY".
Server gets the age from the

client and checks in the database if client qualifies for the selected

movie. If he does he is prompted for that in the

"Message to Notify" textarea.If he/she does not then the user is prompted for that:







For receipts the "Print Your Receipt" button is pressed and the receipt containing the cinema name, movie name, price and other details are printed out.

If the number of empty seats are zero then a hou

se fu

ll message is prompted















------------------------------------------------------------------------------------------------
MicroVillage

Steps for running MicroVillage…


Set the Data Source Driver from control
panel. Name it as "Village".

Database attached is named as "Village.mdb"













---------------------------------------------------------------------------------------------
For source codes and Database please do e-mail me: rajneshwar@gmail.com

RMI Calculator

DOS Based Application which sends numbers to the RMI Server which calculates and sends the answer to the client. (Note: Client and Servers can be anywhere across the network). Find the codes below. It is a simple application showing how RMI(Remote Method Invocation) can be implemented.
Calculator.java
-----------------------------------------------------------------------------
import java.rmi.*;
public interface Calculator extends Remote
{
public int sum(int num1, int num2)throws RemoteException;
}
-----------------------------------------------------------------------------
CalClient.java
-----------------------------------------------------------------------------
import java.rmi.*;

public class CalClient
{
public static void main(String[] args)
{
try{
String url= "rmi://"+args[0]+"/Calculator";
Calculator cal = (Calculator)Naming.lookup(url);
int add = cal.sum(Integer.parseInt(args[1]),Integer.parseInt(args[2]));
System.out.println("The sum is: "+add);
}catch(Exception e){e.printStackTrace();}
}
}
-----------------------------------------------------------------------------
CalServer.java
-----------------------------------------------------------------------------
import java.rmi.*;
import java.rmi.server.*;

public class CalServer extends UnicastRemoteObject implements Calculator
{
public CalServer() throws RemoteException{}
public int sum(int num1, int num2)throws RemoteException
{
return (num1+num2);
}
public static void main(String[] args)
{
try{
CalServer cal = new CalServer();
Naming.bind("Calculator",cal);
System.out.println("Calculator Started ");
}catch(Exception e){e.printStackTrace();}
}
}
-----------------------------------------------------------------------------