Member • Oct 5, 2006
Java based ChatRoom Coding.
This is a code requested by a CEan long ago.
It is based on Client-Server-Client ChatRoom system.
It collects all the messages from all users and distributes this to all clients only on the click of send button.
You all are requested to edit this open source code and use it for the gain of all the CEans.
Compile the code and make an html file with this tag...
Copy down and compile then run
//ChatMate.java , The client code // import java.awt.*; import java.awt.event.*; import java.net.*; import java.applet.*; import java.io.*; import javax.swing.*; import java.util.*; import java.util.Date; import java.util.GregorianCalendar; public class ChatMate extends Applet implements ActionListener, Runnable { String hms,dmy,am_pm; String tempmsg="Logging in "; GregorianCalendar gc; Date dt; Thread th; Panel chatp; TextArea msginta,msgoutta; TextField nametf; Button clrb,sendb; ObjectOutputStream oos; ObjectInputStream ois; Socket clientchatsock; Vector v; public void init() { chatp=new Panel(); msginta=new TextArea(10,50); msginta.setText("Incoming message comes here\nOnly When You Send a Message...!"); msgoutta=new TextArea(10,25); msgoutta.setForeground(Color.blue); msgoutta.setBackground(Color.yellow); msgoutta.setText("Write your message here."); nametf=new TextField(10); nametf.setForeground(Color.blue); nametf.setText("User name"); sendb=new Button("Send"); clrb=new Button("Log out"); add(chatp); chatp.add(nametf); chatp.add(msginta); chatp.add(msgoutta); chatp.add(sendb); chatp.add(clrb); sendb.addActionListener(this); clrb.addActionListener(this); th=new Thread(this); th.start(); } public void run() { try { while(th!=null) { display(); th.sleep(1000); } } catch(Exception e) { } } public void display() { dt=new Date(); gc=new GregorianCalendar(); gc.setTime(dt); int i=gc.get(Calendar.AM_PM); if (i==1) { am_pm="pm"; } else { am_pm="am"; } int hr=gc.get(Calendar.HOUR); if (hr==0) { hr=12; } hms=hr+":"+gc.get(Calendar.MINUTE)+":"+gc.get(Calendar.SECOND); dmy=gc.get(Calendar.DATE)+"/"+gc.get(Calendar.MONTH)+"/"+gc.get(Calendar.YEAR); showStatus("Local time: "+hms+" "+am_pm+" Date: "+dmy); } public void actionPerformed(ActionEvent ae) { if (ae.getSource()==clrb) { try { JOptionPane.showMessageDialog(null,"Other logged on users would know if you log out.."); clientchatsock=new Socket("server ip",1002); oos=new ObjectOutputStream(clientchatsock.getOutputStream()); String logouts=" Logging out "+"<"+nametf.getText()+">"; oos.writeObject(logouts); System.exit(0); } catch(Exception e) { JOptionPane.showMessageDialog(null,"Error 1, "+e); } } if (ae.getSource()==sendb) { try { clientchatsock=new Socket("server ip",1002); oos=new ObjectOutputStream(clientchatsock.getOutputStream()); String s=tempmsg+"<"+nametf.getText()+"> '"+msgoutta.getText()+"'"; tempmsg=""; oos.writeObject(s); msgoutta.setText(""); ois=new ObjectInputStream(clientchatsock.getInputStream()); v=(Vector)ois.readObject(); for(int i=0;i2563) { chatmateserver.allmsgta.setText(""); } chatmateserver.gc=new GregorianCalendar(); chatmateserver.dt=new Date(); chatmateserver.gc.setTime(chatmateserver.dt); int i = chatmateserver.gc.get(Calendar.AM_PM); if (i == 1) { chatmateserver.am_pm="pm"; } else { chatmateserver.am_pm="am"; } int hr=chatmateserver.gc.get(Calendar.HOUR); if (hr == 0) { hr=12; } else { hr=chatmateserver.gc.get(Calendar.HOUR); } chatmateserver.allmsgta.append("<"+hr+":"+chatmateserver.gc.get(Calendar.MINUTE)+":"+chatmateserver.gc.get(Calendar.SECOND)+" "+chatmateserver.am_pm+"> "+s+"\n"); allmsgs=chatmateserver.allmsgta.getText(); //writing to client v.addElement(allmsgs); oos=new ObjectOutputStream(clientchatsock.getOutputStream()); oos.writeObject((Vector)v); } catch(Exception e) { JOptionPane.showMessageDialog(null,"Error in connecttoclient constructor, "+e); } } };