Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-27-2008, 12:33 AM
Member
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0
Kodak07 is on a distinguished road
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
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
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);
        }
        
    }


}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-27-2008, 02:02 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-27-2008, 03:55 AM
Member
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0
Kodak07 is on a distinguished road
Default Solved
I got it working by creating separate threads for the keyboard input and one listening for the server input.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-29-2009, 10:50 PM
Member
 
Join Date: Mar 2009
Posts: 1
Rep Power: 0
eko1892 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi Client TCP or UDP hunterbdb Networking 8 10-17-2008 04:10 AM
Datagram Client and Server, client timer question saru88 Networking 1 10-05-2008 03:12 PM
[SOLVED] simple server chat program, using InputStream and OutputStream. NullPointerE jim478 New To Java 2 07-28-2008 07:32 PM
question about Multi threading in Java fred Advanced Java 1 07-24-2007 01:55 AM
Chat Client Jessi34 New To Java 4 05-31-2007 11:37 AM


All times are GMT +2. The time now is 03:20 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org