Global Chat Room

Hi i have a simple chat that it work with thread but i want one jteaxtarea sharing into all my threads plz help me...thanks
[151 byte] By [mike.kevina] at [2007-10-2 10:25:20]
# 1

Hello mike,

I presume you have a single server to initialize the Chat application and multiple clients representing multiple threads with each thread having a textarea to globally display the text what other user has been typing.

Do you mean that whatever is typed by one user after logging after a couple of minutes should view the contents of the entire conversation held in that room as currently he is able to view the contents of the message typed after he has logged in and that too in the textarea of his own.

What do you mean by one single TextArea sharing in all threads? Does it mean the understanding mentioned above? Pls clarify.

rgds,

Seetesh

seteshhindlea at 2007-7-13 2:00:54 > top of Java-index,Archived Forums,Socket Programming...
# 2
i want all users can see Jtextarea
mike.kevina at 2007-7-13 2:00:54 > top of Java-index,Archived Forums,Socket Programming...
# 3
What I meant was "All users can have their individual TextArea where they can see all the messages posted" or something other than this.rgds,Seetesh
seteshhindlea at 2007-7-13 2:00:54 > top of Java-index,Archived Forums,Socket Programming...
# 4
Is the TextArea common to all users or each user will have his own TextArea which will be updated with the latest messages sent by each user.
seteshhindlea at 2007-7-13 2:00:54 > top of Java-index,Archived Forums,Socket Programming...
# 5

this is my code

client :import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.net.*;

class SocketClient extends JFrame implements ActionListener {

JLabel text, clicked;

JButton button;

JPanel panel;

JTextField textField;

Socket socket = null;

PrintWriter out = null;

BufferedReader in = null;

SocketClient(){ //Begin Constructor

text = new JLabel("Text to send over socket:");

textField = new JTextField(20);

button = new JButton("Click Me");

button.addActionListener(this);

panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBackground(Color.white);

getContentPane().add(panel);

panel.add("North", text);

panel.add("Center", textField);

panel.add("South", button);

} //End Constructor

public void actionPerformed(ActionEvent event){

Object source = event.getSource();

if(source == button){

//Send data over socket

String text = textField.getText();

out.println(text);

textField.setText(new String(""));

//Receive text from server

try{

String line = in.readLine();

textField.setText(line);

//System.out.println("Text received :" + line);

} catch (IOException e){

System.out.println("Read failed");

System.exit(1);

}

}

}

public void openSocket(){

//Create socket connection

try{

socket = new Socket("localhost", 4444);

out = new PrintWriter(socket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

} catch (UnknownHostException e) {

System.out.println("Unknown host.");

System.exit(1);

} catch (IOException e) {

System.out.println("No I/O");

System.exit(1);

}

}

public static void main(String[] args){

SocketClient frame = new SocketClient();

frame.setTitle("Client Program");

WindowListener l = new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

};

frame.addWindowListener(l);

frame.pack();

frame.setVisible(true);

frame.openSocket();

}

}

server :import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.net.*;

class ClientWorker implements Runnable {

private Socket client;

private JTextArea textArea;

ClientWorker(Socket client, JTextArea textArea) {

this.client = client;

this.textArea = textArea;

}

public void run(){

String line;

BufferedReader in = null;

PrintWriter out = null;

try{

in = new BufferedReader(new InputStreamReader(client.getInputStream()));

out = new PrintWriter(client.getOutputStream(), true);

} catch (IOException e) {

System.out.println("in or out failed");

System.exit(-1);

}

while(true){

try{

line = in.readLine();

//Send data back to client

out.println(line);

textArea.append(Thread.currentThread().getName()+": "+line+"\n");

} catch (IOException e) {

System.out.println("Read failed");

System.exit(-1);

}

}

}

}

class SocketThrdServer extends JFrame{

JLabel label = new JLabel("Text received over socket:");

JPanel panel;

JTextArea textArea = new JTextArea();

ServerSocket server = null;

SocketThrdServer(){ //Begin Constructor

panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBackground(Color.white);

getContentPane().add(panel);

panel.add("North", label);

panel.add("Center", textArea);

} //End Constructor

public void listenSocket(){

try{

server = new ServerSocket(4444);

} catch (IOException e) {

System.out.println("Could not listen on port 4444");

System.exit(-1);

}

while(true){

ClientWorker w;

try{

w = new ClientWorker(server.accept(), textArea);

Thread t = new Thread(w);

t.start();

} catch (IOException e) {

System.out.println("Accept failed: 4444");

System.exit(-1);

}

}

}

protected void finalize(){

//Objects created in run method are finalized when

//program terminates and thread exits

try{

server.close();

} catch (IOException e) {

System.out.println("Could not close socket");

System.exit(-1);

}

}

public static void main(String[] args){

SocketThrdServer frame = new SocketThrdServer();

frame.setTitle("Server Program");

WindowListener l = new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

};

frame.addWindowListener(l);

frame.pack();

frame.setVisible(true);

frame.listenSocket();

}

}

mike.kevina at 2007-7-13 2:00:54 > top of Java-index,Archived Forums,Socket Programming...
# 6
but i want all user can see all contain chat
mike.kevina at 2007-7-13 2:00:54 > top of Java-index,Archived Forums,Socket Programming...
# 7

In this case the client just types in some text and clicks on the Button and the Server displays all the messages posted by each user/thread.

I have a similar application code where each user connects to the Chat application and enters the text. All subsequently connect users can send text messages where they all can see the same on their individual textarea rather than storing all the messages from all users on the server's textarea.

Something similar to yahoo conference/chat.

If this is something that you are looking for then give me ur email id, I will post the snippet code across.

Rgds,

Seetesh

seteshhindlea at 2007-7-13 2:00:54 > top of Java-index,Archived Forums,Socket Programming...
# 8
thanks mike.kamel@yahoo.com
mike.kevina at 2007-7-13 2:00:55 > top of Java-index,Archived Forums,Socket Programming...
# 9
i am sorry this is correctmike.kamel@yahoo.co.in
mike.kevina at 2007-7-13 2:00:55 > top of Java-index,Archived Forums,Socket Programming...
# 10
are you have conference source code?Thanks...
mike.kevina at 2007-7-13 2:00:55 > top of Java-index,Archived Forums,Socket Programming...
# 11
Have sent you the mail containing the code for chat.I am currently working on playing the sound on click of a button ie whatever messages typed by many clients will be transmitted/played as audio.Sort of a Voice Chat application.Rgds,Seetesh
seteshhindlea at 2007-7-13 2:00:55 > top of Java-index,Archived Forums,Socket Programming...
# 12
Thanks seetesh but this program is application and i wnat codeplz if you can send me your source code
mike.kevina at 2007-7-13 2:00:55 > top of Java-index,Archived Forums,Socket Programming...
# 13

I have to browse through my system to dig up the source code else connect to our CVS for the same. Will get back to you on that soon.

Well You could use the Java Decompiler to convert the .class files to .java

www.softpedia.com/progDownload/ DJ-Java-Decompiler-Download-13481.html

Rgds,

Seetesh

seteshhindlea at 2007-7-13 2:00:55 > top of Java-index,Archived Forums,Socket Programming...
# 14
hi Seetesh in your program when a user joining, this user can`t see before textsi want he can see all text transferthanks ...
mike.kevina at 2007-7-13 2:00:55 > top of Java-index,Archived Forums,Socket Programming...
# 15
Can U please send me the code that u r mentioning?My Id is itsharychatter@yahoo.com
itsharychattera at 2007-7-20 20:44:16 > top of Java-index,Archived Forums,Socket Programming...
# 16

Please, this is a public forum! Posting your Email address or requesting that other users post theirs is an invitation to SPAM. If the code is so interesting, maybe, we all would like to see it. so post it or a URL that points to it here.

If you want some code that does message switching, I've posted it on the topics:

[url=http://forum.java.sun.com/thread.jspa?threadID=699227]Smple Socket Client [/url]

[url=http://forum.java.sun.com/thread.jspa?threadID=670264]Simple Socket Server[/url]

You can also find most of this in many of the better Java tutorials including those online and "Head First Java".

pkwoostera at 2007-7-20 20:44:16 > top of Java-index,Archived Forums,Socket Programming...
# 17

hi Mike

Ur problem can be solved using multithreading twice.

One set of threads(client thread) is for the connection that each client makes with the server.

The second set of threads are Writerthreads that wud just do the job of communicating the message to everyone online.

Now..once a client has connected to the server..add this client to an ArrayList whose object/data type must be an output stream. now..code a routine using a for loop that sends the message posted by a client thread and read by the server to all the clients in the arraylist. Everytime..the server gets some input stream from any client thread..it shud invoke one such thread(a writer thread) which uses the above mentioned routine to post the message to every other client.

I think my way of explaining was very confusing but thats all i can tell coz i actually have developed exactly wat u want..but i cant give u the code..coz of security reasons...

Deepak

dipakucha at 2007-7-20 20:44:16 > top of Java-index,Archived Forums,Socket Programming...
# 18

Hello friends,

The problem can also be solved (in case the number of clients are small) by having a collection of OutputStreams of all the clients. And when message arrives. let the client thread iterate over this collection and dispach this massage to every connected client (including itself).

Example Code

class Client extends Thread

{//collection of all clients

static ArrayList clients=new ArrayList();

// Intance variables

Socket skt;

BufferedReader br;

PrintStream ps;

Client(Socket s)

{

try{skt=s;

br=new BufferedReader(new InputStreamReader(skt.getInputStream());

ps=new PrintStream(skt.getOutputStream());}catch(Exception e)

{e.printStackTrace();}

start();

//add this client to collection

sychronized(clients){ clients.add(this);}

}

public void run()

{

String ln="";

while(ln!=null)

{ ln=br.readLine(); // wait for communication

if(ln==null) //connection closed

continue;

try{

synchronized(clients)

{Iterator itr= clients.iterator();

while(itr.hasNext())

((Client)itr.next()) . println(ln);

}

}catch(Exception e){e.printStackTrace();}

//remove this client from the collection

clients.remove(this);

}

}

}///end class clienf

}

vaibhav92a at 2007-7-20 20:44:16 > top of Java-index,Archived Forums,Socket Programming...
# 19
Obivously you haven't run this code. You must exclude the current client from the broadcast. Otherwise there will be a combinatorial explosion of broadcasts.
ejpa at 2007-7-20 20:44:16 > top of Java-index,Archived Forums,Socket Programming...
# 20

Dear ejp,

Thank you very much for you kind suggestion. My humble request to you is ro re-examine the code so that you can clear up your suspicion. If in case you are unable to do so then let me guide me:-

In networks Sending & Receiving are two DISTINCT operations. If i send a message to a client then It will only receive it .. that does not mean that the client will RESENT the message back to the client.

Then where does you so called *Combitorial Explosion* comes into picture.

I would request you to run the code your self .. rather than pointing to what you couldnt understand.

VJ

vaibhav92a at 2007-7-20 20:44:16 > top of Java-index,Archived Forums,Socket Programming...