troubleshooting Question

threads

Avatar of tequila_dude
tequila_dude asked on
Java
15 Comments1 Solution251 ViewsLast Modified:
hI,

can someone tell me why he only wants to print the name of the user who asked it (/list) and not the other users ?
static Vector chatters = new Vector( );=> this vector does the job
static Vector whoOnline = new Vector( );=>this one not :(

thxs alot

the code
*********************************************************
// Chatter.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;
import java.util.StringTokenizer;

      // *********************************************
      // * Thread, opgestart voor elke nieuwe client *
      // *********************************************

public class Chatter extends Thread // dit is een subklasse van Thread
{
  //declaratie van variabelen
  static Vector chatters = new Vector( );//Vector, omdat een vector kan worden verdeeld over meerdere toepassingsthreads
  static Vector whoOnline = new Vector( );//static variabelen worden authomatisch geshared tussen threads
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
  private String info;
  private String name;
  private String brol;
  private String sex;
 

    public Chatter(Socket socket) throws IOException
    {      //zet de meegegeven socket in de lokale variabele
        this.socket = socket;
        // creër een in- en uitvoerstroom
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
    }//einde Chatter constructor
   
    //run-methode van klasse Thread wordt overschreven (overridden)
    public void run()
    {      
          String line= " ";
            synchronized(chatters) // zodat geen 2 threads tergelijkertijd deze functie kunnen gebruiken
            {
                chatters.addElement(this); //this == object (met socket,input en outputstream dus)
             }
      
            try
            {      
                  //zolang bericht minstens 1 karater bevat, doe :
                while(true)//!(line = in.readLine()).equalsIgnoreCase("/quit"))
                {
                      line = in.readLine();
                      if(line.endsWith("$info$"))
                      {
                            setClientInfo(line);
                      }
                      else if(line.endsWith("/list"))
                      {
                            printAllOnlineClients();
                      }
                      else
                      {                                        
                              System.out.println(line);// schrijf naar client
                              for(int i = 0; i < chatters.size(); i++)//schrijf naar alle ander clients
                              {      
                                    synchronized(chatters)
                                    {
                                          Chatter sendMessage = (Chatter)chatters.elementAt(i);
                                             sendMessage.out.println(line + "\r");
                                          sendMessage.out.flush();
                                    }
                              }
                        }
                }// einde while-lus
                
            } catch(IOException ioe)
                  {
                        System.out.println("Er heeft zich een fout voorgedaan in de communicatie!");
                      //ioe.printStackTrace();
                  } finally
                        {
                            try
                            {
                                    in.close();
                                    out.close();
                                    socket.close();
                            } catch(IOException ioe)
                                  {
                                  } finally
                                        {
                                                synchronized(chatters)
                                                {
                                                      //verwijder client
                                                    chatters.removeElement(this);
                                                }
                                        }
                        }            
    }//einde run-methode
   
    public void setClientInfo(String info)
    {
          StringTokenizer st = new StringTokenizer(info);
          name = st.nextToken();
          sex  = st.nextToken();
          brol = st.nextToken();
          synchronized(whoOnline)
            {
                whoOnline.addElement(name);
          }         
    }
          
    public void printAllOnlineClients()
    {
          for(int i = 0; i < whoOnline.size(); i++)
                              {      
                                  synchronized(whoOnline)
                                    {
                                          String online = (String)whoOnline.elementAt(i);
                                          synchronized(chatters)
                                          {
                                          Chatter sendMessage = (Chatter)chatters.elementAt(i);
                                             sendMessage.out.println(online + "\r");
                                          sendMessage.out.flush();
                                           }
                                          
                                    }      
                            }
    }
   
}//einde klasse Chatter
ASKER CERTIFIED SOLUTION
Mick Barry
Java Developer
Join our community to see this answer!
Unlock 1 Answer and 15 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros