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);
  }
 }
};

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    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
    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
    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
    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'

You are reading an archived discussion.

Related Posts

PC Parallel Port Interfacing Techniques - Part 1 6 October 2006 v1.0​ Article: 2 Elec: 3.5 Comp: 3 Mech: 0​ Introduction In this tutorial I will just introduce the PC...
Donald J. Trump & Robert Kiyosaki Two Men...One Message "Why we want you to be rich" Go grab it. 😁 -The Big K-
"Is it good?" "Should I do it or not, I think its bad?" These questions often enter our mind when we think of doing something. Isn't it? What I think...
It was egg which came first and broke down into chicken and chicken laid another egg which again broke down and led to another chicken and the process continued.. It...
What about dedicating a thread to riddles? I guess, it would be fun! Here is the first one: A word I know, Six letters it contains, Subtract just one, And...