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.
4 2853
Hi,
Are you using sockets and serversockets?
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
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. -
/*
-
* Main.java for CLIENT
-
*
-
* Created on February 16, 2007, 4:24 PM
-
*/
-
-
import java.net.*;
-
import java.io.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
-
public class Main
-
{
-
static TextField tf = new TextField();
-
static TextArea ta = new TextArea();
-
static Button b1 = new Button("SEND");
-
static Button b2 = new Button("CLEAR");
-
-
public Main()
-
{
-
}
-
-
public static void main(String[] args)throws Exception
-
{
-
Frame fr = new Frame();
-
Panel p1 = new Panel();
-
-
p1.add(b1);
-
p1.add(b2);
-
b1.setEnabled(false);
-
fr.add(p1,BorderLayout.SOUTH);
-
fr.add(ta,BorderLayout.NORTH);
-
fr.add(tf,BorderLayout.CENTER);
-
fr.setSize(300,300);
-
fr.setTitle("CLIENT");
-
fr.setVisible(true);
-
-
InetAddress ip = InetAddress.getByName("127.0.0.1");
-
Socket soc = new Socket(ip,9090);
-
System.out.println("CLIENT ON");
-
-
SendData sd1 = new SendData();
-
sd1.SetSoc(soc);
-
b1.addActionListener(sd1);
-
System.out.println("CLIENT CONNECTION ESTABLISHED");
-
-
TClient t = new TClient();
-
t.SetSoc(soc);
-
t.start();
-
-
WindowCheck wc1 = new WindowCheck();
-
fr.addWindowListener(wc1);
-
-
Clear cl = new Clear();
-
b2.addActionListener(cl);
-
-
Datatyped b = new Datatyped();
-
tf.addKeyListener(b);
-
-
//soc.close();
-
}
-
}
-
-
-
/*
-
* SendData.java , Class for ActionListener for Send button in CLIENT
-
*
-
* Created on February 16, 2007, 12:54 PM
-
*/
-
-
class SendData implements ActionListener
-
{
-
private Socket soc;
-
private PrintWriter out;
-
String str = new String();
-
-
public SendData()
-
{
-
}
-
-
public void actionPerformed(ActionEvent e)
-
{
-
try
-
{
-
OutputStream i = soc.getOutputStream();
-
OutputStreamWriter j = new OutputStreamWriter(i);
-
BufferedWriter k = new BufferedWriter(j);
-
PrintWriter out = new PrintWriter(k,true);
-
-
str =Main.tf.getText();
-
Main.ta.append("CLIENT:" + str + "\n");
-
Main.tf.setText("");
-
-
System.out.println("CLIENT:" + str);
-
out.println(str);
-
}
-
-
catch (IOException ex)
-
{
-
ex.printStackTrace();
-
}
-
}
-
-
public void SetSoc(Socket soc) throws Exception
-
{
-
this.soc = soc;
-
}
-
}
-
-
-
/*
-
* TClient.java for CLIENT
-
*
-
* Created on February 16, 2007, 4:24 PM
-
*/
-
-
class TClient extends Thread
-
{
-
-
private Socket soc; //Cannot be made static else each
-
private BufferedReader in; //Client will not get separate socket
-
-
public TClient()
-
{
-
}
-
-
public void run()
-
{
-
try
-
{
-
BufferedReader in = new BufferedReader(
-
new InputStreamReader(
-
soc.getInputStream()));
-
-
-
String Str = in.readLine();
-
while(true)
-
{
-
System.out.println(" FROM SERVER : " + Str);
-
Main.ta.append("SERVER : " + Str + "\n");
-
Str = in.readLine();
-
}
-
-
}
-
-
catch (IOException ex)
-
{
-
ex.printStackTrace();
-
}
-
}
-
-
public void SetSoc(Socket soc) throws Exception
-
{
-
this.soc = soc;
-
}
-
-
}
-
-
-
/*
-
* Datatyped.java for CLIENT
-
*
-
* Created on February 15, 2007, 11:25 PM
-
*/
-
-
class Datatyped implements KeyListener
-
{
-
-
public void keyTyped(KeyEvent e)
-
{
-
-
}
-
-
public void keyPressed(KeyEvent e)
-
{
-
Main.b1.setEnabled(true);
-
}
-
-
public void keyReleased(KeyEvent e)
-
{
-
}
-
-
}
-
-
-
/*
-
* Clear.java , Class for ActionListener for Clear button in CLIENT
-
*
-
* Created on February 20, 2007, 9:32 AM
-
*/
-
-
class Clear implements ActionListener
-
{
-
public Clear()
-
{
-
}
-
-
public void actionPerformed(ActionEvent e)
-
{
-
Main.ta.setText("");
-
}
-
}
-
-
-
/*
-
* WindowCheck.java for CLIENT
-
*
-
* Created on February 16, 2007, 12:31 PM
-
*/
-
-
class WindowCheck implements WindowListener
-
{
-
-
public WindowCheck()
-
{
-
}
-
-
public void windowOpened(WindowEvent e)
-
{
-
}
-
-
public void windowClosing(WindowEvent e)
-
{
-
System.exit(0);
-
}
-
-
public void windowClosed(WindowEvent e)
-
{
-
}
-
-
public void windowIconified(WindowEvent e)
-
{
-
}
-
-
public void windowDeiconified(WindowEvent e)
-
{
-
}
-
-
public void windowActivated(WindowEvent e)
-
{
-
}
-
-
public void windowDeactivated(WindowEvent e)
-
{
-
}
-
-
}
-
code for server -
/*
-
* Main.java for SERVER
-
*
-
* Created on February 16, 2007, 4:24 PM
-
*/
-
-
import java.net.*;
-
import java.io.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
-
public class Main
-
{
-
static TextField tf = new TextField();
-
static TextArea ta = new TextArea();
-
static Button b1 = new Button("SEND");
-
static Button b2 = new Button("CLEAR");
-
-
public Main()
-
{
-
}
-
-
public static void main(String[] args) throws Exception
-
{
-
Frame fr = new Frame();
-
Panel p1 = new Panel();
-
-
p1.add(b1);
-
p1.add(b2);
-
b1.setEnabled(false);
-
fr.add(p1,BorderLayout.SOUTH);
-
fr.add(ta,BorderLayout.NORTH);
-
fr.add(tf,BorderLayout.CENTER);
-
fr.setSize(500,300);
-
fr.setTitle("SERVER");
-
fr.setVisible(true);
-
-
System.out.println("SERVER ON");
-
InetAddress ip = InetAddress.getByName("127.0.0.1");
-
ServerSocket ss = new ServerSocket(9090);
-
Socket soc = ss.accept();
-
-
TServer t1 = new TServer();
-
t1.SetSoc(soc);
-
t1.start();
-
System.out.println("CLIENT CONNECTED TO SERVER ");
-
-
SendData ssd = new SendData();
-
ssd.SetSoc(soc);
-
b1.addActionListener(ssd);
-
-
Clear cl = new Clear();
-
b2.addActionListener(cl);
-
-
WindowCheck wc1 = new WindowCheck();
-
fr.addWindowListener(wc1);
-
-
Datatyped b = new Datatyped();
-
tf.addKeyListener(b);
-
// soc.close();
-
// ss.close();
-
}
-
}
-
-
-
/*
-
* SendData.java for SERVER
-
*
-
* Created on February 16, 2007, 4:24 PM
-
*/
-
-
class SendData implements ActionListener
-
{
-
private Socket soc;
-
private PrintWriter out;
-
String str = new String();
-
-
public SendData()
-
{
-
}
-
-
public void actionPerformed(ActionEvent e)
-
{
-
try
-
{
-
OutputStream i = soc.getOutputStream();
-
OutputStreamWriter j = new OutputStreamWriter(i);
-
BufferedWriter k = new BufferedWriter(j);
-
PrintWriter out = new PrintWriter(k,true);
-
-
str =Main.tf.getText();
-
Main.ta.append("SERVER:" + str + "\n");
-
Main.tf.setText("");
-
-
System.out.println("SERVER:" + str);
-
out.println(str);
-
-
}
-
-
catch (IOException ex)
-
{
-
ex.printStackTrace();
-
}
-
-
}
-
-
public void SetSoc(Socket soc) throws Exception
-
{
-
this.soc = soc;
-
}
-
}
-
-
-
/*
-
* TServer.java for SERVER
-
*
-
* Created on February 16, 2007, 4:24 PM
-
*/
-
-
class TServer extends Thread
-
{
-
private Socket soc; //Cannot be made static else each
-
private PrintWriter out; //client will not get separate socket
-
private BufferedReader in;
-
-
public TServer() throws Exception
-
{
-
-
}
-
-
public void run()
-
{
-
try
-
{
-
-
BufferedReader in = new BufferedReader(
-
new InputStreamReader(
-
soc.getInputStream()));
-
-
PrintWriter out = new PrintWriter(
-
new BufferedWriter(
-
new OutputStreamWriter(
-
soc.getOutputStream())),true);
-
-
String Str = in.readLine();
-
while(true)
-
{
-
System.out.println(" FROM CLIENT : " + Str);
-
Main.ta.append("CLIENT : " + Str + "\n");
-
Str = in.readLine();
-
}
-
}
-
catch (Exception ex)
-
{
-
ex.printStackTrace();
-
}
-
}
-
-
public void SetSoc(Socket soc) throws Exception
-
{
-
System.out.println("CLIENT SOCKET CONNECTED");
-
this.soc = soc;
-
}
-
}
-
-
-
/*
-
* Datatyped.java for SERVER
-
*
-
* Created on February 15, 2007, 11:25 PM
-
*/
-
-
class Datatyped implements KeyListener
-
{
-
-
public void keyTyped(KeyEvent e)
-
{
-
-
}
-
-
public void keyPressed(KeyEvent e)
-
{
-
Main.b1.setEnabled(true);
-
}
-
-
public void keyReleased(KeyEvent e)
-
{
-
}
-
-
}
-
-
-
/*
-
* Clear.java , Class for ActionListener for Clear button in SERVER
-
*
-
* Created on February 20, 2007, 9:23 AM
-
*/
-
-
class Clear implements ActionListener
-
{
-
public Clear()
-
{
-
}
-
-
public void actionPerformed(ActionEvent e)
-
{
-
Main.ta.setText("");
-
}
-
}
-
-
-
/*
-
* WindowCheck.java for SERVER
-
*
-
* Created on February 16, 2007, 4:24 PM
-
*/
-
-
class WindowCheck implements WindowListener
-
{
-
-
public WindowCheck()
-
{
-
}
-
-
public void windowOpened(WindowEvent e)
-
{
-
}
-
-
public void windowClosing(WindowEvent e)
-
{
-
System.exit(0);
-
}
-
-
public void windowClosed(WindowEvent e)
-
{
-
}
-
-
public void windowIconified(WindowEvent e)
-
{
-
}
-
-
public void windowDeiconified(WindowEvent e)
-
{
-
}
-
-
public void windowActivated(WindowEvent e)
-
{
-
}
-
-
public void windowDeactivated(WindowEvent e)
-
{
-
}
-
-
}
-
-
Plz helpme out. Thanks again
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 -
void poll()
-
{
-
Messages newMessages = server.getNewMessages(messageNumber);
-
display(newMessages);
-
messageNumber += newMessages.getSize();
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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
|
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-...
|
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....
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |