Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By Norm

Thread: Help needed in this multi-client chat application

  1. #1
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default Help needed in this multi-client chat application

    Help needed in this multi-client chat application-model.jpg



    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..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help needed in this multi-client chat application

    how can i send that data to the other threads
    The data that any thread receives can be shared among 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.

  3. #3
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default 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?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.

  5. #5
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default Re: Help needed in this multi-client chat application

    Quote Originally Posted by Norm View Post
    You should create the classes that would go in the list. You could use the ArrayList class to how the list.
    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
    Java 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);
            }
            
        }
    }
    CLIENT CODE
    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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.

    one client gets some part of the data and others some.
    How is the data broken into pieces? Can you post the print out that shows this?

    Try debugging the code by adding printlns to show how the variables' values are set and used and to show execution flow.

  7. #7
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default 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)....

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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:
    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()
    Sample Scanner:
    Java Code:
        Scanner in = new Scanner("S msg 1\nS msg 2\nQUIT\n");
    Add lots of printlns to show the execution flow and what class/method is doing what.

  9. #9
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default 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....

  10. #10
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

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

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.

  12. #12
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default 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.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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 == operator
    animit likes this.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    animit is offline Member
    Join Date
    Mar 2012
    Location
    Nepal
    Posts
    14
    Rep Power
    0

    Default 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)??

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help needed in this multi-client chat application

    requestFocusInWindow()

    When in doubt, read the API.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    sreev is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default 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

  1. Replies: 4
    Last Post: 03-31-2011, 10:37 PM
  2. Suggestions on Multi Client Java Application
    By neofite in forum Advanced Java
    Replies: 3
    Last Post: 01-25-2011, 05:24 AM
  3. Need help with chat client
    By the_flirt in forum Networking
    Replies: 4
    Last Post: 04-23-2010, 03:49 PM
  4. Multi-user chat server and client
    By 435.mahesh in forum Java Software
    Replies: 6
    Last Post: 04-25-2009, 12:45 AM
  5. Multi Client/Server Chat Question
    By Kodak07 in forum Networking
    Replies: 3
    Last Post: 03-29-2009, 10:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •