Results 1 to 16 of 16
- 03-03-2012, 04:31 AM #1
Member
- Join Date
- Mar 2012
- Location
- Nepal
- Posts
- 8
- Rep Power
- 0
Help needed in this multi-client chat application

here in this model i have created a server which listens for the client request...as soon as one comes it creates a thread to deal with that client and the main server again goes back to listen state...
As u can see there are three clients and therefore three threads for each client..i have created furthermore two threads named Send and Recv in each thread for sending and receiving data to/from clients..
Now where i am stuck is, when Client 1 sends something to server, Recv thread of Thread 1 gets it but how can i send that data to the other threads (Thread 2 and Thread 3) so that Client 2 and Client 3 can get it..
Any help will be appreciated...thanks in advance..
Note: each client has got is own Send and Recv thread for sending and receiving data..
- 03-03-2012, 07:32 PM #2
Re: Help needed in this multi-client chat application
The data that any thread receives can be shared among the other threads.how can i send that data to the other threads
You could use a queue/list to save new data and that the other threads could go through to see it there are messages to be sent.
- 03-04-2012, 07:21 AM #3
Member
- Join Date
- Mar 2012
- Location
- Nepal
- Posts
- 8
- Rep Power
- 0
Re: Help needed in this multi-client chat application
Is there any specific class in java to do that task? or have i to create the queue/list all by myself?
- 03-04-2012, 01:10 PM #4
Re: Help needed in this multi-client chat application
You should create the classes that would go in the list. You could use the ArrayList class to how the list.
- 03-04-2012, 03:39 PM #5
Member
- Join Date
- Mar 2012
- Location
- Nepal
- Posts
- 8
- Rep Power
- 0
Re: Help needed in this multi-client chat application
i didn't get u!! can u please elaborate what i am supposed to do?
m posting my code here...it is incomplete and not fully functional as i wish it to be...right now its multi-client and server app..clients can connect and send data to server but when i type something in server clients get them in pieces..i mean to say one client gets some part of the data and others some..but that's not the problem because i want to write multi-client chat application for which clients need to exchange data between them...so please help me with some ideas...
SERVER CODE
CLIENT CODEJava Code:import java.io.DataInputStream; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; class SThread extends Thread { Socket sock; DataInputStream in=new DataInputStream(System.in); PrintStream outp; SThread(Socket s) { sock=s; try { outp=new PrintStream(sock.getOutputStream()); } catch(Exception e) { System.out.println(e); } } @Override public void run() { try { while(true) { String line=in.readLine(); if(line.trim().toUpperCase().equals("QUIT")) { break; //System.exit(1); } else outp.println(line); } outp.close(); sock.close(); } catch(Exception e) { System.out.println(e); } } } class RThread extends Thread { Socket new_sock; DataInputStream inp; RThread(Socket s) { new_sock=s; try { inp=new DataInputStream(new_sock.getInputStream()); } catch(Exception e) { System.out.println(e); } } @Override public void run() { try { while(true) { String line1=inp.readLine(); if(line1==null) { break;//System.exit(1); } else System.out.println("Client: "+line1); } inp.close(); new_sock.close(); } catch(Exception e) { System.out.println(e); } } } class ServerThread extends Thread { Socket socket; int n; ServerThread(Socket s,int x) { socket=s; n=x; } @Override public void run() { SThread sendt=new SThread(socket); RThread recvt=new RThread(socket); sendt.start(); recvt.start(); } } public class Server { public static void main(String[] args) { int tn=1; try { ServerSocket s=new ServerSocket(10000); while(true) { Socket sock=s.accept(); ServerThread st=new ServerThread(sock,tn); st.start(); tn++; } } catch(Exception e) { System.out.println(e); } } }
Java Code:import java.io.*; import java.net.*; class SendThread extends Thread { Socket sock; DataInputStream in=new DataInputStream(System.in); PrintStream outp; SendThread(Socket s) { sock=s; try { outp=new PrintStream(sock.getOutputStream()); } catch(Exception e) { System.out.println(e); } } @Override public void run() { try { while(true) { String line=in.readLine(); if(line.trim().toUpperCase().equals("QUIT")) { break; //System.exit(1); } else outp.println(line); } outp.close(); sock.close(); } catch(Exception e) { System.out.println(e); } } } class RecvThread extends Thread { Socket new_sock; DataInputStream inp; RecvThread(Socket s) { new_sock=s; try { inp=new DataInputStream(new_sock.getInputStream()); } catch(Exception e) { System.out.println(e); } } @Override public void run() { try { while(true) { String line1=inp.readLine(); if(line1==null) { break;//System.exit(1); } else System.out.println("Server: "+line1); } inp.close(); new_sock.close(); } catch(Exception e) { System.out.println(e); } } } public class Client { public static void main(String[] args) { try{ Socket s=new Socket("localhost",10000); System.out.println("Connection Established\nEnter QUIT to exit"); SendThread sendt=new SendThread(s); RecvThread recv=new RecvThread(s); sendt.start(); recv.start(); } catch(Exception e) { System.out.println(e); } } }Last edited by Norm; 03-04-2012 at 03:45 PM. Reason: added code tags
- 03-04-2012, 03:50 PM #6
Re: Help needed in this multi-client chat application
Create a class to hold all the information for a client.
Create an instance of the class for each client that connects to the server.
Save that instance of the class in an arraylist that the server can use to get the data it needs to communicate with any of the clients.
How is the data broken into pieces? Can you post the print out that shows this?one client gets some part of the data and others some.
Try debugging the code by adding printlns to show how the variables' values are set and used and to show execution flow.
- 03-04-2012, 04:56 PM #7
Member
- Join Date
- Mar 2012
- Location
- Nepal
- Posts
- 8
- Rep Power
- 0
Re: Help needed in this multi-client chat application
okay i think i understand a bit of what u are trying to explain...i'll try that and come back again..
as for the data being broken, sorry but i got it wrong...i mean to say that the data I type in server is received by only one client at a time but not by both..
if there are two clients connected and i have to say "HELLO! CLIENT" which i type in the server window, the message isn't sent to both clients..its sent to only one...if i again type "HI" then it too is sent to only one client (the client could be the same which received the first message)....
- 03-04-2012, 05:05 PM #8
Re: Help needed in this multi-client chat application
For testing client server apps I like to run all the code in one JVM. I do this by having a class with a main method that calls the main methods of all the other classes to be tested.
Also for testing I use the Scanner class's constructor that takes a String. That makes for consistent testing with the same Strings for each test.
With multiple clients, I'd pass an ID String to the client's constructor that it could use in its printouts so you know which client printed which message.
Sample main:
Sample Scanner:Java Code:public static void main(final String[] args) { Thread t = new Thread(new Runnable() { public void run() { Server.main(args); } }); t.start(); Thread t2 = new Thread(new Runnable() { public void run() { Client.main(args); } }); t2.start(); } // end main()
Add lots of printlns to show the execution flow and what class/method is doing what.Java Code:Scanner in = new Scanner("S msg 1\nS msg 2\nQUIT\n");
- 03-04-2012, 05:12 PM #9
Member
- Join Date
- Mar 2012
- Location
- Nepal
- Posts
- 8
- Rep Power
- 0
Re: Help needed in this multi-client chat application
i'll give it a try...
thanks a lot for your precious time... :)
i will be posting after i test the code....
- 04-03-2012, 03:11 PM #10
Member
- Join Date
- Mar 2012
- Location
- Nepal
- Posts
- 8
- Rep Power
- 0
Re: Help needed in this multi-client chat application
hi norm! i have something to ask u..
The chat app i was trying to code has been completed..i didn't go with my original code but modified it..and i also added the feature to see the online users using ArrayList, ALL THANKS TO U!! i got the idea of ArrayList only when u mentioned it here...and further more i built it in GUI using the same code...
Anyways! now there are some minor bugs i want to correct..and i hope u'll help me with that too..
u can think of the yahoo chat interface..there is the Text Area where we type our message and click send button to send the message and the message appears on the screen (Animit: hello fern!)...everything's fine except that when there is no msg and i click send button, even then, username with blank msg appears on screen (Animit: )
i want nothing to appear on screen when there is no msg in the Text Area even though i click send button..
my code is like this:
//this code gets executed when i click the send button
String msg=msgTextArea.getText();
msgTextArea.setText(null);
showMsgTextArea.append(username+": "+msg+newline);
- 04-03-2012, 03:16 PM #11
Re: Help needed in this multi-client chat application
Use an if statement to test for an empty message and don't send it if empty.
If you don't understand my response, don't ignore it, ask a question.
- 04-03-2012, 03:37 PM #12
Member
- Join Date
- Mar 2012
- Location
- Nepal
- Posts
- 8
- Rep Power
- 0
Re: Help needed in this multi-client chat application
i tried that too...
if(msg.trim()!=" " && msg!=null)
{
showMsgTextArea.append(username+": "+msg+newline);
msgTextArea.setText(null);
}
this doesn't work.Last edited by animit; 04-03-2012 at 03:40 PM.
- 04-03-2012, 03:40 PM #13
Re: Help needed in this multi-client chat application
The String class has methods you can use to test the contents of a String.
Use the equals() method for comparing the contents of a String, not the == operatorIf you don't understand my response, don't ignore it, ask a question.
- 04-03-2012, 03:53 PM #14
Member
- Join Date
- Mar 2012
- Location
- Nepal
- Posts
- 8
- Rep Power
- 0
Re: Help needed in this multi-client chat application
oh! how dumb i was!!! i know the method..in fact i have used it on many if statements in the same application but forgot to use here... :D
thanks again!!
Umm..there is something more...after i click the send button, i again have to click the Text Area for typing message....is there a way to make the Text Area gain back the focus so that i don't have to click it every time after i send some message(i.e click the send button)??
- 04-03-2012, 05:10 PM #15
Re: Help needed in this multi-client chat application
requestFocusInWindow()
When in doubt, read the API.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-05-2012, 06:46 PM #16
Member
- Join Date
- Apr 2012
- Posts
- 1
- Rep Power
- 0
Re: Help needed in this multi-client chat application
hi...if u don mind can u just upload ur code here....i am doing a miniproject in this topic and i have a problem in connecting the clients from other systems to the server program running on one particular system. Is ur program the same what i am looking for?
Similar Threads
-
Voice chat with multiple chat rooms, suggestions needed
By sonofrage in forum NetworkingReplies: 4Last Post: 03-31-2011, 10:37 PM -
Suggestions on Multi Client Java Application
By neofite in forum Advanced JavaReplies: 3Last Post: 01-25-2011, 05:24 AM -
Need help with chat client
By the_flirt in forum NetworkingReplies: 4Last Post: 04-23-2010, 03:49 PM -
Multi-user chat server and client
By 435.mahesh in forum Java SoftwareReplies: 6Last Post: 04-25-2009, 12:45 AM -
Multi Client/Server Chat Question
By Kodak07 in forum NetworkingReplies: 3Last Post: 03-29-2009, 10:50 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks