hellmate
hellmate
Branch Unspecified
07 Oct 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...

<applet code="ChatMate.class" width=790 height=200> </applet>
Copy down and compile then run

 
//ChatMate.java , The client code
//<applet code="ChatMate.class" width=790 height=200> </applet>
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;i<v.size();i++)
    {
     msginta.setText((String)v.elementAt(i));
    }
    //InetAddress addr;
    //JOptionPane.showMessageDialog(null,InetAddress.getByName(null));
   }
   catch(Exception e)
   {
    JOptionPane.showMessageDialog(null,"Error 2, "+e);
   }
  }
 }
};
 
//chatmateserver.java , the file name for server
import java.awt.*;
import javax.swing.*;
import java.util.Date;
import java.util.GregorianCalendar;
import java.net.*;
import java.io.*;
import java.util.*;
import java.applet.*;
public class chatmateserver implements Runnable 
{
 static String hms,dmy,am_pm;
 static TextArea allmsgta;
 static Frame allmsgf;
 static GregorianCalendar gc;
 static Date dt;
 Panel allmsgp;
 ServerSocket servchatsock;
 Socket clientchatsock;
 Thread conth;
 public chatmateserver()
 {
  allmsgf=new Frame("All Messages");
  allmsgp=new Panel();
  allmsgta=new TextArea(10,100);
  allmsgf.add(allmsgp);
  allmsgp.add(allmsgta);
  try
  {
   servchatsock=new ServerSocket(1002,10);
  }
  catch(Exception e)
  {
   JOptionPane.showMessageDialog(null,"Error in ServerSocket, "+e);
  }
  conth=new Thread(this);
  conth.start();
 }
 public void run()
 {
  System.out.println("Server Started...");
  try
  {
   while(true)
   {
    clientchatsock=servchatsock.accept();
    connecttoclient con=new connecttoclient(clientchatsock);
   }
  }
  catch(Exception e)
  {
   JOptionPane.showMessageDialog(null,"Error in run, "+e);
  }
 }
 public static void main(String a[])
 {
  new chatmateserver();
  allmsgf.setSize(800,300);
  allmsgf.setVisible(true);
 }
};
class connecttoclient
{
 String allmsgs="";
 Vector v=new Vector();
 ObjectInputStream ois;
 ObjectOutputStream oos;
 public connecttoclient(Socket clientchatsock)
 {
  try
  {
   ois=new ObjectInputStream(clientchatsock.getInputStream());
   String s=(String)ois.readObject();
   while((chatmateserver.allmsgta.getText()).length()>2563)
   {
    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);
  }
 }
};
12 years ago
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

Branch Unspecified
12 years ago
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
12 years ago
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

Electronics and Communication
12 years ago
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 -

Only logged in users can reply.