View Feed
group-icon
Java Developers
Community of Java Developers: Get Java Programming Help from fellow Java Developers across the world.
562 Members
Join this group to post and comment.
hellmate
hellmate • 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);
  }
 }
};
Kaustubh Katdare
Kaustubh Katdare • Oct 5, 2006
Good Start, Hellmate!

Good start, Hellmate 😁 !

CEans!

Now can we have discussions on improving the code & adding features to it? If you have suggestions/feedback on the code, feel free to post them in this thread!

-The Big K-
instruite
instruite • Oct 5, 2006
will try to test the code later when at home

but just for your information
there are few GPL based chat softwares like
phpchat, spchat, flash chat and some others which can be integrated with vbulletin
Kaustubh Katdare
Kaustubh Katdare • Oct 5, 2006
I am aware of chat system which can be integrated with vBulletin. We will look forward to having a chat system integrated with CrazyEngineers, but there are no immediate plans for the same.

Let us see how far we can go with this 'base' code. Bring in your optimization to this code. Test is on your machine, make sure its working and then post it on this thread. I'm sure we can do a great job with the ChatRoom code.

Do enclose your code in "code" tags ( the # button in the message compose box does the same )

-The Big K-
xheavenlyx
xheavenlyx • Oct 7, 2006
Im having a problem with my Java Virtual Machine. God its really irritating! Dont know how to fix it too. Some error on 'console not found'

Share this content on your social channels -