CHAT PROGRAM USING JAPPLET.

 My problem is that I have developed a chat program using RMI and Swings by defining a 
ClientInterface which will be having setMessage(String s) method which will be called 
by server ,and ServerInterface which will be having three methods 
login(ClientInterface client,String name),logout(String str,String 
name),sendMessage(String name),and server program and a client . It is working 
properly when the client is a normal user(frame), But I�d like to have a Client 
program if it is Japplet.

 My ClientInterface :

 import java.io.*; import java.rmi.*; public interface ClientInterface extends Remote 
{ public void setMessage(String message) throws RemoteException; }

 My ServerInterface : 

import java.rmi.*; public interface ServerInterface extends Remote { public void 
login(ClientInterface client,String name) throws RemoteException; public void 
sendMessage(String str,String name) throws RemoteException; public void logout(String 
name) throws RemoteException; } 

Server : 

import java.rmi.server.*; import java.rmi.*; import java.io.*; import java.util.*; 
public class Server extends UnicastRemoteObject implements ServerInterface { private 
Hashtable chatters=new Hashtable(); public Server() throws RemoteException { super(); 
} public void login(ClientInterface client,String name)throws RemoteException { 
chatters.put(name,client); System.out.println(name +" is logged in"); } public void 
sendMessage(String message,String name)throws RemoteException { Enumeration 
part=chatters.keys(); while(part.hasMoreElements()) { String str=null; try { 
str=(String)part.nextElement(); ClientInterface 
client=(ClientInterface)chatters.get(str); client.setMessage(name+":"+ message); } 
catch(Exception ex) { System.out.println("Error in sending message to "+str); 
chatters.remove(str); } } } public void logout(String name)throws RemoteException { 
chatters.remove(name); System.out.println(name+" : LoggedOut"); } public static void 
main(String arh[]) { try { Server server=new Server(); Naming.rebind("Chat",server); 
System.out.println("Chat server is ready"); } catch(Exception e) { 
System.out.println("error"+e); } } } 

This is my Client Program using Frames,any enhancements to Japplet are welcome. 

Client :

 import java.awt.*; import java.awt.event.*; import java.rmi.server.*; import 
javax.swing.*; import java.rmi.*; public class ChatClient extends javax.swing.JFrame 
implements ClientInterface { String name; ServerInterface server; public ChatClient() 
{ try { UnicastRemoteObject.exportObject(this); 
server=(ServerInterface)Naming.lookup("rmi://wks10/Chat"); } catch(Exception e) { 
System.out.println("problem in login "+e); } setTitle("Client"); 
setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE); 
getContentPane().setLayout(new BorderLayout(0,0)); JPanel jp2=new JPanel(); 
jp2.add(new JLabel("Message")); jp2.add(tfText); 
getContentPane().add(BorderLayout.NORTH,jp2); jp1.setLayout(new BorderLayout(0,0)); 
getContentPane().add(BorderLayout.CENTER,jp1); jp1.setBounds(0,0,488,274); 
jsp1.setOpaque(true); jp1.add(BorderLayout.CENTER,jsp1); jsp1.setBounds(0,0,485,274); 
tam.setDisabledTextColor(new java.awt.Color(153,153,153)); 
jsp1.getViewport().add(tam); tam.setBounds(0,0,485,271); tam.setEditable(false); 
tfText.setEditable(false); jp2.setLayout(new FlowLayout(FlowLayout.CENTER,5,5)); 
getContentPane().add(BorderLayout.SOUTH,jp2); jp2.setBounds(0,274,488,35); 
jb1.setText("LogIn"); jb3.setText("LogOut"); jb1.setActionCommand("LogIn"); 
jp2.add(jb1); jb1.setBounds(132,5,75,25); jb2.setText("send"); jb2.setEnabled(false); 
jb3.setEnabled(false); jb2.addActionListener(new ActionListener() { public void 
actionPerformed(ActionEvent ae) { try { server.sendMessage(tfText.getText(),name); 
tfText.setText(""); tfText.requestFocus(); } catch(Exception e) { 
JOptionPane.showMessageDialog(ChatClient.this,"Error in sending"); 
System.out.println("Error in sending "+e.getMessage()); } } }); 
jb3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent 
ae) { try { server.logout(name); System.exit(0); } catch(Exception e) { 
JOptionPane.showMessageDialog(ChatClient.this,"Error in logout"); } } }); 
jb2.setActionCommand("Send"); jp2.add(jb2); jb2.setBounds(212,5,63,25); 
jb2.setText("send"); jb3.setActionCommand("LogOut"); jp2.add(jb3); 
jb3.setBounds(280,5,75,25); jb1.addActionListener(new ActionListener() { public void 
actionPerformed(ActionEvent ae) { String 
str=JOptionPane.showInputDialog(ChatClient.this,"Enter your Name"); name=str; 
ChatClient.this.setTitle(name); try { server.login(ChatClient.this,name); 
jb2.setEnabled(true); jb3.setEnabled(true); jb1.setEnabled(false); 
tfText.setEditable(true); } catch(Exception e) { 
JOptionPane.showMessageDialog(ChatClient.this,"Error in login"); 
System.out.println("Error "+e.getMessage()); } } }); setSize(600,300); 
setVisible(true); getRootPane().setDefaultButton(jb1); jb1.requestFocus(); } public 
static void main(String jk[]) { new ChatClient(); } javax.swing.JTextField tfText=new 
JTextField(20); javax.swing.JPanel jp1=new javax.swing.JPanel(); 
javax.swing.JScrollPane jsp1=new javax.swing.JScrollPane(); javax.swing.JTextArea 
tam=new javax.swing.JTextArea(); javax.swing.JPanel jp2=new javax.swing.JPanel(); 
javax.swing.JButton jb1=new javax.swing.JButton(); javax.swing.JButton jb2=new 
javax.swing.JButton(); javax.swing.JButton jb3=new javax.swing.JButton(); public void 
setMessage(String message)throws RemoteException { tam.append(message+"\n"); } }

 Any suggestions as to how I can write a client program in Japplet are welcome. 



_____________________________________________________
Chat with your friends as soon as they come online. Get Rediff Bol at
http://bol.rediff.com




_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to