Results 1 to 4 of 4
  1. #1
    Kodak07 is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default Multi Client/Server Chat Question

    I am making a chat client/server app. When i run the program only the client that sent the message is getting it back from the server. I think it is sitting on the userInput line waiting for keyboard input, how do i make it continue to loop?


    Thanks and i hope i put this in the right forum code(server and Client) is below

    Client Code
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    
    /**
     *
     * @author bcoates
     */
    import java.net.*;
    import java.io.*;
    public class chatClient {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
        {
    	Socket echoSocket=null;
    	PrintWriter out=null;
    	BufferedReader in = null;
            // TODO code application logic here
            try
            {
                echoSocket = new Socket ("localhost", 8888);
    	    out = new PrintWriter(echoSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
                
            }
            catch(UnknownHostException e)
            {
                System.out.println("Dont know about that host");
            }
            catch(IOException e)
            {
                System.out.println("something blew up:"+e);
            }
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String userInput=null;
    	String serverOut=null;
            
            while(true)
            {
                
    		try
    		{
                        userInput=stdIn.readLine();
                                                    
                    }
    	    catch(IOException e)
    		{
    			System.out.println("something blew up" +e);
    	        System.exit(1);
    		}
                
                if(userInput!=null)
                              out.println(userInput); 
                try
                {
                    serverOut = in.readLine();
                    
                    
                    
                }
                catch(IOException e)
    		{
    			System.out.println("something blew up" +e);
    	        System.exit(1);
    		}
                if(serverOut!=null)
                              System.out.println(serverOut);
                
            }
          }
    
    }
    Server Code
    Java Code:
    /**
     *
     * @author bcoates
     */
    import java.net.*;
    import java.io.*;
    import java.util.*; 
    public class multiServer 
    {
        
          // HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
          static protected List<PrintWriter> writers = new ArrayList<PrintWriter>();
       
        public static void main(String[] args) 
        {  
            int i=1;
           try
           {
                     ServerSocket servsock = new ServerSocket(8888, 5);
    		 Socket incoming;
                     System.out.println("The chat server is running.");
                     
    
    		while(true)
    		   {
    			 incoming = servsock.accept();
    			 multiServerThread connection = new multiServerThread(incoming);
                             Thread t = new Thread(connection);
    			 
                             t.start();
                             
                       }
                     
           } 
           catch(IOException e)
            {
                System.out.println("couldnt make socket");
            }
            
            
        }
    
    }
    
    
    class multiServerThread implements Runnable
    {
        Socket incoming;
        PrintWriter out=null;
        PrintWriter broad=null;
        BufferedReader in = null;
        String cliString=null;
        int id;
        
        multiServerThread(Socket incoming)
        {	
            this.incoming=incoming;
            //this.id=id;         
        }
        
        public void run()
        {
            
            try
            {
               out = new PrintWriter(incoming.getOutputStream(), true);
               //multiServer.writers.add(out);
               System.out.println("Number of Clients: " +multiServer.writers.size());
                in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
                multiServer.writers.add(out);
                while(true)
                {
                   try
                   {
                       cliString = in.readLine();
                       
                       if(cliString !=null)
                       {
                           System.out.println("Got message: "+cliString);
                           for (int i=0; i<multiServer.writers.size();i++) 
                         {
                            broad=multiServer.writers.get(i);
                            broad.println(cliString);
                            
                         }
     
                           //out.println(cliString);
                       }
                           
                      
                   } 
                   catch (IOException e)
                   {
                       System.out.println("yup it blew up here"+e);
                   }
                }
            } catch(IOException e)
            {
                System.out.print("boom goes the dynamite"+ e);
            }
            
        }
    
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    how do i make it continue to loop?
    Is the "it" the client code? If you want to code to do two things at the same time, you'll need to have the things running on different threads.

    Comment on your code:
    put printStackTrace() calls in your catch blocks so you'll know why and where the exception occured

  3. #3
    Kodak07 is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default Solved

    I got it working by creating separate threads for the keyboard input and one listening for the server input.

  4. #4
    eko1892 is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default

    Hi, i've tried your sytax's their working fine but i was wondering if there is any chance to tell me how did you do the loop thing and second keyboard entry from server. Please reply to me a.s.a.p Thanks in advance.

Similar Threads

  1. Chat Client
    By Jessi34 in forum New To Java
    Replies: 5
    Last Post: 12-09-2009, 03:49 PM
  2. Multi Client TCP or UDP
    By hunterbdb in forum Networking
    Replies: 8
    Last Post: 10-17-2008, 04:10 AM
  3. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  4. Replies: 2
    Last Post: 07-28-2008, 07:32 PM
  5. question about Multi threading in Java
    By fred in forum Advanced Java
    Replies: 1
    Last Post: 07-24-2007, 01:55 AM

Posting Permissions

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