multithreading - Java threads not updating -


i have server , client program in right want send friends list server client. doing when create new client handler thread in server class via objectoutputstream (sending array of string containing usernames of online users).

however when run "broadcast" function talk threads not update them , instead when new client connects previous client connected receive friends list( when connects; doesn't receive friends list. when b connects receives friends list. when c connects; b receives friends list not update)

server code(the bits relevant) :

public class server_frame extends javax.swing.jframe  {     arraylist<string> onlineusers = new arraylist<string>();     arraylist<clienthandler> connectedclients = new arraylist<clienthandler>();     boolean keepgoing;    public class clienthandler extends thread    {     socket sock;     objectinputstream fromclient;     objectoutputstream toclient;       public clienthandler(socket clientsocket)     {        id = ++uniqueid;         this.sock = clientsocket;         system.out.println("thread trying create object input/output streams");         try         {             // create output first             toclient = new objectoutputstream(clientsocket.getoutputstream());             fromclient  = new objectinputstream(clientsocket.getinputstream());             // read username             username = (string) fromclient.readobject();             system.out.println(username + " connected.");             onlineusers.add(username);          }         catch (ioexception e) {             system.out.println("exception creating new input/output streams: " + e);             return;         } catch (classnotfoundexception ex) {             logger.getlogger(server_frame.class.getname()).log(level.severe, null, ex);         }     }     @override     public void run() //displays online users     {         boolean keepgoing = true;             while(keepgoing) {     }  }   public void start() {     keepgoing = true;     /* create socket server , wait connection requests */     try      {         // socket used server         serversocket serversocket = new serversocket(2222);          // infinite loop wait connections         while(keepgoing)          {              // format message saying waiting             system.out.println("server waiting clients on port " + 2222 + ".");              socket socket = serversocket.accept();      // accept connection             // if asked stop             if(!keepgoing)                 break;             clienthandler t = new clienthandler(socket);  // make thread of             connectedclients.add(t);                      // save in arraylist             t.start();             broadcast();           }   private synchronized void broadcast() throws ioexception {   // loop in reverse order in case have remove client // because has disconnected       // add hh:mm:ss , \n message      // display message on console or gui     for(int = 0; < connectedclients.size(); i++) {                 clienthandler ct = connectedclients.get(i);                 ct.toclient.writeobject(onlineusers);     }      //system.out.print(message);   } 

client code:

public boolean start(string username) {     // try connect server     try {         sock = new socket(address, port);     }      // if failed not can     catch(exception ec) {         system.out.println("error connectiong server:" + ec);         return false;     }      string msg = "connection accepted " + sock.getinetaddress() + ":" + sock.getport();     system.out.println(msg);      /* creating both data stream */     try     {         fromserver  = new objectinputstream(sock.getinputstream());         toserver = new objectoutputstream(sock.getoutputstream());     }     catch (ioexception eio) {         system.out.println("exception creating new input/output streams: " + eio);         return false;     }      // creates thread listen server      new listenfromserver().start();     // send our username server message     try     {         toserver.writeobject(username);     }     catch (ioexception eio) {         system.out.println("exception doing login : " + eio);         disconnect();         return false;     }     // success inform caller worked     return true; }  class listenfromserver extends thread {      public void run() {                 //loaddata();                 system.out.println("thread started");         while(true) {             try {                                 if(fromserver.readobject() != null)                                 {                                     system.out.println("reading online users");                                     onlineusers = (arraylist<string>) fromserver.readobject();                                                 loadonlineusers(onlineusers);                                  }                                 //thread.sleep(1000);                                 //if user connects                                  //onlineusers = (arraylist<string>) fromserver.readobject();                                 //loadonlineusers(onlineusers);               }             catch(ioexception e) {                 system.out.println("server has close connection: " + e);              }             // can't happen string object need catch anyhow             catch(classnotfoundexception e2) {                                 system.out.println("not found");             }         }     } }   private void loadonlineusers(arraylist<string> users) {           defaultlistmodel model = new defaultlistmodel();      (int = 0; < users.size(); i++){         model.addelement(users.get(i));     }      lst_connected_users.setmodel(model);  } 

am calling broadcast in correct place? correctly broadcasting clients onlineusers array? or clients not reading correctly? thanks


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -