473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Chat application program

Hi,
Has anyone ever created a chat application using java. Ive made a one but that works only for 1 to 1 communication between the client and the server. not more than 2 people can communicate with one another.
Plz help me out its urgent.
Feb 25 '08 #1
4 2853
MarkoKlacar
296 Expert 100+
Hi,

Are you using sockets and serversockets?
Feb 27 '08 #2
JosAH
11,448 Expert 8TB
If you are using Sockets and a ServerSocket as the previous poster asked, let
the ServerSocket accept a new connection (you'll get a new Socket then) and
start up a new Thread using that Socket that handles the communication with
the new client. The first thread simply loops, accepting new connections. The
newly created Threads handle the client's communication. When the client
disappears the Thread simply dies.

If you want to go fancy you can preallocate a fixed 'n' sized pool of client handling
Threads; if no more Threads are available in that pool you have 'n' clients chatting
simultaneously. In this simple scenario Threads are waiting in the pool for a new
client and when the client disappears the Thread doesn't die but returns itself
to the pool again where it waits until a new client signs on and the cycle repeats.

kind regards,

Jos
Feb 27 '08 #3
Hi,
Thanks. Im using socket and serversocket only. But i tried a lot it doesnt works. I want that clients should be able to talk to whichever client they want. Im pasting my code for both client and server. Plz check it out and do the changes wherever it is wrong and let me know.

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * Main.java for CLIENT
  3.  *
  4.  * Created on February 16, 2007, 4:24 PM
  5.  */
  6.  
  7. import java.net.*;
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11.  
  12. public class Main
  13. {
  14.     static TextField tf = new TextField();
  15.     static TextArea ta = new TextArea();  
  16.     static Button b1 = new Button("SEND");
  17.     static Button b2 = new Button("CLEAR");
  18.  
  19.     public Main()
  20.     {
  21.     }
  22.  
  23.      public static void main(String[] args)throws Exception
  24.     {      
  25.         Frame fr = new Frame();
  26.         Panel p1 = new Panel();
  27.  
  28.         p1.add(b1);
  29.         p1.add(b2);
  30.         b1.setEnabled(false);
  31.         fr.add(p1,BorderLayout.SOUTH);
  32.         fr.add(ta,BorderLayout.NORTH);
  33.         fr.add(tf,BorderLayout.CENTER); 
  34.         fr.setSize(300,300);
  35.         fr.setTitle("CLIENT");
  36.         fr.setVisible(true);      
  37.  
  38.         InetAddress ip = InetAddress.getByName("127.0.0.1");
  39.         Socket soc = new Socket(ip,9090);
  40.         System.out.println("CLIENT ON");
  41.  
  42.         SendData sd1 = new SendData();
  43.         sd1.SetSoc(soc);
  44.         b1.addActionListener(sd1);
  45.         System.out.println("CLIENT CONNECTION ESTABLISHED"); 
  46.  
  47.         TClient t = new TClient();
  48.         t.SetSoc(soc);
  49.         t.start();
  50.  
  51.         WindowCheck wc1  = new WindowCheck();
  52.         fr.addWindowListener(wc1);    
  53.  
  54.         Clear cl = new Clear();
  55.         b2.addActionListener(cl);
  56.  
  57.         Datatyped b = new Datatyped();
  58.         tf.addKeyListener(b);  
  59.  
  60.         //soc.close();                     
  61.    }     
  62. }
  63.  
  64.  
  65. /*
  66.  * SendData.java , Class for ActionListener for Send button in CLIENT
  67.  *
  68.  * Created on February 16, 2007, 12:54 PM
  69.   */
  70.  
  71. class SendData implements ActionListener
  72. {   
  73.     private Socket soc;
  74.     private PrintWriter out;
  75.     String str = new String(); 
  76.  
  77.     public SendData()
  78.     {
  79.     }
  80.  
  81.     public void actionPerformed(ActionEvent e)
  82.     {    
  83.         try
  84.         {
  85.             OutputStream i = soc.getOutputStream();
  86.             OutputStreamWriter j = new OutputStreamWriter(i);
  87.             BufferedWriter k = new BufferedWriter(j);
  88.             PrintWriter out = new PrintWriter(k,true);
  89.  
  90.             str =Main.tf.getText();
  91.             Main.ta.append("CLIENT:" + str + "\n");
  92.             Main.tf.setText("");
  93.  
  94.             System.out.println("CLIENT:" + str);                  
  95.             out.println(str);  
  96.         } 
  97.  
  98.         catch (IOException ex)
  99.         {
  100.             ex.printStackTrace();
  101.         }                
  102.     }    
  103.  
  104.      public void SetSoc(Socket soc) throws Exception
  105.      {
  106.         this.soc = soc; 
  107.      }           
  108. }
  109.  
  110.  
  111. /*
  112.  * TClient.java for CLIENT
  113.  *
  114.  * Created on February 16, 2007, 4:24 PM
  115.  */
  116.  
  117. class TClient extends Thread
  118. {
  119.  
  120.     private Socket soc;         //Cannot be made static else each 
  121.     private BufferedReader in;  //Client will not get separate socket    
  122.  
  123.     public TClient()
  124.     {
  125.     }
  126.  
  127.     public void run()
  128.     {
  129.         try
  130.         {
  131.            BufferedReader in =   new BufferedReader(
  132.                                  new InputStreamReader(
  133.                                  soc.getInputStream()));
  134.  
  135.  
  136.              String Str = in.readLine();
  137.              while(true)
  138.              {
  139.              System.out.println(" FROM SERVER : " + Str);
  140.              Main.ta.append("SERVER : " + Str + "\n");
  141.              Str = in.readLine();
  142.              }
  143.  
  144.         }
  145.  
  146.         catch (IOException ex)
  147.         {
  148.             ex.printStackTrace();
  149.         }
  150. }
  151.  
  152.      public void SetSoc(Socket soc) throws Exception
  153.      {
  154.         this.soc = soc; 
  155.      }  
  156.  
  157. }
  158.  
  159.  
  160. /*
  161.  * Datatyped.java for CLIENT
  162.  *
  163.  * Created on February 15, 2007, 11:25 PM
  164.  */
  165.  
  166. class Datatyped implements KeyListener
  167. {
  168.  
  169.     public void keyTyped(KeyEvent e)
  170.     {
  171.  
  172.     }
  173.  
  174.     public void keyPressed(KeyEvent e)
  175.     {
  176.          Main.b1.setEnabled(true);
  177.     }
  178.  
  179.     public void keyReleased(KeyEvent e)
  180.     {
  181.     }
  182.  
  183. }
  184.  
  185.  
  186. /*
  187.  * Clear.java , Class for ActionListener for Clear button in CLIENT
  188.  *
  189.  * Created on February 20, 2007, 9:32 AM
  190.  */
  191.  
  192. class Clear implements ActionListener
  193. {
  194.     public Clear()
  195.     {
  196.     }
  197.  
  198.     public void actionPerformed(ActionEvent e)
  199.     {
  200.         Main.ta.setText("");
  201.     } 
  202. }
  203.  
  204.  
  205. /*
  206.  * WindowCheck.java  for CLIENT
  207.  *
  208.  * Created on February 16, 2007, 12:31 PM
  209.  */
  210.  
  211. class WindowCheck implements WindowListener
  212. {
  213.  
  214.      public WindowCheck()
  215.     {
  216.     }
  217.  
  218.     public void windowOpened(WindowEvent e)
  219.     {
  220.     }
  221.  
  222.     public void windowClosing(WindowEvent e)
  223.     {
  224.         System.exit(0);
  225.     }
  226.  
  227.     public void windowClosed(WindowEvent e)
  228.     {
  229.     }
  230.  
  231.     public void windowIconified(WindowEvent e)
  232.     {
  233.     }
  234.  
  235.     public void windowDeiconified(WindowEvent e)
  236.     {
  237.     }
  238.  
  239.     public void windowActivated(WindowEvent e)
  240.     {
  241.     }
  242.  
  243.     public void windowDeactivated(WindowEvent e)
  244.     {
  245.     }
  246.  
  247. }
  248.  
code for server
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * Main.java for SERVER
  3.  *
  4.  * Created on February 16, 2007, 4:24 PM
  5.  */
  6.  
  7. import java.net.*;
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11.  
  12. public class Main
  13. {
  14.     static TextField tf = new TextField();
  15.     static TextArea ta = new TextArea();  
  16.     static Button b1 = new Button("SEND");
  17.     static Button b2 = new Button("CLEAR");
  18.  
  19.     public Main()
  20.     {
  21.     }
  22.  
  23.     public static void main(String[] args) throws Exception
  24.     {
  25.         Frame fr = new Frame();
  26.         Panel p1 = new Panel();
  27.  
  28.         p1.add(b1);
  29.         p1.add(b2);
  30.         b1.setEnabled(false);
  31.         fr.add(p1,BorderLayout.SOUTH);
  32.         fr.add(ta,BorderLayout.NORTH);
  33.         fr.add(tf,BorderLayout.CENTER); 
  34.         fr.setSize(500,300);
  35.         fr.setTitle("SERVER");
  36.         fr.setVisible(true);      
  37.  
  38.         System.out.println("SERVER ON");
  39.         InetAddress ip = InetAddress.getByName("127.0.0.1");
  40.         ServerSocket ss = new ServerSocket(9090);
  41.         Socket soc = ss.accept();
  42.  
  43.         TServer t1 = new TServer();
  44.         t1.SetSoc(soc);
  45.         t1.start();
  46.         System.out.println("CLIENT CONNECTED TO SERVER ");    
  47.  
  48.         SendData ssd = new SendData();
  49.         ssd.SetSoc(soc);
  50.         b1.addActionListener(ssd);
  51.  
  52.         Clear cl = new Clear();
  53.         b2.addActionListener(cl);
  54.  
  55.         WindowCheck wc1  = new WindowCheck();
  56.         fr.addWindowListener(wc1);               
  57.  
  58.         Datatyped b = new Datatyped();
  59.         tf.addKeyListener(b);  
  60. //        soc.close();   
  61. //        ss.close();     
  62.      }
  63. }
  64.  
  65.  
  66. /*
  67.  * SendData.java for SERVER
  68.  *
  69.  * Created on February 16, 2007, 4:24 PM
  70.  */
  71.  
  72. class SendData implements ActionListener
  73. {
  74.     private Socket soc;
  75.     private PrintWriter out;
  76.     String str = new String(); 
  77.  
  78.     public SendData()
  79.     {
  80.     }
  81.  
  82.     public void actionPerformed(ActionEvent e)
  83.     {    
  84.         try
  85.         {
  86.             OutputStream i = soc.getOutputStream();
  87.             OutputStreamWriter j = new OutputStreamWriter(i);
  88.             BufferedWriter k = new BufferedWriter(j);
  89.             PrintWriter out = new PrintWriter(k,true);
  90.  
  91.             str =Main.tf.getText();
  92.             Main.ta.append("SERVER:" + str + "\n");
  93.             Main.tf.setText("");
  94.  
  95.             System.out.println("SERVER:" + str);                  
  96.             out.println(str);              
  97.  
  98.         } 
  99.  
  100.         catch (IOException ex)
  101.         {
  102.             ex.printStackTrace();
  103.         }           
  104.  
  105.     }    
  106.  
  107.      public void SetSoc(Socket soc) throws Exception
  108.      {
  109.         this.soc = soc; 
  110.      }
  111. }
  112.  
  113.  
  114. /*
  115.  * TServer.java for SERVER
  116.  *
  117.  * Created on February 16, 2007, 4:24 PM
  118.  */
  119.  
  120. class TServer extends Thread 
  121. {
  122.     private Socket soc;       //Cannot be made static else each 
  123.     private PrintWriter out;  //client will not get separate socket
  124.     private BufferedReader in;
  125.  
  126.     public TServer() throws Exception
  127.     {
  128.  
  129.     }
  130.  
  131.     public void run()  
  132.     {
  133.         try
  134.         {  
  135.  
  136.             BufferedReader in =   new BufferedReader(
  137.                               new InputStreamReader(
  138.                               soc.getInputStream()));
  139.  
  140.             PrintWriter out  = new PrintWriter(
  141.                            new BufferedWriter(
  142.                            new OutputStreamWriter(
  143.                            soc.getOutputStream())),true);   
  144.  
  145.             String Str = in.readLine();
  146.             while(true)
  147.             {
  148.                 System.out.println(" FROM CLIENT : " + Str);
  149.                 Main.ta.append("CLIENT : " + Str + "\n");
  150.                 Str = in.readLine();
  151.             }    
  152.         }                           
  153.         catch (Exception ex)
  154.         {
  155.             ex.printStackTrace();
  156.         }                                   
  157.     }  
  158.  
  159.    public void SetSoc(Socket soc) throws Exception
  160.    {   
  161.         System.out.println("CLIENT SOCKET CONNECTED");
  162.         this.soc = soc;    
  163.     }
  164.  }
  165.  
  166.  
  167. /*
  168.  * Datatyped.java for SERVER
  169.  *
  170.  * Created on February 15, 2007, 11:25 PM
  171.  */
  172.  
  173. class Datatyped implements KeyListener
  174. {
  175.  
  176.     public void keyTyped(KeyEvent e)
  177.     {
  178.  
  179.     }
  180.  
  181.     public void keyPressed(KeyEvent e)
  182.     {
  183.          Main.b1.setEnabled(true);
  184.     }
  185.  
  186.     public void keyReleased(KeyEvent e)
  187.     {
  188.     }
  189.  
  190. }
  191.  
  192.  
  193. /*
  194.  * Clear.java , Class for ActionListener for Clear button in SERVER
  195.  *
  196.  * Created on February 20, 2007, 9:23 AM
  197.  */
  198.  
  199. class Clear implements ActionListener
  200. {
  201.     public Clear()
  202.     {
  203.     }
  204.  
  205.     public void actionPerformed(ActionEvent e)
  206.     {
  207.         Main.ta.setText("");
  208.     }
  209. }
  210.  
  211.  
  212. /*
  213.  * WindowCheck.java for SERVER
  214.  *
  215.  * Created on February 16, 2007, 4:24 PM
  216.  */
  217.  
  218. class WindowCheck implements WindowListener
  219. {
  220.  
  221.     public WindowCheck()
  222.     {
  223.     }
  224.  
  225.     public void windowOpened(WindowEvent e)
  226.     {
  227.     }
  228.  
  229.     public void windowClosing(WindowEvent e)
  230.     {
  231.         System.exit(0);
  232.     }
  233.  
  234.     public void windowClosed(WindowEvent e)
  235.     {
  236.     }
  237.  
  238.     public void windowIconified(WindowEvent e)
  239.     {
  240.     }
  241.  
  242.     public void windowDeiconified(WindowEvent e)
  243.     {
  244.     }
  245.  
  246.     public void windowActivated(WindowEvent e)
  247.     {
  248.     }
  249.  
  250.     public void windowDeactivated(WindowEvent e)
  251.     {
  252.     }
  253.  
  254. }
  255.  
  256.  

Plz helpme out. Thanks again
Feb 27 '08 #4
chaarmann
785 Expert 512MB
Hi,
Has anyone ever created a chat application using java. Ive made a one but that works only for 1 to 1 communication between the client and the server. not more than 2 people can communicate with one another.
Plz help me out its urgent.
I created one recently, which works with AJAX in a browser.
Are you using more than one server or more than one JVM on one server?
If yes, you need to synchronize the messages from users on different JVMs through an EJB (or the slower way: through a database) or write something of your own with Sockets or RMI.
If no, you can just save the messages altogether in a hashmap which is declared static.

If you use sockets, you should have one message-server where all clients connect to. So clients are not connected between each other, but connect to the same message server. Then clients can add messages to the server, or they poll the server for new messages.
Clients are storing the last message number they got. So they will only get newer messages (=messages with higer number) when they call code like
Expand|Select|Wrap|Line Numbers
  1. void poll()
  2. {
  3.    Messages newMessages = server.getNewMessages(messageNumber);
  4.    display(newMessages);
  5.    messageNumber += newMessages.getSize();
  6. }
You might think of programming that the server keeps track of each last message sent over to each client, but that will create much more work for you, because then you have to care for cleanup of disconnected clients etc.
Feb 27 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sangeetha | last post by:
Hello, I've assigned to develop a simple chat application using "C" program. I've written program but missing one thing. Simultaneously both user can type in their window. But i dont know how to...
2
by: mark | last post by:
What's the logical way to build a .NET based chat program? .NET Remoting? MSMQ? Something else? Thanks in advance. Mark
3
by: John Wright | last post by:
I am trying to create a very simple chat program using UDP through a web page. I am having problems getting this going. All the examples I've seen deal with remoting or application variables, or...
3
by: A | last post by:
Hi all! I am currently working on a project where I need to create a web-based chat application that will be used for auctions. If possible, could I get some advice on how to create chat...
3
by: Sven | last post by:
My current application is programmed in Visual Basic 6.0 It is a server and uses the winsock-control Now I have learned that the winsock control is not (fully) supported by VB.NET and that I...
2
by: Ahmed | last post by:
Hello everyone, I am designing a chat program for a company. The program will be used locally(through lan). I am progamming the chat program using VB.NET windows application. Lets consider the...
1
by: ajaywinds | last post by:
Hi, I had made chat client and Server application in VB which works properly in LAN. But I like to chat with my client application through internet. How I can make this possible? Ajay
8
by: Bllich | last post by:
Greetings, there is a nice multi-user chat application written by Microsoft in VB which code is on the location: http://download.microsoft.com/download/c/4/8/c488a081-85f2-4a2e-a121-...
4
by: Ronald Green | last post by:
Hi, I have this theoretical problem: Say that I need a chat server that also has a database where it saves all conversations. However, I need several servers for availability and load balancing....
2
by: phider1 | last post by:
I wrote a little UDP-based chat program. It's not very user-friendly, requiring every user to add every other user to a list. Anyway, I want to adapt it to TCP so you don't have to do that, as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.